A=imread('3.jpg');
figure(1);
subplot(241);
imshow(A);title('原图');
I=rgb2gray(A);
subplot(242);;imshow(I);title('灰度图');
K=medfilt2(I,[16,16]);
subplot(243);;imshow(A);title('中值滤波');
s=size(K);
all_white=255*ones(s(1),s(2));
all_white_uint8=uint8(all_white);
L=imsubtract(all_white_uint8,K);
level=graythresh(L);
I2=im2bw(L,level);
subplot(244);
imshow(I2);title('二值图');
se=strel('square',3);
J=imopen(I2,se);
J1=imclose(J,se);
subplot(245);imshow(J1);title('形态学处理');
J1=~J1;
subplot(246);imshow(J1);title('形态学处理');
mainfc;
hold on
subplot(241);
[L,num1] = bwlabel(J1,8); %区域标记,
STATS = regionprops(L, 'all');
for k = 1:num1
%area(k)=STATS(k).Area; %计算各区域的面积。
boundary = STATS(k).BoundingBox;
rectangle('Position',boundary,'edgecolor','r' );
end
BW=edge(J1,'canny');
subplot(247);imshow(BW);title('边缘检测');
BW1=bwareaopen(BW,400,8);
subplot(248);imshow(BW1);title('去除小面积');