?? untitled.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); %認為眼睛區(qū)域高度占人臉的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; %選定鼻子區(qū)域高度為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.'); %顯示鼻子定位結(jié)果
%嘴角定位
nosey=max(nosey1,nosey2)+20;
hMouth=uint8(imSrcH*0.85); %認為mouth區(qū)域高度占人臉的0.6-0.9左右
hMouth=double(hMouth);
hMouth=nosey+30;
mouthfile=imgSrc(nosey:hMouth-10,eyemid-30:eyemid+40); %定出嘴巴角區(qū)域
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算子對嘴巴區(qū)域進行濾波
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.'); %顯示嘴角定位結(jié)果
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -