clc;
clear;
close all;
warning off;
addpath 'func\'
load grnn.mat
NAME = 'cam1';
p0 = ['Imges\',NAME];
dt = dir(p0);
p = [dt.name];
correct = 0;
for k = 1:length(dt)-3
path = dt(k+3,1).name;
path = dt(k+3,1).name
%这个部分,我们基于cam0这个图像集合,进行库的建立和训练
%这个部分,我们基于cam0这个图像集合,进行库的建立和训练
Ks = 1/2;
I0 = imread([ 'Imges\',NAME,'\',path]);
[R,C,K] = size(I0);
if K == 1
I1 = I0;
else
I1 = rgb2gray(I0);
end
I1 = imresize(I1,Ks);
[R1,C1,K] = size(I1);
figure(1);
subplot(221);
imshow(I1);
title('原始图像');
%% 预处理:
%第一步由于采集到的深度图有的地方的深度值为零,首先用最邻近差值算法将为零的深度值用其周围的点代替
I2 = func_nearest_Interpolation(I1);
subplot(222);
imshow(uint8(I2));
title('最邻近差值图像');
%第二步:用中值滤波算法对上一步骤获得的图像进行处理,去噪声;
L = 5;
I3 = uint8(medfilt2(I2,[L,L]));
subplot(223);
imshow(I3);
title('中值滤波');
%第三步:获得二值图
I4(1:floor(5*R1/7),:) = im2bw(I3(1:floor(5*R1/7),:) , 0.9*graythresh(I3(1:floor(5*R1/7),:)));
I4(1+floor(5*R1/7):R1,:) = im2bw(I3(1+floor(5*R1/7):R1,:),1.25*graythresh(I3(1+floor(5*R1/7):R1,:)));
subplot(224);
imshow(I4);
title('二值图');
%第四步:边缘图
I5 = edge(I4,'canny');
%第五步:distance map
I6 = func_distancemap(I5);
I6 = 255-uint8(255*I6);
figure(2);
subplot(221);
imshow(I6);
title('distance图');
%第六步:提取上半身
[Is,Is_edge,indy] = func_bodycatch(I4,I6);
Is2 = bwareaopen(Is,4000);
subplot(222);
imshow(Is);
title('提取上半身');
subplot(223);
imshow(Is2);
title('提取上半身边缘');
%人体的提取
ff = uint8(255*Is2);
[rows,cols] = size(ff);
[Ls,n] = bwlabel(ff);
X1 = [];
X2 = [];
Y1 = [];
Y2 = [];
flag = 0;
L1 = zeros(R,C,3);
S = [];
for i=1:n
[r,c] = find(Ls==i);
a1(i) = max(r);
a2(i) = min(r);
b1(i) = max(c);
b2(i) = min(c);
w(i) = b1(i)-b2(i);
h(i) = a1(i)-a2(i);
S(i) = w(i)*h(i);
X1 = [X1,a2(i)];
X2 = [X2,a1(i)];
Y1 = [Y1,b2(i)];
Y2 = [Y2,b1(i)];
L1(a2(i):a2(i)+2,b2(i):b1(i),1) = 0;
L1(a2(i):a2(i)+2,b2(i):b1(i),2) = 0;
L1(a2(i):a2(i)+2,b2(i):b1(i),3) = 255;
L1(1.2*a1(i)-2:1.2*a1(i),b2(i):b1(i),1) = 0;
L1(1.2*a1(i)-2:1.2*a1(i),b2(i):b1(i),2) = 0;
L1(1.2*a1(i)-2:1.2*a1(i),b2(i):b1(i),3) = 255;
L1(a2(i):1.2*a1(i),b1(i)-2:b1(i),1) = 0;
L1(a2(i):1.2*a1(i),b1(i)-2:b1(i),2) = 0;
L1(a2(i):1.2*a1(i),b1(i)-2:b1(i),3) = 255;
L1(a2(i):1.2*a1(i),b2(i):b2(i)+2,1) = 0;
L1(a2(i):1.2*a1(i),b2(i):b2(i)+2,2) = 0;
L1(a2(i):1.2*a1(i),b2(i):b2(i)+2,3) = 255;
end
if length(S) > 1
LL = L1;
[V,I] = sort(S);
inds = I(end-1:end);
[RR,CC] = size(Is2);
IF = zeros(RR,CC);
for i = 1:RR
for j = 1:CC
if Is2(i,j) == 1
IF(i,j) = I1(i,j);
else
IF(i,j) = 0;
end
end
end
if X1(inds(1)) < X1(inds(2))
IF1 = IF(X1(inds(1)):X2(inds(1)),Y1(inds(1)):Y2(inds(1)));
XC1 = Y2(inds(1));
YC1 = X1(inds(1));
IF2 = IF(X1(inds(2)):X2(inds(2)),Y1(inds(2)):Y2(inds(2)));
XC2 = Y2(inds(2));
YC2 = X1(inds(2));
else
IF2 = IF(X1(inds(1)):X2(inds(1)),Y1(inds(1)):Y2(inds(1)));
XC2 = Y2(inds(1));
YC2 = X1(inds(1));
IF1 = IF(X1(inds(2)):X2(inds(2)),Y1(inds(2)):Y2(inds(2)));
XC1 = Y2(inds(2));
YC1 = X1(inds(2));
end
end
if length(S) == 1
[IF1,IF2,CUT,IFS,L1] = func_body_fenge(Is2,X1,X2,Y1,Y2);
LL = L1;
XC1 = Y2-30;
YC1 = X1;
XC2 = CUT-30;
YC2 = X1;
end
figure(3);
subplot(131);
imshow(IF,[]);
title('提取上半身边缘');
subplot(132);
imshow(IF1,[]);
title('提取上半身边缘1');
subplot(133);
imshow(IF2,[]);
title('提取上半身边缘2');
%特征提取
Hog_Dat1 = func_feature(IF1);
Hog_Dat2 = func_feature(IF2);
FF1 = sim(net,Hog_Dat1);
FF2 = sim(net,Hog_Dat2);
I0 = imresize(I0,Ks);
I_final = I0;
for i = 1:R1
for j = 1:C1
if LL(i,j,3) == 255
I_final(i,j,1) = 0;
I_final(i,j,2) = 0;
I_final(i,j,3) = 255;
end
end
end
figure(4);
imshow(I_final);
title('姿态识别效果');
if round(FF1) == 1
correct = correct+1;
text(XC2,YC2,'跳舞','color',[0,1,0],'fontsize',24);
else
text(XC2,YC2,'站立','color',[0,1,0],'fontsize',24);
end
if round(FF2) == 2
correct = correct+1;
text(XC1,YC1,'站立','color',[0,1,0],'fontsize',24);
else
text(XC1,YC1,'跳舞','color',[0,1,0],'fontsize',24);
end
pause(0.01);
end
%计算正确率
corrects = correct/2/(length(dt)-3);
disp('正确率:');
100*corrects