⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 untitled.m

📁 利用人脸的灰度值的不同可以高效识别人脸区域以及定位的功能
💻 M
字号:
clear;
%读入图象
imgSrc=imread('02.bmp');  
[imSrcH,imSrcW]=size(imgSrc);
subplot(2,2,1);                                          %画出原图像
imshow(imgSrc(10:imSrcH-10,5:imSrcW-5));                   %剪切图像
%眼睛定位
imMidW=imSrcW/2;
hEye=uint8(imSrcH*0.4);            %认为眼睛区域高度占人脸的0.4左右
hEye=double(hEye);
imgleft=imgSrc(10:hEye,5:imMidW);
imgright=imgSrc(10:hEye,imMidW+5:imSrcW);
[leftH,leftW]=size(imgleft);
[leftebIndex,lefteyeIndex,leftb,leftc,leftminW,leftmaxW]=position(imgleft); 
%左眼定位
[rightebIndex,righteyeIndex,rightb,rightc,rightminW,rightmaxW]=position(imgright); %右眼定位
lefty1=leftb(1)-18+max(leftebIndex)+max(leftebIndex)-8;
lefty2=leftc(1)-18+max(leftebIndex)+max(leftebIndex)-8;
righty1=rightb(1)-18+max(rightebIndex)+max(rightebIndex)-8;
righty2=rightc(1)-18+max(rightebIndex)+max(rightebIndex)-8;
subplot(2,2,1);
hold on
plot(lefteyeIndex,max(leftebIndex),'r.');
subplot(2,2,1);
hold on
plot(imMidW+righteyeIndex,max(rightebIndex),'r.');          %眼睛位置显示
hold on
plot([leftminW+lefteyeIndex-22+2 leftmaxW+lefteyeIndex-22+2],[lefty1+3 lefty2+3],'r.');
hold on
plot([imMidW+rightminW+righteyeIndex-15+2 imMidW+rightmaxW+righteyeIndex-15+2],[righty1+3 righty2+3],'r.');
%鼻孔定位
eyey=max(max(leftebIndex),max(rightebIndex))+25;
eyemid=uint8((lefteyeIndex+imMidW+righteyeIndex)/2);
eyemid=double(eyemid);
hNose=eyey+40;                              %选定鼻子区域高度为40
leftnosefile=imgSrc(eyey:hNose-8,5:eyemid);
subplot(2,2,3);
imshow(leftnosefile);
leftnoseyvector = mean(leftnosefile,2);
leftnoseyvector = leftnoseyvector/max(leftnoseyvector);             %归一化
leftnoseyvector=vectorsmooth(leftnoseyvector,3);                %向量平滑
leftnoseyvector=leftnoseyvector/max(leftnoseyvector);
[leftyval,leftyIndex]=findmins(leftnoseyvector,1);                         %寻找谷值
leftnosexvector = mean(leftnosefile);
leftnosexvector=leftnosexvector/max(leftnosexvector);
leftnosexvector=vectorsmooth(leftnosexvector,3);
leftnosexvector=leftnosexvector/max(leftnosexvector);
[leftxVal,leftxIndex]=findmins(leftnosexvector,2);
nosex1=max(leftxIndex);
nosey1=max(leftyIndex)+eyey-10;
rightnosefile=imgSrc(eyey:hNose-8,eyemid:imSrcW-8);
subplot(2,2,4);
imshow(rightnosefile);
rightnoseyvector = mean(rightnosefile,2);
rightnoseyvector = rightnoseyvector/max(rightnoseyvector);
rightyVector=vectorsmooth(rightnoseyvector,3);
rightyVector=rightyVector/max(rightyVector);
[rightyval,rightyIndex]=findmins(rightyVector,1);
rightnosexvector = mean(rightnosefile);
rightnosexvector=rightnosexvector/max(rightnosexvector);
rightnosexvector=vectorsmooth(rightnosexvector,3);
rightnosexvector=rightnosexvector/max(rightnosexvector);
[rightxVal,rightxIndex]=findmins(rightnosexvector,2);
nosex2=eyemid+min(rightxIndex);
nosey2=min(rightyIndex)+eyey-10;
subplot(221);
hold on
plot([nosex1 nosex2],[nosey1 nosey2],'r.');              %显示鼻子定位结果
%嘴角定位
nosey=max(nosey1,nosey2)+20;
hMouth=uint8(imSrcH*0.85);  %认为mouth区域高度占人脸的0.6-0.9左右
hMouth=double(hMouth);
hMouth=nosey+30;
mouthfile=imgSrc(nosey:hMouth-10,eyemid-30:eyemid+40);   %定出嘴巴角区域
subplot(2,2,2);
imshow(mouthfile);
%suan算子法定为嘴角
[MHeight,MWidth]=size(mouthfile);
MouthIntegral = sum(mouthfile,2);
MouthIntegral = MouthIntegral/max(MouthIntegral);
MouthIntegral=vectorsmooth(MouthIntegral,3);
MouthIntegral=MouthIntegral/max(MouthIntegral);
[mVal,mIndex]=findmins(MouthIntegral,1);
Copy=zeros(MHeight,MWidth);
%SUSAN算子对嘴巴区域进行滤波
c=0;
for i=4:MHeight-3
    for j=4:MWidth-3
        for m=-3:3
            for n=-3:3
                if (abs(m)+abs(n)<=4)
if abs(double(mouthfile(i+m,j+n))-double(mouthfile(i,j)))<=20
c=c+exp(-double((double(mouthfile(i+m,j+n))-double(mouthfile(i,j)))/15)^6);
                    end
                end
            end
        end
        Copy(i,j)=c;
        c=0;
    end
end
g=0.58*max(max(Copy));
for i=1:MHeight-6
    for j=1:MWidth-6
        if Copy(i+3,j+3)<g
            Copy1(i,j)=1;
        else Copy1(i,j)=0;
        end
    end
end
[eyeH,eyeW]=find(Copy1==1);                %Copy为滤波后的二值图像
mouthminW=min(eyeW);
mouthmaxW=max(eyeW);
minP=find(eyeW==mouthminW);
maxP=find(eyeW==mouthmaxW);
mouthx1=mouthminW+eyemid-30;
mouthx2=mouthmaxW+eyemid-30;
mouthb=sort(eyeH(minP)+20-max(mIndex));
mouthc=sort(eyeH(maxP)+20-max(mIndex));
mouthy1=mouthb(1)-20+max(mIndex)+nosey-5;         
mouthy2=mouthc(1)-20+max(mIndex)+nosey-5;
subplot(2,2,1);
hold on
plot( [mouthx1 mouthx2],[mouthy1 mouthy2] ,'r.');       %显示嘴角定位结果

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -