/ Published in: MatLab
This code uses the Expectation Maximization algorithm for automatically thresholding an image.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
%read image imshow(img1); %parameter initialization for EM-Algo temp =[]; phi1 = 0.5; phi2 = 0.5; u1 = 0; u2 = 50; sig1 = 2; sig2 = 3; for k = 1 : 3 %Expectation Step probc1 = ( prob1*phi1 ) / ( (prob1*phi1)+ (prob2*phi2) ); probc2 = ( prob2*phi2 )/( (prob1*phi1) + (prob2*phi2) ); temp = [temp ; probc1 probc2]; end %Maximization Step temp = []; end %Thresholding thresh = ((u2 + u1)/2); if img1(m) > thresh img1(m) = 255; else img1(m) = 0; end end imshow(img1);