您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 能源与动力工程 > matlab实现人眼定位程序
%-------------------------------------------------------------------------clearallx=imread('C:\wuzun.jpg');%读取原始图像figure(1);subplot(1,4,1);imshow(x);title('原图像1');y=rgb2gray(x);subplot(1,4,2);imshow(y);title('图像1的灰度图');%图1灰度图u1=imnoise(y,'salt&pepper',0.07);subplot(1,4,3);imshow(u1);title('图像1加噪声图');%给图1加椒盐噪声zz=medfilt2(u1,[33]);z=medfilt2(zz,[55]);%(2次中值滤波)subplot(1,4,4);imshow(z);title('图像1中值滤波');%图1中值滤波(3*3窗口)%figure(6);subplot(1,2,1);imshow(zz);title('一次滤波');%subplot(1,2,2);imshow(z);title('二次滤波');x1=imread('C:\wuzun1.jpg');figure(2);subplot(1,4,1);imshow(x1);title('原图像2');y1=rgb2gray(x1);subplot(1,4,2);imshow(y1);title('图像2灰度图');%图2灰度图u2=imnoise(y1,'salt&pepper',0.13);subplot(1,4,3);imshow(u2);title('图像2叫噪声图');%图2加椒盐噪声zz1=medfilt2(u2,[33]);z1=medfilt2(zz1,[55]);%(2次中值滤波)subplot(1,4,4);imshow(z1);title('图像2滤波图');%图2中值滤波%figure(5);subplot(1,2,1);imshow(zz1),title('一次滤波');%subplot(1,2,2);imshow(z1);title('二次滤波');%figure(3);subplot(1,2,1);imshow(z);title('处理后图1');%figure(3);subplot(1,2,2);imshow(z1);title('处理后图2');f=imsubtract(z1,z);%图1图2差分figure(4);subplot(1,1,1);imshow(f);title('差分后图像');%agin=medfilt2(f,[33]);figure(10);imshow(agin);f=agin;%差分图中值滤波%-------------------------------------------------------------------------%-------------------------------------------------------------------------%眼睛的粗定位%标注一只眼睛[hw]=size(f);Amax=0;m=0;n=0;fori=10:hforj=10:wifAmax=f(i,j)Amax=f(i,j);m=i;n=j;endendend%标另外一只眼睛Bmax=0;m1=0;n1=0;fori1=10:m-10forj1=10:w-10ifBmax=f(i1,j1)Bmax=f(i1,j1);m1=i1;n1=j1;endendendfori2=m+10:h-10forj2=10:w-10ifBmax=f(i2,j2)Bmax=f(i2,j2);m1=i2;n1=j2;endendend%判断两眼的参数是否符合要求%+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++%----------------------------------------------------------------------figure(7);imshow(z);holdon;plot(n,m,'+',n1,m1,'+');title('标定眼睛图');%给眼睛画上矩形框figure(8);imshow(z),title('眼睛粗定位');holdon;Bvertex=[n-25m-15;n+25m+15];%矩形的左上顶点坐标和右下顶点坐标plot([Bvertex(1,1),Bvertex(2,1)],[Bvertex(1,2),Bvertex(1,2)],'r')plot([Bvertex(2,1),Bvertex(2,1)],[Bvertex(1,2),Bvertex(2,2)],'r')plot([Bvertex(2,1),Bvertex(1,1)],[Bvertex(2,2),Bvertex(2,2)],'r')plot([Bvertex(1,1),Bvertex(1,1)],[Bvertex(1,2),Bvertex(2,2)],'r');holdon;Bvertex=[n1-25m1-15;n1+25m1+15];%矩形的左上顶点坐标和右下顶点坐标plot([Bvertex(1,1),Bvertex(2,1)],[Bvertex(1,2),Bvertex(1,2)],'r')plot([Bvertex(2,1),Bvertex(2,1)],[Bvertex(1,2),Bvertex(2,2)],'r')plot([Bvertex(2,1),Bvertex(1,1)],[Bvertex(2,2),Bvertex(2,2)],'r')plot([Bvertex(1,1),Bvertex(1,1)],[Bvertex(1,2),Bvertex(2,2)],'r');%----------------------------------------------------------------------%------------------------------------------------------------------------%最大类间方差定二值化阈值[height,width]=size(z);hist=zeros(256,1);%直方图%threshold=128;%初始阈值%计算直方图fori=1:heightforj=1:widthm=z(i,j)+1;%zz(i,j)灰度值从0-255hist(m)=hist(m)+1;%某级灰度的像素数endendhist=hist/(height*width);%落在每一灰度级上的概率avg=0;form=1:256avg=avg+(m-1)*hist(m);endtemp=0;fori=1:256p1=0;avg1=0;avg2=0;T_current=i-1;%当前分割阈值form=1:T_current-1p1=hist(m)+p1;%低灰度级概率总和endp2=1-p1;%高灰度级概率总和form=1:256ifmT_currentavg1=avg1+(m-1)*hist(m);elseavg2=avg2+(m-1)*hist(m);endendavg1=avg1/p1;avg2=avg2/p2;D=p1*(avg1-avg)^2+p2*(avg2-avg)^2;ifD=tempfinalT=T_current;temp=D;endend%滤波后差分图二值化f1=z;fori=1:hforj=1:wiff1(i,j)=finalTf1(i,j)=0;elsef1(i,j)=255;endendendfigure(13);imshow(f1);title('差分后图二值化');%------------------------------------------------------------------------%------------------------------------------------------------------------%提取眼睛轮廓%精确定位眼睛矩形,最高点、最低点、最左边、左右边四点确定的矩形。%一只眼轮廓[h1,w1]=size(f1);zg=0;zd=0;zzb=0;zyb=0;%i3=0;j3=0;xj=50;yj=30;%垂直和水平方向中心点矩形大小一半x3=zeros(1,100);y3=zeros(1,60);x4=zeros(1,100);y4=zeros(1,60);x5=zeros(1,100);y5=zeros(1,60);x6=zeros(1,100);y6=zeros(1,60);zgx=0;zdx=0;zzbx=0;zybx=0;zgy=0;zdy=0;zzby=0;zyby=0;%最高的点yfori3=n-50:n+50forj3=m-30:m+30iff1(i3,j3)==255&f1(i3,j3+1)==0x3(i3-n+51)=i3;y3(j3-m+31)=j3;endendendfork=1:1:100ifzgyy3zgy=y3;endend%最低点yforj3=m-30:m+30fori3=n-50:n+50iff1(i3,j3)==255&f1(i3,j3-1)==0x4(i3-n+51)=i3;y4(j3-m+31)=j3;endendendfork=1:1:100ifzdyy4zdy=y4-1;endend%最左点xforj3=m-30:m+30fori3=n+50:1:n-50iff1(i3,j3)==255&f1(i3+1,j3)==0x5(i3-n+51)=i3;y5(j3-m+31)=j3;endendendfork=1:1:100ifzzbxx5zzbx=x5+1;endend%最右点xforj3=m-30:m+30fori3=n+50:1:n-50iff1(i3,j3)==255&f1(i3-1,j3)==0x6(i3-n+51)=i3;y6(j3-m+31)=j3;endendendfork=1:1:100ifzybxx6zybx=x6-1;endend%精确画出眼睛矩形区域figure(9);imshow(f1),title('眼睛轮廓');holdon;Bvertex=[215163;241175];%矩形的左上顶点坐标和右下顶点坐标plot([Bvertex(1,1),Bvertex(2,1)],[Bvertex(1,2),Bvertex(1,2)],'r')plot([Bvertex(2,1),Bvertex(2,1)],[Bvertex(1,2),Bvertex(2,2)],'r')plot([Bvertex(2,1),Bvertex(1,1)],[Bvertex(2,2),Bvertex(2,2)],'r')plot([Bvertex(1,1),Bvertex(1,1)],[Bvertex(1,2),Bvertex(2,2)],'r');%---------------------------------------------------------------------------%另一只眼轮廓holdon;Bvertex=[139154;170167];%矩形的左上顶点坐标和右下顶点坐标plot([Bvertex(1,1),Bvertex(2,1)],[Bvertex(1,2),Bvertex(1,2)],'r')plot([Bvertex(2,1),Bvertex(2,1)],[Bvertex(1,2),Bvertex(2,2)],'r')plot([Bvertex(2,1),Bvertex(1,1)],[Bvertex(2,2),Bvertex(2,2)]
本文标题:matlab实现人眼定位程序
链接地址:https://www.777doc.com/doc-7455380 .html