1.问题描述:
2.部分程序:
clc;
clear;
close all;
vstream = mmreader('avi\11.avi');
ratio = 0.5;
imH = get(vstream,'Height')*ratio;
imW = get(vstream,'Width')*ratio;
nFrm = get(vstream,'NumberOfFrames');
for i=30:nFrm
n=i-1;
h=i-2;
m=read(vstream,i);%%从S处把图像读取出来
m=rgb2gray(m);%%将图像M灰度化
m=medfilt2(m,[3,3]);%中值滤波
if i>=3
n = read(vstream,i-1);
n = rgb2gray(n);
h = read(vstream,i-2);
h = rgb2gray(h);
n = medfilt2(n,[3,3]);
h = medfilt2(h,[3,3]);
q = im2double(m);%3.jpg%将图像数组转换为double型
w = im2double(n);%2.jpg
g = im2double(h);%1.jpg
c = q-w;
jx = q-g;
th=10/255;
k=find(abs(c)>=th);
c(k)=1;
k=find(abs(c)<th);
c(k)=0;
c=bwareaopen(c,15);%开运算,除去小部分
se90=strel('line',3,90);
se0=strel('line',3,0);
c=bwmorph(c,'close'); %对上述图像进行形态学闭运算
c=imdilate(c,[se90,se0]);
c=bwmorph(c,'close');
c=bwareaopen(c,50);
k=find(abs(jx)>=th);
jx(k)=1;
k=find(abs(jx)<th);
jx(k)=0;
j=bwareaopen(j,15);
jx=bwmorph(jx,'close'); %对上述图像进行形态学闭运算
jx=imdilate(jx,[se90,se0]);
jx=bwmorph(jx,'close');
jx=bwareaopen(jx,50);%除去小部分
c=c&jx;
c=imerode(c,[se90,se0]);
SE2 = strel('diamond', 6);
c=imdilate(c,SE2,'same'); %dilate
c = imresize(c,ratio);
figure(1);
subplot(221);imshow(c);
[Ls,ns] = bwlabel(c);
figure(1);
subplot(222);imshow(double(mean(Ls,3)),[]);
BW(1:size(c,1),1:size(c,2)) = 0;
for n=1:ns
[r,col] = find(Ls==n);
a1(n) = max(r);
a2(n) = min(r);
b1(n) = max(col);
b2(n) = min(col);
wx(n) = b1(n)-b2(n);
gx(n) = a1(n)-a2(n);
square(n) = wx(n)*gx(n);%区域面积
LL1 = size(r);%区域中点的个数
if square(n) > 800%当面积大于一定值时
for ii = 1:LL1
BW(r(ii),col(ii)) = 1; %区域中点为白色
end
end
end
figure(1);
subplot(223);imshow(BW);
[B,L,N,A] = bwboundaries(BW, 8, 'noholes');
IMG_Org = read(vstream, i);
img = imresize(IMG_Org,ratio);
figure(1);
subplot(224);imshow(img);title(i);hold on;
disp(i);
nobjs(i) = size(B,1);
jj = 0;
nseg = i;
M(1:nobjs(i)) = 0;
Qw(i,1:nobjs(i)) = 0;
z = 1;
nn(i) = 0;
if nobjs(i)>1
for a=1:nobjs(i)-1
for j=1:nobjs(i)-a
x1=mean(B{a}(:,2));
x2=mean(B{j+a}(:,2));
dis=min(abs(max(B{j}(:,1))-min((B{a+j}(:,1)))),abs(max(B{a+j}(:,1))-min(B{j}(:,1))));
if (((x1<x2)&&(max(B{j}(:,2))>min(B{j+a}(:,2))))||((x1>x2)&&(min(B{j}(:,2))<max(B{j+a}(:,2)))))&&dis<500
M(a) = z;
M(a+j) = z;
L(a) = a;
L(a+j) = a;
z = z + 1;
nn(i) = nn(i) + 1;
end
end
end
end
% for j = 1:nobjs(i)
% plot(B{j}(:,2),B{j}(:,1),'y','LineWidth',2);hold on
% end
for j=1:nobjs(i)
%计算中心坐标
X_top(j) = max(B{j}(:,2))-3;
X_bot(j) = min(B{j}(:,2))+3;
Y_top(j) = max(B{j}(:,1))-3;
Y_bot(j) = min(B{j}(:,1))+3;
plot(X_bot(j):X_top(j),Y_bot(j),'g--','LineWidth',2);hold on;
plot(X_bot(j):X_top(j),Y_top(j),'g--','LineWidth',2);hold on;
plot(X_bot(j),Y_bot(j):Y_top(j),'g--','LineWidth',2);hold on;
plot(X_top(j),Y_bot(j):Y_top(j),'g--','LineWidth',2);hold on;
end
%进行头部检测算法来区分不同的人
% for j=1:nobjs(i)-nn(i)
% %计算中心坐标
% X_top(j) = max(B{j}(:,2))-3;
% X_bot(j) = min(B{j}(:,2))+8;
% Y_top(j) = max(B{j}(:,1))-3;
% Y_bot(j) = min(B{j}(:,1))+3;
%
% X_center(nseg,j) = 0.5*(X_top(j) + X_bot(j));
% Y_center(nseg,j) = 0.5*(Y_top(j) + Y_bot(j));
% % X_center(nseg,j) =sum(B{j}(:,2))/size(B{j}(:,2),1);
% % Y_center(nseg,j) =sum(B{j}(:,1))/size(B{j}(:,1),1);
%
% plot(B{j}(:,2),B{j}(:,1),'y','LineWidth',2);hold on
%
% % plot(X_bot(j):X_top(j),Y_bot(j),'g--','LineWidth',2);hold on;
% % plot(X_bot(j):X_top(j),Y_top(j),'g--','LineWidth',2);hold on;
% % plot(X_bot(j),Y_bot(j):Y_top(j),'g--','LineWidth',2);hold on;
% % plot(X_top(j),Y_bot(j):Y_top(j),'g--','LineWidth',2);hold on;
% end
end
pause(0.5)
end
3.仿真结论:
C-09