您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 国内外标准规范 > 人脸识别MATLAB代码
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);%列像素fori=1:rowforj=1:columnrr(i,j)=R1(i,j)/RGB(i,j);gg(i,j)=G1(i,j)/RGB(i,j);endendrrr=mean(rr);r=mean(rrr);ggg=mean(gg);g=mean(ggg);2.均值和协方差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,g5]=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);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(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]=rgb_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,r22,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);[mn]=size(bw);bw=double(bw);xmean=0;ymean=0;fori=1:m,forj=1:n,xmean=xmean+j*bw(i,j);ymean=ymean+i*bw(i,j);end;end;if(area==0)xmean=0;ymean=0;elsexmean=xmean/area;ymean=ymean/area;xmean=round(xmean);ymean=round(ymean);end4.求偏转角度function[theta]=orient(bw,xmean,ymean)[mn]=size(bw);bw=double(bw);a=0;b=0;c=0;fori=1:m,forj=1:n,a=a+(j-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.找区域边界function[left,right,up,down]=bianjie(A)[mn]=size(A);left=-1;right=-1;up=-1;down=-1;forj=1:n,fori=1:m,if(A(i,j)~=0)left=j;break;end;end;if(left~=-1)break;end;end;forj=n:-1:1,fori=1:m,if(A(i,j)~=0)right=j;break;end;end;if(right~=-1)break;end;end;fori=1:m,forj=1:n,if(A(i,j)~=0)up=i;break;end;end;if(up~=-1)break;end;end;fori=m:-1:1,forj=1:n,if(A(i,j)~=0)down=i;break;end;end;if(down~=-1)break;end;end;6.求起始坐标functionnewcoord=checklimit(coord,maxval)newcoord=coord;if(newcoord1)newcoord=1;end;if(newcoordmaxval)newcoord=maxval;end;7.模板匹配function[ccorr,mfit,RectCoord]=mobanpipei(mult,frontalmodel,ly,wx,cx,cy,angle)frontalmodel=rgb2gray(frontalmodel);model_rot=imresize(frontalmodel,[lywx],'bilinear');%调整模板大小model_rot=imrotate(model_rot,angle,'bilinear');%旋转模板[l,r,u,d]=bianjie(model_rot);%求边界坐标bwmodel_rot=imcrop(model_rot,[lu(r-l)(d-u)]);%选择模板人脸区域[modx,mody]=center(bwmodel_rot);%求质心[morig,norig]=size(bwmodel_rot);%产生一个覆盖了人脸模板的灰度图像mfit=zeros(size(mult));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);fori=starty:endy,forj=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=[sxsy(r-1)(d-u)];%产生矩形坐标8.主程序clear;[fname,pname]=uigetfile({'*.jpg';'*.bmp';'*.tif';'*.gif'},'Pleasechooseacolorpicture...');%返回打开的图片名与图片路径名[u,v]=size(fname);y=fname(v);%图片格式代表值switchycase0errordlg('YouShouldLoadImageFileFirst...','Warning...');case{'g';'G';'p';'P';'f';'F'};%图片格式若是JPG/jpg、BMP/bmp、TIF/tif或者GIF/gif,才打开I=cat(2,pname,fname);Ori_Face=imread(I);subplot(2,3,1),imshow(Ori_Face);otherwiseerrordlg('YouShouldLoadImageFileFirst...','Warning...');endR=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;m=[0.4144,0.3174];%均值n=[0.0031,-0.0004;-0.0004,0.0003];%方差row=size(Ori_Face,1);%行像素数column=size(Ori_Face,2);%列像素数fori=1:rowforj=1:columnifRGB(i,j)==0rr(i,j)=0;gg(i,j)=0;elserr(i,j)=R1(i,j)/RGB(i,j);%rgb归一化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)');%皮肤概率服从高斯分布endendendsubplot(2,3,2);imshow(p);%显示皮肤灰度图像low_pa
本文标题:人脸识别MATLAB代码
链接地址:https://www.777doc.com/doc-5471604 .html