Image = imread('1.jpg');
figure,imshow(Image);
title(' [原图] ');
mainfc;
Theshold = graythresh(Image);
Image_BW=im2bw(Image,Theshold);
figure,imshow(Image_BW);
title(' [初次二值化图像] ');
Image_BW_medfilt= medfilt2(Image_BW,[13 13]);
figure,imshow(Image_BW_medfilt);
title('[中值滤波后的二值化图像] ');
Optimized_Image_BW = Image_BW_medfilt|Image_BW;
figure,imshow(Optimized_Image_BW);
title(' [进行“或”运算优化图像效果] ');
Reverse_Image_BW =~Optimized_Image_BW;
figure,imshow(Reverse_Image_BW);
title(' [优化后二值化图象取反]' );
Filled_Image_BW = bwfill(Reverse_Image_BW ,'holes');
figure, imshow(Filled_Image_BW);
title(' [已填充背景色的二进制图像]' );
SE = strel('disk',4);
Open_Image_BW = imopen(Filled_Image_BW,SE);
figure, imshow(Open_Image_BW);
title(' [开运算后的图像]' );
[Label, Number]=bwlabel(Open_Image_BW,8);
Array=bwlabel(Open_Image_BW,8);
Sum=[];
for i= 1:Number
[r,c]=find(Array==i);
rc=[r c];
Num = length(rc);
Sum(i)=Num;
end
Sum;
N=0;
for i=1:length(Sum)
if(Sum(i))>1500
N=N+1;
end
end
Number=Number+N