基于MATLAB的人臉識別源程序_第1頁
基于MATLAB的人臉識別源程序_第2頁
基于MATLAB的人臉識別源程序_第3頁
基于MATLAB的人臉識別源程序_第4頁
基于MATLAB的人臉識別源程序_第5頁
已閱讀5頁,還剩28頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、色彩空間轉換1.function r,g=rgb_RGB(Ori_Face)R=Ori_Face(:,:,1);G=Ori_Face(:,:,2);B=Ori_Face(:,:,3);將型轉換成型double uint8 %R1=im2double(R);G1=im2double(G);B1=im2double(B);RGB=R1+G1+B1;行像素 row=size(Ori_Face,1);%列像素 %column=size(Ori_Face,2);for i=1:row for j=1:columnrr(i,j)=R1(i,j)/RGB(i,j);gg(i,j)=G1(i,j)/RGB(i

2、,j);endendrrr=mean(rr);r=mean(rrr);ggg=mean(gg);g=mean(ggg);均值和協(xié)方差2.學資學習網提供考研資料皮膚庫學資學習網提供考研資料皮膚庫皮膚庫皮膚庫皮膚庫皮膚庫皮膚庫皮膚庫皮膚庫皮膚庫皮膚庫皮膚庫皮膚庫皮膚庫皮膚庫皮膚庫皮膚庫皮膚庫皮膚庫皮膚庫皮膚庫皮膚庫皮膚庫皮膚庫皮膚庫皮膚庫r=cat(1,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r13,r14,r15,r16,r17,r18,r19,r20,r21,r22,r23,r24,r25,r26,r27);g=cat(1,g1,g2,g3,g4,g5,g

3、6,g7,g8,g9,g10,g11,g12,g13,g14,g15,g16,g17,g18,g19,g20,g21,g22,g23,g24,g25,g26,g27);m=mean(r,g)n=cov(r,g)求質心 3.function xmean, ymean = center(bw) bw=bwfill(bw,'holes');area= bwarea(bw);m n =size(bw);bw=double(bw);xmean=0; ymean = 0;for i=1:m,for j=1:n,xmean= xmean+ j*bw(i,j);i*bw(i,j); + yme

4、an= ymean end;end;if(area=0) xmean=0;ymean=0;else xmean= xmean/area;ymean= ymean/area;xmean= round(xmean);ymean= round(ymean);end求偏轉角度4.function theta = orient(bw,xmean,ymean)m n =size(bw);bw=double(bw);a= 0;b= 0;c= 0;for i=1:m,for j=1:n,a = a + (j - xmean)八2* bw(i,j);bw(i,j); * ymean) - (i * xmean)

