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:row

for j=1:column

rr(i,j)=R1(i,j)/RGB(i,j);

gg(i,j)=G1(i,j)/RGB(i,j);

end

end

rrr=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);

[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);

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);

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);

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)

[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;

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 = i;

break;

end;

end;

if (down ~= -1)

break;

end;

end;

6.求起始坐标

function newcoord = checklimit(coord,maxval)

newcoord = coord;

if (newcoord<1)

newcoord=1;

end;

if (newcoord>maxval)

newcoord=maxval;

end;

7.模板匹配

function [ccorr, mfit, RectCoord] = mobanpipei(mult, frontalmodel,ly,wx,cx, cy, angle)

frontalmodel=rgb2gray(frontalmodel);

model_rot = imresize(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)]); %选择模板人脸区域

[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);

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';'*.tif';'*.gif'},'Please choose a color picture...'); %返回打开的图片名与图片路径名

[u,v]=size(fname);

y=fname(v); %图片格式代表值

switch y

case 0

errordlg('You Should Load Image File First...','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);

otherwise

errordlg('You Should Load Image File First...','Warning...');

end

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;

m=[ 0.4144,0.3174]; %均值

n=[0.0031,-0.0004;-0.0004,0.0003]; %方差

row=size(Ori_Face,1); %行像素数

column=size(Ori_Face,2); %列像素数

for i=1:row

for j=1:column

if RGB(i,j)==0

rr(i,j)=0;gg(i,j)=0;

else

rr(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)'); %皮肤概率服从高斯分布

end

end

end

subplot(2,3,2);imshow(p); %显示皮肤灰度图像

low_pass=1/9*ones(3);

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;

end

[C, I] = min(changelist);

optimalThreshold = (7-I)*0.1

two_value = zeros(i,j);

two_value(find(image_low>optimalThreshold)) = 1; %二值化

subplot(2,3,4);imshow(two_value); %显示二值图像

frontalmodel=imread('E:\我的照片\人脸模板.jpg'); %读入人脸模板照片

FaceCoord=[];

imsourcegray=rgb2gray(Ori_Face); %将原照片转换为灰度图像

[L,N]=bwlabel(two_value,8); %标注二值图像中连接的部分,L为数据矩阵,N为颗粒的个数

for i=1:N,

[x,y]=find(bwlabel(two_value)==i); %寻找矩阵中标号为i的行和列的下标

bwsegment = bwselect(two_value,y,x,8); %选择出第i个颗粒

numholes = 1-bweuler(bwsegment,4); %计算此区域的空洞数

if (numholes >= 1) %若此区域至少包含一个洞,则将其选出进行下一步运算

RectCoord = -1;

[m n] = size(bwsegment);

[cx,cy]=center(bwsegment); %求此区域的质心

bwnohole=bwfill(bwsegment,'holes'); %将洞封住(将灰度值赋为1)

justface = uint8(double(bwnohole) .* double(imsourcegray));

%只在原照片的灰度图像中保留该候选区域

angle = orient(bwsegment,cx,cy); %求此区域的偏转角度

bw = imrotate(bwsegment, angle, 'bilinear');

bw = bwfill(bw,'holes');

[l,r,u,d] =bianjie(bw);

wx = (r - l +1); %宽度

ly = (d - u + 1); %高度

wratio = ly/wx %高宽比

if ((0.8<=wratio)&(wratio<=2))

%如果目标区域的高度/宽度比例大于0.8且小于2.0,则将其选出进行下一步运算

S=ly*wx; %计算包含此区域矩形的面积

A=bwarea(bwsegment); %计算此区域面积

if (A/S>0.35)

[ccorr,mfit, RectCoord] = mobanpipei(justface,frontalmodel,ly,wx, cx,cy, angle);

end

if (ccorr>=0.6)

mfitbw=(mfit>=1);

invbw = xor(mfitbw,ones(size(mfitbw)));

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

%在认为是人脸的区域画矩形

[numfaces x] = size(FaceCoord);

for i=1:numfaces,

hd = rectangle('Position',FaceCoord(i,:));

set(hd, 'edgecolor', 'y');

end

怎么用matlab画一个笑脸,MATLAB笑脸识别相关推荐

  1. matlab画一个放大图中图

    matlab画一个放大图中图 1 magnify介绍 2 具体思路 3 具体步骤 4 参考 1 magnify介绍 magnify是个动态放大镜,固化后可以用tools>edit plot移动小 ...

  2. matlab如何画一个圆柱,matlab怎么画圆柱

    用Matlab 画函数图像一.螺旋线 1.静态螺旋线 a=0:0.1:20*pi... 例 在区间[0,2π]画sin(x),并分别标注"sin(x)""cos(x)&q ...

  3. matlab画一个树,搞气氛!用MATLAB画一棵Bling Bling的圣诞树

    0.前言 马上圣诞节了,今天推一篇用MATLAB画圣诞树的,效果如下图所示: 1.准备工作 因为这次用的是MATLAB,不像PYTHON一样需要装一些依赖库,要实现本文的效果,只需安装MATLAB即可 ...

  4. matlab 画一个矩形

    画一个矩形 %rectangle('Position',[x,y,w,h],'PropertyName',propertyvalue) %axis([xmin,xmax,ymin,ymax]) clc ...

  5. 如何用Matlab画一个数学动态GIF

    先放一张三维球体旋转图形效果图 该图形旋转主要用到了圆的参数方程和空间几何变换矩阵. 1. 画出球形 为了画出一个圆形,圆形的参数方程为: y=y0+r×cos(θ) y = y 0 + r × c ...

  6. 怎样用MATLAB画二次函数曲线,matlab画二次函数图像

    [8 70 118 100 9 0 5]; 以上是每一个 X 和 Y 对应的坐标,请问如何编程能够绘制平滑曲线,这个图形就像二次函数一样的 如果要在图中绘制一条直线加上 y=...... MATLAB ...

  7. matlab画动漫,matlab绘制gif

    想自己画一个卷积动态生成的图便于理解,但是没有找到具体如何使用matlab来绘制gif的教程,还是得自己动手看官方文档啊! 1. animatedline 介绍: an = animatedline ...

  8. matlab画圆柱,使用Matlab画出圆台圆锥圆柱

    <使用Matlab画出圆台圆锥圆柱>由会员分享,可在线阅读,更多相关<使用Matlab画出圆台圆锥圆柱(5页珍藏版)>请在人人文库网上搜索. 1.用Matlab画圆柱圆台圆锥自 ...

  9. 期货策略matlab,code 一个利用MATLAB编写的螺纹钢期货高频交易套利策略 联合开发网 - pudn.com...

    code 所属分类:金融证券系统 开发工具:matlab 文件大小:506KB 下载次数:398 上传日期:2013-10-09 14:14:53 上 传 者:huangxiao 说明:  一个利用M ...

  10. matlab创建一个函数,matlab定义一个新函数

    本文收集整理关于matlab定义一个新函数的相关议题,使用内容导航快速到达. 内容导航: Q1:matlab怎么建立自定义函数 自定义函数的格式: function 函数名(参数) 函数表达式 end ...

最新文章

  1. HDU1040简单排序题
  2. 人脸检测--Grid Loss: Detecting Occluded Faces
  3. 游标式屏幕菜单c语言,【C语言】控制台窗口图形界面编程(六):光标设置
  4. MyBatis子查询
  5. step4 . day2标准IO和文件IO 小测试demo
  6. 程序员你知道被迫参与一个两亿的项目,想跑还逃不掉吗?
  7. pytorch GPU加速运算
  8. 计算机一级ftp协议传输,文件传输协议(FTP)必将消亡
  9. catkin_make:Project ‘cv_bridge‘ specifies ‘/usr/include/opencv‘ as an include dir, which is not fo
  10. python 倒计时功能怎么用print实现_在python中的print语句中实现60秒倒计时
  11. 通过描述系统的微分方程,判断系统是否为线性系统以及是定常系统还是时变系统
  12. Flutter 本地数据库sqflite实战操作
  13. Java基础案例教程———【任务4-2】模拟物流快递系统
  14. mysql dos 怎样卸载_MySQL安装与卸载
  15. redis基础命令和数据操作命令学习笔记
  16. LaTex练习日记02 —— 字体设置
  17. SVG互动排版公众号图文 『两次物体移动与展开长图』 模板代码
  18. 安卓图片分类浏览器php,Android快速实现图片浏览
  19. android ebusy,在Android中使用pjsip发送INVITE时出错:初始化媒体通道出错(PJ_EBUSY)...
  20. 计算相似度评价值体系:欧几里德距离和皮尔逊相关度

热门文章

  1. 一个小技巧告诉你,邮箱域名地址格式怎么选择?
  2. 美国大学生数学建模-足球传递网络图
  3. maximum-subarray[最大连续子序列]
  4. App上架时,华为应用市场提示:在测试环境:Wi-Fi联网、EMUI11.0 ( P40),软件存在闪退。如何模拟EMUI11.0 ( P40)
  5. AutoCAD如何方便截图放到Word文档,改成白底黑字
  6. JavaBase 求 个位,十位,百位,千位
  7. OJ每日一练——乘方计算
  8. mac硬盘故障升级系统_硬件升级:如何安装新硬盘,第2页,故障排除
  9. iOS二维码限制区域识别、扫描动画
  10. Ninja ripper 工具使用教程