function showthresh(path) dirls=dir(path); for i=1:length(dirls) %for i=1:1 img=imread(dirls(i).name); % convert image to range 0-1 a=double(img); b=(a-min(min(a)))./(max(max(a))-min(min(a))); figure(1) imshow(b); % threshold using otsu's method c=im2bw(b,graythresh(b)); figure(2) imshow(c); % find connected components (objects) d=bwlabel(c); % max is the number of objects there are mxd=max(max(d)); % find the index of the largest object % imhist expects the image to be scaled 0-1 so divide by % number of objects before calling it [sz,x]=imhist(d/mxd); [y,indx]=max(sz(2:size(sz,1))); % take original image and mask out everything but the % biggest object e=b; e(find(d~=indx))=0; figure(3) imshow(e,[min(min(e)) max(max(e))]); end