clc
clear
[name,path]=uigetfile({'*.jpg';'*.bmp';'tif'},'载入图像');
x=imread([path,name]); %获取位置
figure(1);
subplot(231)
imshow(x); %显示图像
title('测试原图')
f=x;
mainfc;
[a b c]=size(f);
for i=1:a
for j=1:b
if f(i,j)<50
F(i,j)=1;
else
F(i,j)=0;
end
end
end
subplot(232)
imshow(F);
title('定位图');
f=F;
f=bwareaopen(f,5000);
subplot(233)
imshow(f);
title('腐蚀');
se=strel('disk',5); %生成圆形结构元素
f=imdilate(f,se); %用生成的结构元素对图像进行腐蚀
subplot(234)
imshow(f);
title('膨胀');
se=strel('disk',6);
fc=imclose(f,se);%闭运算
fc=imfill(fc,'hole');%填洞
subplot(235),imshow(fc);
title('填洞')
%%%%定位
L = bwlabel(fc);
STATS = regionprops(L,'all');
%在bw图像上绘制出连通域的矩形框
subplot(236); imshow(x); title('定位图')
hold on
BW=fc;
[L,num] = bwlabel(BW); %标记
global boundary
%判别是否有口罩
msgbox('佩戴口罩');
for i = 1 : 1
boundary = STATS(i).BoundingBox;
rectangle('Position',boundary,'edgecolor','r' );
end