5、 - (j + b = b c = c + (i - ymean)八2* bw(i,j);end;end;b=2* b;theta= atan(b/(a-c)/2;從幅度轉換到角度% = theta*(180/pi);theta找區(qū)域邊界5.function left, right, up, down = bianjie(A)m n = size(A);left = -1;right = -1;up= -1;down = -1;for j=1:n, for i=1:m,if (A(i,j) =0)left = j;break;end;end;if (left = -1) break;end;e

6、nd;for j=n:-1:1,學資學習網提供考研資料for i=1:m,學資學習網提供考研資料if (A(i,j) =0)right =j;break;end;end;if (right = -1) break;end;end;for i=1:m,for j=1:n,if (A(i,j) =0) up=i;break;end;end;if (up = -1)break;end;end;for i=m:-1:1,for j=1:n,if (A(i,j) =0)down = i;break;end;end;if (down = -1) break;end;end;求起始坐標6.function

7、newcoord = checklimit(coord,maxval) coord; newcoord =(newcoord<1) if newcoord=1;end;(newcoord>maxval)ifnewcoord=maxval;end;模板匹配7.mobanpipei(mult,=RectCoordmfit,ccorr,functionfrontalmodel,ly,wx,cx, cy, angle)frontalmodel=rgb2gray(frontalmodel);調整模板大小%wx,'bilinear'); model_rot =imresize(

8、frontalmodel,ly旋轉模板% imrotate(model_rot,angle,'bilinear');model_rot =求邊界坐標% bianjie(model_rot);l,r,u,d =選擇模板人臉bwmodel_rot=imcrop(model_rot,l u (r-l) (d-u);%區(qū)域求質心 =center(bwmodel_rot);%modx,modymorig, norig = size(bwmodel_rot);產生一個覆蓋了人臉模板的灰度圖像%mfit = zeros(size(mult);mfitbw = zeros(size(mult)

9、;limy, limx = size(mfit);計算原圖像中人臉模板的坐標%startx = cx-modx;starty = cy-mody;endx = startx + norig-1;endy = starty + morig-1;startx = checklimit(startx,limx);starty = checklimit(starty,limy);endx = checklimit(endx,limx);checklimit(endy,limy); = endyfor i=starty:endy,for j=startx:endx,mfit(i,j) = model_r

10、ot(i-starty+1,j-startx+1);end;end;計算相關度corr2(mfit,mult)%ccorr =l,r,u,d = bianjie(bwmodel_rot);sx= startx+l;sy= starty+u;產生矩形坐標% (d-u); sx sy(r-1)RectCoord =主程序 8.clear;fname,pname=uigetfile('*.jpg''*.bmp''*.tif''*.gif','Pleasechoosea返回打開的圖片名與圖片路徑名picture.');%c

11、oloru,v=size(fname);y=fname(v);%switch ycase0errordlg('You Should Load ImageFile First.','Warning.');圖片格式若是%case'g''G''p''P''f''F'、 、或者,才打開GIF/gif JPG/jpgBMP/bmpTIF/tifI=cat(2,pname,fname);Ori_Face=imread(I);subplot(2,3,1),imshow(Ori_Fa

12、ce);otherwiseerrordlg('You Should Load ImageFile First.','Warning.');endR=Ori_Face(:,:,1);G=Ori_Face(:,:,2);B=Ori_Face(:,:,3);將型轉換成型處理double uint8 %R1=im2double(R);G1=im2double(G);B1=im2double(B);RGB=R1+G1+B1;均值 0.4144,0.3174;%m=方差 n=0.0031,-0.0004;-0.0004,0.0003;%行像素數(shù)%row=size(Ori_F

13、ace,1);列像素數(shù)column=size(Ori_Face,2);% for i=1:rowfor j=1:columnif RGB(i,j)=0rr(i,j)=0;gg(i,j)=0;else歸一化 rgbrr(i,j)=R1(i,j)/RGB(i,j); %gg(i,j)=G1(i,j)/RGB(i,j);x=rr(i,j),gg(i,j);皮膚概率服從高斯分p(i,j)=exp(-0.5)*(x-m)*inv(n)*(x-m)');%布endend end顯示皮膚灰度圖像subplot(2,3,2);imshow(p);% low_pass=1/9*ones(3);低通濾波去

14、噪聲%image_low=filter2(low_pass, p);subplot(2,3,3);imshow(image_low);previousSkin2 = zeros(i,j);changelist= ;for threshold= 0.55:-0.1:0.05 two_value = zeros(i,j);two_value(find(image_low>threshold) = 1;change= sum(sum(two_value- previousSkin2);changelist= changelist change;previousSkin2 = two_value

15、;endmin(changelist); = I C,optimalThreshold = (7-I)*0.1two_value = zeros(i,j);二值化 %1; two_value(find(image_low>optimalThreshold) =顯示二值圖像%subplot(2,3,4);imshow(two_value);讀入人臉模板我的照片人臉模板.jpg');%照片F(xiàn)aceCoord=;將原照片轉換為灰度imsourcegray=rgb2gray(Ori_Face);%圖像標注二值圖像中連接的部分為數(shù),LL,N=bwlabel(two_value,8);%據矩

16、陣,為顆粒的個數(shù)Nfor i=1:N,尋找矩陣中標號為的i %x,y=find(bwlabel(two_value)=i);行和列的下標選擇出第個顆粒% bwselect(two_value,y,x,8);ibwsegment=計算此區(qū)域的空洞數(shù)numholes= 1-bweuler(bwsegment,4);%若此區(qū)域至少包含一個洞,則將其選出% 1) if (numholes>=進行下一步運算RectCoord= -1;m n = size(bwsegment);求此區(qū)域的質心%cx,cy=center(bwsegment);將洞封住(將灰度值賦bwnohole=bwfill(bws

17、egment,'holes');%為) 1justface = uint8(double(bwnohole) .* double(imsourcegray);只在原照片的灰度圖像中保留該候選區(qū)%域求此區(qū)域的偏轉角度% orient(bwsegment,cx,cy);angle=bw = imrotate(bwsegment,angle,'bilinear');bw = bwfill(bw,'holes');l,r,u,d =bianjie(bw);寬度+1);% l wx = (r -高度ly = (d - u +% 1);高寬比 wratio

18、= ly/wx%if (0.8<=wratio)&(wratio<=2)如果目標區(qū)域的高度寬度比例大于且小于,則將2.0 0.8%/其選出進行下一步運算計算包含此區(qū)域矩形的面積%S=ly*wx;計算此區(qū)域面積A=bwarea(bwsegment);%if (A/S>0.35)ccorr,mfit, RectCoord = mobanpipei(justface,frontalmodel,ly,wx, cx,cy, angle);end(ccorr>=0.6) ifmfitbw=(mfit>=1);invbw = xor(mfitbw,ones(size(m

19、fitbw);source_with_hole=uint8(double(invbw).*double(imsourcegray);final_image = uint8(double(source_with_hole) + double(mfit);顯示覆蓋了模板臉的%subplot(2,3,5);imshow(final_image);灰度圖像imsourcegray= final_image;顯示檢測效果圖%subplot(2,3,6);imshow(Ori_Face);end;if (RectCoord= -1)FaceCoord= FaceCoord; RectCoord;end end end end在認為是人臉的區(qū)域畫矩形%numfacesx = size(FaceCoord);for i=1:numfaces, hd = rectangle('Position',FaceCoord(i,:);set(hd,'edgecolor','y');end人臉檢測是人

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論