clc
[filename, cd1] = uigetfile( ...
{'*.tif;*.TIF;*.JPG;*.jpg;*.bmp;*.BMP;*.jpeg;*.JPEG;','Image file';...
'*.*', 'All file (*.*)'},'Pick an Image');
d = imread(filename);
figure(1)
imshow(d);
title('原图');
%% 提取
% 红色特征字体
% 提取红色分量
Image=d;
GI1 = (Image(:,:,1)>110 & Image(:,:,2)<150 & Image(:,:,3)<150 ...
& abs(double(Image(:,:,2))-double(Image(:,:,3)))<50 & abs(double(Image(:,:,1))-double(Image(:,:,2)))>30);
%%GI白色像素个数
figure(2)
imshow(GI1);
title('初步定位')
% 分割标志
d = GI1;
se = ones(8); % 腐蚀膨胀模版
d = imdilate(d,se);% 做膨胀运算
figure(3)
imshow(d);
title('膨胀');
figure(4);
imshow(Image);
title('定位图');
L = bwlabel(d);%默认8连通; bwlabe(bw,4);
STATS = regionprops(L,'all');%STATS中含有所有连通域的properations
%在bw图像上绘制出连通域的矩形框
hold on
for i = 1 : 4
boundary = STATS(i).BoundingBox;
rectangle('Position',boundary,'edgecolor','b' );
end