手势识别的代码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

clc;
clear;
hand_M=[];%定义训练集的特征向量
T=zeros(10,50);%定义训练集的目标向量
for ii=1:10 %手势类别循环
for jj=1:5 %手势个数循环
T(ii,(ii-1)*5+jj)=1;%
str=strcat('D:\ibm d盘\学习\研究生\任务\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,:);%为了提升运算速度和准确率,提取图片相应区域
%滤波方法的选择
for d=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方向阈值
%下面这部分就是通过阈值提取手部粗图像
for i=1:m
for j=1:n
if (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;
else hand_hsv_1(i,j,:)=1;
end
if (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;
else hand_hsv_2(i,j,:)=1;
end
end
end
%手部粗图像提取完毕
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);%连通区域的个数
%通过筛选连通区域面积来提取手部精确图像
for i=1:a(2)
if w(i)>1200
[x,y]=find(L1==i);
x_min=min(x);
y_min=min(y);
x_max=max(x);
y_max=max(y);
if x_min>2
x_min=x_min-2;
end
if y_min>2
y_min=y_min-2;
end
if x_maxx_max=x_max+2;
end
if y_maxy_max=y_max+2;
end
cut_spilth=~(L1(x_min:x_max,y_min:y_max));%对单个连通区域进行分析
L=bwlabel(cut_spilth);%对单个连通区域取反后标记
STATS=regionprops(L,'area');
w2=[STATS.Area];
b=size(w2);
if b(2)<7&&w(i)<3500
I=L

1(x_min:x_max,y_min:y_max);
I(find(I==i))=1;
hand_bw=I;
W=w(i);
else
L1(find(L1==i))=0;
end
else
L1(find(L1==i))=0;
end
end
hand_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;
for x_i=1:x
for y_i=1:y
centroid_x=centroid_x+x_i*f(x_i,y_i);
centroid_y=centroid_y+y_i*f(x_i,y_i);
end
end
centroid_x=centroid_x/W;
centroid_y=centroid_y/W;
hand_M((ii-1)*5+jj,3)=0;
for x_i=1:x
for y_i=1:y
hand_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);%重心矩
end
end
hand_M((ii-1)*5+jj,4)=0;
for x_i=1:x
for y_i=1:y
hand_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);%惯性矩
end
end
hand_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;
end
end
P=hand_M';%训练集的特征向量矩阵
%验证集
hand_M_1=[];
Tt=zeros(10,20);
for ii=1:10
for jj=1:2
Tt(ii,(ii-1)*2+jj)=1;
str=strcat('D:\ibm d盘\学习\研究生\任务\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,:);
for d=1:3
K=medfilt2(small_img(:,:,d));
end
hsv_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;
for i=1:m
for j=1:n
if (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;
else hand_hsv_1(i,j,:)=1;
end
if (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;
else hand_hsv_2(i,j,:)=1;
end
end
end
hand_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;
for i=1:a(2)
if w(i)>1200
[x,y]=find(L1==i);
x_min=min(x);
y_min=min(y);
x_max=max(x);
y_max=max(y);
if x_min>2
x_min=x_min-2;
end
if y_min>2
y_min=y_min-2;
end
if x_maxx_max=x_max+2;
end
if y_maxy_max=y_max+2;
end
cut_spilth=~(L1(x_min:x_max,y_min:y_max));
L=bwlabel(cut_spilth);
STATS=regionprops(L,'area');
w2=[STATS.Area];
b=size(w2);
if b(2)<7&&w(i)<3500
I=L1(x_min:x_max,y_min:y_max);
I(find(I==i))=1;
hand_bw=I;
W=w(i);
else
L1(find(L1==i))=0;
end
else
L1(find(L1=

=i))=0;
end
end

hand_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;
for x_i=1:x
for y_i=1:y
centroid_x=centroid_x+x_i*f(x_i,y_i);
centroid_y=centroid_y+y_i*f(x_i,y_i);
end
end
centroid_x=centroid_x/W;
centroid_y=centroid_y/W;
hand_M_1((ii-1)*2+jj,3)=0;
for x_i=1:x
for y_i=1:y
hand_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);
end
end
hand_M_1((ii-1)*2+jj,4)=0;
for x_i=1:x
for y_i=1:y
hand_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);
end
end
hand_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)/1000000;
hand_M_1((ii-1)*2+jj,1)=W/1000;
hand_M_1((ii-1)*2+jj,2)=Edge_long/100;
end
end
%完毕
Pt=hand_M_1';
net_1=newff(minmax(P),[12,10],{'tansig','logsig'},'trainlm');

% 设置训练参数
net_1.trainParam.show = 50;
net_1.trainParam.lr = 0.05;
net_1.trainParam.mc = 0.9;
net_1.trainParam.epochs = 200;
net_1.trainParam.goal = 1e-2;

% 调用 TRAINGDM 算法训练 BP 网络
net_1= train(net_1,P,T);
A=sim(net_1,Pt);
c=0;
for i=1:10
for j=1:20
if (Tt(i,j)==1)&&(A(i,j)-Tt(i,j)<=0.3)&&(A(i,j)-Tt(i,j)>=-0.3);
c=c+1;
end
end
end
c%判断准确的个数

相关文档
最新文档