《基于MATLAB的人臉識別源程序(共16頁).doc》由會員分享,可在線閱讀,更多相關《基于MATLAB的人臉識別源程序(共16頁).doc(16頁珍藏版)》請在匯文網上搜索。
1、精選優(yōu)質文檔-傾情為你奉上1.色彩空間轉換function r,g=rgb_RGB(Ori_Face)R=Ori_Face(:,:,1);G=Ori_Face(:,:,2);B=Ori_Face(:,:,3);R1=im2double(R); % 將uint8型轉換成double型G1=im2double(G);B1=im2double(B);RGB=R1+G1+B1;row=size(Ori_Face,1); % 行像素column=size(Ori_Face,2); % 列像素for i=1:rowfor j=1:column rr(i,j)=R1(i,j)/RGB(i,j); gg(i,
2、j)=G1(i,j)/RGB(i,j);endendrrr=mean(rr);r=mean(rrr);ggg=mean(gg);g=mean(ggg);2.均值和協(xié)方差t1=imread(D:matlab皮膚庫1.jpg);r1,g1=rgb_RGB(t1);t2=imread(D:matlab皮膚庫2.jpg);r2,g2=rgb_RGB(t2);t3=imread(D:matlab皮膚庫3.jpg);r3,g3=rgb_RGB(t3);t4=imread(D:matlab皮膚庫4.jpg);r4,g4=rgb_RGB(t4);t5=imread(D:matlab皮膚庫5.jpg);r5,g
3、5=rgb_RGB(t5);t6=imread(D:matlab皮膚庫6.jpg);r6,g6=rgb_RGB(t6);t7=imread(D:matlab皮膚庫7.jpg);r7,g7=rgb_RGB(t7);t8=imread(D:matlab皮膚庫8.jpg);r8,g8=rgb_RGB(t8);t9=imread(D:matlab皮膚庫9.jpg);r9,g9=rgb_RGB(t9);t10=imread(D:matlab皮膚庫10.jpg);r10,g10=rgb_RGB(t10);t11=imread(D:matlab皮膚庫11.jpg);r11,g11=rgb_RGB(t11);
4、t12=imread(D:matlab皮膚庫12.jpg);r12,g12=rgb_RGB(t12);t13=imread(D:matlab皮膚庫13.jpg);r13,g13=rgb_RGB(t13);t14=imread(D:matlab皮膚庫14.jpg);r14,g14=rgb_RGB(t14);t15=imread(D:matlab皮膚庫15.jpg);r15,g15=rgb_RGB(t15);t16=imread(D:matlab皮膚庫16.jpg);r16,g16=rgb_RGB(t16);t17=imread(D:matlab皮膚庫17.jpg);r17,g17=rgb_RGB
5、(t17);t18=imread(D:matlab皮膚庫18.jpg);r18,g18=rgb_RGB(t18);t19=imread(D:matlab皮膚庫19.jpg);r19,g19=rgb_RGB(t19);t20=imread(D:matlab皮膚庫20.jpg);r20,g20=rgb_RGB(t20);t21=imread(D:matlab皮膚庫21.jpg);r21,g21=rgb_RGB(t21);t22=imread(D:matlab皮膚庫22.jpg);r22,g22=rgb_RGB(t22);t23=imread(D:matlab皮膚庫23.jpg);r23,g23=r
6、gb_RGB(t23);t24=imread(D:matlab皮膚庫24.jpg);r24,g24=rgb_RGB(t24);t25=imread(D:matlab皮膚庫25.jpg);r25,g25=rgb_RGB(t25);t26=imread(D:matlab皮膚庫26.jpg);r26,g26=rgb_RGB(t26);t27=imread(D:matlab皮膚庫27.jpg);r27,g27=rgb_RGB(t27);r=cat(1,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r13,r14,r15,r16,r17,r18,r19,r20,r21,r
7、22,r23,r24,r25,r26,r27);g=cat(1,g1,g2,g3,g4,g5,g6,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,xm
8、ean = xmean + j*bw(i,j);ymean = ymean + i*bw(i,j);end;end;if(area=0) xmean=0; ymean=0;else xmean = xmean/area; ymean = ymean/area; xmean = round(xmean); ymean = round(ymean);end4. 求偏轉角度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
9、 - xmean)2 * bw(i,j);b = b + (j - xmean) * (i - ymean) * bw(i,j);c = c + (i - ymean)2 * bw(i,j);end;end;b = 2 * b;theta = atan(b/(a-c)/2;theta = theta*(180/pi); % 從幅度轉換到角度5. 找區(qū)域邊界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
10、(A(i,j) = 0) left = j; break; end; end; if (left = -1) break; end;end;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 =
11、 i; break; end; end; if (down = -1) break; end;end;6. 求起始坐標function newcoord = checklimit(coord,maxval)newcoord = coord;if (newcoordmaxval) newcoord=maxval;end;7.模板匹配function ccorr, mfit, RectCoord = mobanpipei(mult, frontalmodel,ly,wx,cx, cy, angle)frontalmodel=rgb2gray(frontalmodel);model_rot = im
12、resize(frontalmodel,ly wx,bilinear); % 調整模板大小model_rot = imrotate(model_rot,angle,bilinear); % 旋轉模板l,r,u,d = bianjie(model_rot); % 求邊界坐標bwmodel_rot=imcrop(model_rot,l u (r-l) (d-u); % 選擇模板人臉區(qū)域modx,mody =center(bwmodel_rot); % 求質心morig, norig = size(bwmodel_rot);% 產生一個覆蓋了人臉模板的灰度圖像mfit = zeros(size(mu
13、lt);mfitbw = zeros(size(mult);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);endy = checklimit(endy,limy);for i=starty:endy,for j=startx:endx,mfit(i,j) = model_rot(i-starty+1,j-startx+1);end;end;ccorr = corr2(mfit,mult) % 計算相關度l,r,u,d = bianjie(bwmodel_rot);sx = startx+l;sy = starty+u;RectCoord = sx sy (r-1) (d-u); % 產生矩形坐標8.主程序clear;fname,pname=uigetfile(*.jpg;*.bmp;*.