您好,欢迎访问三七文档
clc;clear;hand_M=[];%定义训练集的特征向量T=zeros(10,50);%定义训练集的目标向量forii=1:10%手势类别循环forjj=1:5%手势个数循环T(ii,(ii-1)*5+jj)=1;%str=strcat('D:\ibmd盘\学习\研究生\任务\0323手势\训练集\',num2str(ii),'_',num2str(jj),'.bmp');%定义文件读取路径,可以根据实际情况修改img=imread(str);%读取路径中的图片[M,N]=size(img(:,:,1));%提取图片的行列值%figure(1);%imshow(img);small_img=img(floor(M/16):2*floor(M/3),floor(N/3)-50:floor(N/3)*2,:);%为了提升运算速度和准确率,提取图片相应区域%滤波方法的选择ford=1:3%对RGB图像三个方向的图像进行滤波,三个滤波后的图像合成后成为最终图像K=medfilt2(small_img(:,:,d));%通过效果比较,最终确定中值滤波small_img(:,:,d)=K;%将三个方向的滤波图像合成为最终图像end%滤波完成hsv_img=rgb2hsv(small_img);%RGB图像空间转换到HSV空间hand_hsv_1=hsv_img;%由HSV空间H方向直方图可以知道手部阈值分布在两侧,因此手部提取分为两部分hand_hsv_2=hsv_img;%上面是第一部分,这是第二部分,提取后合并两部分[m,n]=size(hsv_img(:,:,1));%提取图像的行和列的值h1=0/255;%H方向阈值h2=40/255;%H方向阈值h3=204/255;%H方向阈值h4=255/255;%H方向阈值s1=5/255;%S方向阈值s2=250/255;%S方向阈值v1=30/255;%V方向阈值v2=255/255;%V方向阈值%下面这部分就是通过阈值提取手部粗图像fori=1:mforj=1:nif(hand_hsv_1(i,j,1)=h2)&&(hand_hsv_1(i,j,1)=h1)&&(hand_hsv_1(i,j,2)=s1)&&(hand_hsv_1(i,j,2)=s2)&&(hand_hsv_1(i,j,3)=v1)&&(hand_hsv_1(i,j,3)=v2)hand_hsv_1(i,j,:)=0;elsehand_hsv_1(i,j,:)=1;endif(hand_hsv_2(i,j,1)=h4)&&(hand_hsv_2(i,j,1)=h3)&&(hand_hsv_2(i,j,2)=s1)&&(hand_hsv_2(i,j,2)=s2)&&(hand_hsv_2(i,j,3)=v1)&&(hand_hsv_2(i,j,3)=v2)hand_hsv_2(i,j,:)=0;elsehand_hsv_2(i,j,:)=1;endendend%手部粗图像提取完毕hand_hsv=hand_hsv_1-~(hand_hsv_2);%手部粗图像合成,是二值图像%figure(7);Y=~hand_hsv(:,:,1);%取第一个方向的图像,取反,便于标记L1=bwlabel(Y);%标记二值图像STATS=regionprops(L1,'area');%对标记区域求面积w=[STATS.Area];%提取面积向量a=size(w);%连通区域的个数%通过筛选连通区域面积来提取手部精确图像fori=1:a(2)ifw(i)1200[x,y]=find(L1==i);x_min=min(x);y_min=min(y);x_max=max(x);y_max=max(y);ifx_min2x_min=x_min-2;endify_min2y_min=y_min-2;endifx_maxm-2x_max=x_max+2;endify_maxn-1y_max=y_max+2;endcut_spilth=~(L1(x_min:x_max,y_min:y_max));%对单个连通区域进行分析L=bwlabel(cut_spilth);%对单个连通区域取反后标记STATS=regionprops(L,'area');w2=[STATS.Area];b=size(w2);ifb(2)7&&w(i)3500I=L1(x_min:x_max,y_min:y_max);I(find(I==i))=1;hand_bw=I;W=w(i);elseL1(find(L1==i))=0;endelseL1(find(L1==i))=0;endendhand_edge=edge(uint8(hand_bw*255),'sobel');Edge_long=sum(sum(hand_edge==1));%得出两个面积和周长参数%提取力矩特征f=hand_bw;[x,y]=size(f);centroid_x=0;centroid_y=0;forx_i=1:xfory_i=1:ycentroid_x=centroid_x+x_i*f(x_i,y_i);centroid_y=centroid_y+y_i*f(x_i,y_i);endendcentroid_x=centroid_x/W;centroid_y=centroid_y/W;hand_M((ii-1)*5+jj,3)=0;forx_i=1:xfory_i=1:yhand_M((ii-1)*5+jj,3)=hand_M((ii-1)*5+jj,3)+(x_i-centroid_x)*(y_i-centroid_y)*f(x_i,y_i);%重心矩endendhand_M((ii-1)*5+jj,4)=0;forx_i=1:xfory_i=1:yhand_M((ii-1)*5+jj,4)=hand_M((ii-1)*5+jj,4)+((x_i-centroid_x)^2+(y_i-centroid_y)^2)*f(x_i,y_i);%惯性矩endendhand_M((ii-1)*5+jj,3)=hand_M((ii-1)*5+jj,3)/100000;hand_M((ii-1)*5+jj,4)=hand_M((ii-1)*5+jj,4)/1000000;hand_M((ii-1)*5+jj,1)=W/1000;hand_M((ii-1)*5+jj,2)=Edge_long/100;endendP=hand_M';%训练集的特征向量矩阵%验证集hand_M_1=[];Tt=zeros(10,20);forii=1:10forjj=1:2Tt(ii,(ii-1)*2+jj)=1;str=strcat('D:\ibmd盘\学习\研究生\任务\0323手势\验证集\',num2str(ii),'_',num2str(jj),'.bmp');img=imread(str);[M,N]=size(img(:,:,1));%figure(1);%imshow(img);small_img=img(floor(M/16):2*floor(M/3),floor(N/3)-50:floor(N/3)*2,:);ford=1:3K=medfilt2(small_img(:,:,d));endhsv_img=rgb2hsv(small_img);hand_hsv_1=hsv_img;hand_hsv_2=hsv_img;[m,n]=size(hsv_img(:,:,1));h1=0/255;h2=40/255;h3=204/255;h4=255/255;s1=5/255;s2=250/255;v1=30/255;v2=255/255;fori=1:mforj=1:nif(hand_hsv_1(i,j,1)=h2)&&(hand_hsv_1(i,j,1)=h1)&&(hand_hsv_1(i,j,2)=s1)&&(hand_hsv_1(i,j,2)=s2)&&(hand_hsv_1(i,j,3)=v1)&&(hand_hsv_1(i,j,3)=v2)hand_hsv_1(i,j,:)=0;elsehand_hsv_1(i,j,:)=1;endif(hand_hsv_2(i,j,1)=h4)&&(hand_hsv_2(i,j,1)=h3)&&(hand_hsv_2(i,j,2)=s1)&&(hand_hsv_2(i,j,2)=s2)&&(hand_hsv_2(i,j,3)=v1)&&(hand_hsv_2(i,j,3)=v2)hand_hsv_2(i,j,:)=0;elsehand_hsv_2(i,j,:)=1;endendendhand_hsv=hand_hsv_1-~(hand_hsv_2);%imshow(hand_hsv(:,:,1));Y=~hand_hsv(:,:,1);L1=bwlabel(Y);STATS=regionprops(L1,'area');w=[STATS.Area];a=size(w);c=0;fori=1:a(2)ifw(i)1200[x,y]=find(L1==i);x_min=min(x);y_min=min(y);x_max=max(x);y_max=max(y);ifx_min2x_min=x_min-2;endify_min2y_min=y_min-2;endifx_maxm-2x_max=x_max+2;endify_maxn-1y_max=y_max+2;endcut_spilth=~(L1(x_min:x_max,y_min:y_max));L=bwlabel(cut_spilth);STATS=regionprops(L,'area');w2=[STATS.Area];b=size(w2);ifb(2)7&&w(i)3500I=L1(x_min:x_max,y_min:y_max);I(find(I==i))=1;hand_bw=I;W=w(i);elseL1(find(L1==i))=0;endelseL1(find(L1==i))=0;endendhand_edge=edge(uint8(hand_bw*255),'sobel');%figure(9);%imshow(hand_edge);Edge_long=sum(sum(hand_edge==1));%得出两个面积和周长参数%提取力矩特征f=hand_bw;[x,y]=size(f);centroid_x=0;centroid_y=0;forx_i=1:xfory_i=1:ycentroid_x=centroid_x+x_i*f(x_i,y_i);centroid_y=centroid_y+y_i*f(x_i,y_i);endendcentroid_x=centroid_x/W;centroid_y=centroid_y/W;hand_M_1((ii-1)*2+jj,3)=0;forx_i=1:xfory_i=1:yhand_M_1((ii-1)*2+jj,3)=hand_M_1((ii-1)*2+jj,3)+(x_i-centroid_x)*(y_i-centroid_y)*f(x_i,y_i);endendhand_M_1((ii-1)*2+jj,4)=0;forx_i=1:xfory_i=1:yhand_M_1((ii-1)*2+jj,4)=hand_M_1((ii-1)*2+jj,4)+((x_i-centroid_x)^2+(y_i-centroid_y)^2)*f(x_i,y_i);endendhand_M_1((ii-1)*2+jj,3)=hand_M_1((ii-1)*2+jj,3)/100000;hand_M_1((ii-1)*2+jj,4)=hand_M_1((ii-1)*2+jj,4)/10000
本文标题:手势识别的代码
链接地址:https://www.777doc.com/doc-3367690 .html