How to do a Algoritm of globals' thresholding segmentation?

1 view (last 30 days)
Hi:
I must do a practice of a globals' thresholding segmentation for one leason at university, and I'm a little bit lost.
This is the code if have done, but it has a bug...Could some one help me?
im=imread('C:\Users\Fran\Desktop\Universidad\Ing. Industrial\5º AÑO\1º Cuatrimestres\Vision por computador\Imagenes\texto.jpg');
U=graythresh(im);
US=im2double(im);
UI=im2bw(US);
U0=0.5;
[nfilas, ncolumnas]=size(UI);
tic
while U<U0
G1M=0;G1=0;
G2M=0;G2=0;
for i=1:nfilas
for j=1:ncolumnas
U1=im(i,j);
if U1>=U
G1=G1+U1;
G1M=G1M+1;
im(i,j)=1;
end
if U1<U
%G2A=G2;
G2=G2+U1;
G2M=G2M+1;
im(i,j)=0;
end
end
M1=G1/G1M;
M2=G2/G2M;
U=0.5*(M1+M2);
end
figure, imshow(im);
end
toc;
Thanks a lot
  1 Comment
Matt J
Matt J on 3 Nov 2013
Edited: Matt J on 3 Nov 2013
PLease provide information about the bug, i.e., how you know there is one. If there are error messages, copy/paste them in full.

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 3 Nov 2013
You don't need that for loop. Simply do
binaryImage = im>U; % Binarize.
G1M = sum(im(binaryImage)); % Sum gray levels above threshold (for some reason???)
G2M = sum(im(~binaryImage)); % Sum gray levels below threshold (for some reason???)
Not sure what G1M and G2M were computed for.

Francisco
Francisco on 3 Nov 2013
the exercice is:
Other way to chose the threshold "Automaticly", is doing the next steps:
  1. Chose one initial threshold U, it's recommended to be betwen maximun and minimum intensity levels of the image.
  2. Do two groups, G1 pixel with a value of intensity >=U,and G2 pixel with value intensity <U.
  3. Calculate average intensity m1 and m2 for pixels of G1 and G2.
  4. Calculate new threshold: U=0.5(m1+m2),
  5. Repeat steps 2 and 4, until the diference in U in diferents iterations are nether than U0=0.5

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!