%fuction norect=photo_selectclear(),
clear,close all;
iptsetpref('ImshowBorder','tight')
[filename,path]=uigetfile({'*.*','ALL Files'},'选择文件');
photo=imread([path,filename]);
%photogray=rgb2gray(filename);
figure,imshow(photo);%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%原图
% 灰度增强
% I=rgb2gray(photo);
% I=im2double(I);
% %I=2*I 对比度增强
%
% figure,subplot(1,2,1),imshow(I),title('灰度图像');
% subplot(1,2,2),imhist(I);
%
% ph=histeq(I);%均衡化
% figure,subplot(1,2,1),imshow(ph),title('灰度图像均衡化');
% subplot(1,2,2),imhist(ph);
%
% %平滑,中值滤波
% ph=medfilt2(ph,[5 5]);
% figure('name','中值滤波平滑'),imshow(ph);
photoYcbcr=rgb2ycbcr(photo);
py_y=photoYcbcr(:,:,1)%亮度
py_cr=photoYcbcr(:,:,3);%色度红
py_cb=photoYcbcr(:,:,2);%色度蓝
py_cr2=mat2gray(py_cr);
py_cb2=mat2gray(py_cb);%矩阵/图形
photoYcbcr2=cat(3,py_y,py_cb2,py_cr2);%link together
figure,subplot(2,3,1),imshow(photoYcbcr),title('Ycbcr空间图形');%rgb刚转换后的Ycbcr
subplot(2,3,2),imshow(py_y),title('y分量');
subplot(2,3,3),imshow(py_cr2),title('cr分量');
subplot(2,3,4),imshow(py_cb2),title('cb分量');
subplot(2,3,5),imshow(photoYcbcr2);
photoYcbcr=(py_cr>=138)&(py_cr<=173)&(py_cb>=100)&(py_cb<=127);%肤色范围
figure('name','给定肤色范围'),imshow(photoYcbcr);%%%%%%%%%%%%%%剪切融合(肤色范围)后的Ycbcr
photoYcbcr=double(photoYcbcr);
%中值滤波 平滑
photoYcbcr=medfilt2(photoYcbcr,[5 5]);
photoYcbcr=imfill(photoYcbcr,'holes');
figure('name','去噪,中值滤波'),imshow(photoYcbcr);%%%%%%中值滤波5*5
%高通滤波会出事
% lo=fspecial('laplacian');
% photoYcbcr=filter2(lo,photoYcbcr);
% figure('name','锐化,高通,log'),imshow(photoYcbcr);
%%%这次用圆形进行膨胀腐蚀,暂时性,之后对比矩阵像素
%%%%%腐蚀/膨胀 膨胀用单位矩阵,开操作也是
s=ones(8);%可以考虑圆形,菱形最好strel('square/disk/dimond',n)
photoYcbcr_big=imerode(photoYcbcr,s);
photoYcbcr_open=imopen(photoYcbcr,s);
photoYcbcr=imreconstruct(photoYcbcr_big,photoYcbcr);
photoYcbcr=imfill(photoYcbcr,'holes');
figure('name','腐蚀开运算,重点重构'),imshow(photoYcbcr);
stre=strel('disk',8);
photoYcbcr=imerode(photoYcbcr,stre);
photoYcbcr=imdilate(photoYcbcr,stre);
photoYcbcr=imfill(photoYcbcr,'holes');
figure('name','再次腐蚀+膨胀'),imshow(photoYcbcr);
%%%计算联通区域,根据连通域的标号计算面积
[arry_count,number]=bwlabeln(photoYcbcr,4);%计算低连通,高了不行
%imlabeln 竖行查找快
sum=0;
for i=1:number;
[row,column]=find(arry_count==i);
sub_row=max(row)-min(row);%row和column各自形成数组
sub_column=max(column)-min(column);
zone=sub_column*sub_row;%方形区域
zone_round=size(find(arry_count==i),1);%脸部区域
rat=zone_round/zone;
sum_row=size(row);sum=sum+sum_row(1);%sum是区域行的数量,因为bwlabeln函数的输出表示(单竖行),column也可以
%%%%%%%%zone_round,利用find计算每个像素的坐标矩阵,利用size计算矩阵第一个数值(表示每个像素所在的行数)的个数,其实
%%%%%%%%和size(row,1)是一样的
if(sub_row/sub_column<0.5)|(sub_row/sub_column>3)|rat<0.6
for j=1:sum_row(1)
arry_count(row(j),column(j))=0;
end
%else continue;
end
end
photoYcbcr_x=bwperim(arry_count,8);%logical,返回像素边缘的点的图像
photoYcbcr_x=uint8(photoYcbcr_x);
z=find(photoYcbcr_x(:)>0);
photoYcbcr_x(z)=255;
figure('name','人脸'),imshow(photoYcbcr);
photo_r=photoYcbcr_x;
photo_g=photoYcbcr_x;
photo_b=photoYcbcr_x;
photo_rgb=cat(3,photo_r,photo_g,photo_b);
figure('name','结果'),imshow(photo_rgb);
img_r=min(photo_r+photo(:,:,1),255);
img_g=min(photo_g+photo(:,:,2),255);
img_b=min(photo_b+photo(:,:,3),255);
img_photo=cat(3,img_r,img_g,img_b);
figure('name','结果'),imshow(img_photo);