您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 管理学资料 > 照片人脸检测MATLAB代码
%载入图像Img=imread('star1.jpg');ifndims(Img)==3I=rgb2gray(Img);elseI=Img;endBW=im2bw(I,graythresh(I));%二值化figure;subplot(2,2,1);imshow(Img);title('原图像','FontWeight','Bold');subplot(2,2,2);imshow(Img);title('网格标记图像','FontWeight','Bold');holdon;[xt,yt]=meshgrid(round(linspace(1,size(I,1),10)),...round(linspace(1,size(I,2),10)));mesh(yt,xt,zeros(size(xt)),'FaceColor',...'None','LineWidth',3,...'EdgeColor','r');subplot(2,2,3);imshow(BW);title('二值图像','FontWeight','Bold');[n1,n2]=size(BW);r=floor(n1/10);%分成10块,行c=floor(n2/10);%分成10块,列x1=1;x2=r;%对应行初始化s=r*c;%块面积fori=1:10y1=1;y2=c;%对应列初始化forj=1:10if(y2=c||y2=9*c)||(x1==1||x2==r*10)%如果是在四周区域loc=find(BW(x1:x2,y1:y2)==0);[p,q]=size(loc);pr=p/s*100;%黑色像素所占的比例数ifpr=100BW(x1:x2,y1:y2)=0;endendy1=y1+c;%列跳跃y2=y2+c;%列跳跃endx1=x1+r;%行跳跃x2=x2+r;%行跳跃end[L,num]=bwlabel(BW,8);%区域标记stats=regionprops(L,'BoundingBox');%得到包围矩形框Bd=cat(1,stats.BoundingBox);[s1,s2]=size(Bd);mx=0;fork=1:s1p=Bd(k,3)*Bd(k,4);%宽*高ifpmx&&(Bd(k,3)/Bd(k,4))1.8%如果满足面积块大,而且宽/高1.8mx=p;j=k;endendsubplot(2,2,4);imshow(I);holdon;rectangle('Position',Bd(j,:),...'EdgeColor','r','LineWidth',3);title('标记图像','FontWeight','Bold');ifndims(Img)==3I=rgb2gray(Img);elseI=Img;endBW=im2bw(I,graythresh(I));%二值化figure;subplot(2,2,1);imshow(Img);title('原图像','FontWeight','Bold');subplot(2,2,2);imshow(Img);title('网格标记图像','FontWeight','Bold');holdon;[xt,yt]=meshgrid(round(linspace(1,size(I,1),10)),...round(linspace(1,size(I,2),10)));mesh(yt,xt,zeros(size(xt)),'FaceColor',...'None','LineWidth',3,...'EdgeColor','r');subplot(2,2,3);imshow(BW);title('二值图像','FontWeight','Bold');[n1,n2]=size(BW);r=floor(n1/10);%分成10块,行c=floor(n2/10);%分成10块,列x1=1;x2=r;%对应行初始化s=r*c;%块面积fori=1:10y1=1;y2=c;%对应列初始化forj=1:10if(y2=c||y2=9*c)||(x1==1||x2==r*10)%如果是在四周区域loc=find(BW(x1:x2,y1:y2)==0);[p,q]=size(loc);pr=p/s*100;%黑色像素所占的比例数ifpr=100BW(x1:x2,y1:y2)=0;endendy1=y1+c;%列跳跃y2=y2+c;%列跳跃endx1=x1+r;%行跳跃x2=x2+r;%行跳跃end[L,num]=bwlabel(BW,8);%区域标记stats=regionprops(L,'BoundingBox');%得到包围矩形框Bd=cat(1,stats.BoundingBox);[s1,s2]=size(Bd);mx=0;fork=1:s1p=Bd(k,3)*Bd(k,4);%宽*高ifpmx&&(Bd(k,3)/Bd(k,4))1.8%如果满足面积块大,而且宽/高1.8mx=p;j=k;endendsubplot(2,2,4);imshow(I);holdon;rectangle('Position',Bd(j,:),...'EdgeColor','r','LineWidth',3);title('标记图像','FontWeight','Bold');
本文标题:照片人脸检测MATLAB代码
链接地址:https://www.777doc.com/doc-1390634 .html