作业帮 > 综合 > 作业

那位高人给加个注释啊,本人小白,

来源:学生作业帮 编辑:拍题作业网作业帮 分类:综合作业 时间:2024/04/27 23:26:39
那位高人给加个注释啊,本人小白,
function d = tracking(video)
if ischar(video)
% Load the video from an avi file.
avi = aviread(video);
pixels = double(cat(4,avi(1:2:end).cdata))/255;
clear avi
else
% Compile the pixel data into a single array
pixels = double(cat(4,video{1:2:end}))/255;
clear video
end
% Convert to RGB to GRAY SCALE image.
nFrames = size(pixels,4);
for f = 1:nFrames
% F = getframe(gcf);
% [x,map]=frame2im(F);
% imwrite(x,'fln.jpg','jpg');
% end
pixel(:,:,f) = (rgb2gray(pixels(:,:,:,f)));
end
rows=240;
cols=320;
nrames=f;
for l = 2:nrames
d(:,:,l)=(abs(pixel(:,:,l)-pixel(:,:,l-1)));
k=d(:,:,l);
% imagesc(k);
% drawnow;
% himage = imshow('d(:,:,l)');
% hfigure = figure;
% impixelregionpanel(hfigure,himage);
% datar=imageinfo(imagesc(d(:,:,l)));
% disp(datar);
bw(:,:,l) = im2bw(k,.2);
bw1=bwlabel(bw(:,:,l));
imshow(bw(:,:,l))
hold on
% % for h=1:rows
% for w=1:cols
%
% if(d(:,:,l)< 0.1)
% d(h,w,l)=0;
% end
% end
%
% end
% % disp(d(:,:,l));
% % size(d(:,:,l))
cou=1;
for h=1:rows
for w=1:cols
if(bw(h,w,l)>0.5)
% disp(d(h,w,l));
toplen = h;
if (cou == 1)
tpln=toplen;
end
cou=cou+1;
break
end
end
end
disp(toplen);
coun=1;
for w=1:cols
for h=1:rows
if(bw(h,w,l)>0.5)
leftsi = w;
if (coun == 1)
lftln=leftsi;
coun=coun+1;
end
break
end
end
end
disp(leftsi);
disp(lftln);
% % drawnow;
% % d = abs(pixel(:,:,l),pixel(:,:,l-1));
% % disp(d);
% s = regionprops(bw1,'BoundingBox');
% % centroids = cat(1,s.Centroid);
%
% % ang=s.Orientation;
%
% % plot(centroids(:,1),centroids(:,2),'r*')
% for r = 1 :length(s)
% rectangle('Position',s(r).BoundingBox,'EdgeColor','r');
%
% % plot('position',s(r).BoundingBox,'faceregion','r');
% end
%
% % disp(ang);
% % imaqmontage(k);
widh=leftsi-lftln;
heig=toplen-tpln;
widt=widh/2;
disp(widt);
heit=heig/2;
with=lftln+widt;
heth=tpln+heit;
wth(l)=with;
hth(l)=heth;
disp(heit);
disp(widh);
disp(heig);
rectangle('Position',[lftln tpln widh heig],'EdgeColor','r');
disp(with);
disp(heth);
plot(with,heth,'r*');
drawnow;
hold off
end;
% wh=square(abs(wth(2)-wth(nrames)));
% ht=square(abs(hth(2)-hth(nrames)));
% disp(wth(1
% distan=sqrt(wh+ht);
%
% disp(distan);
function d = tracking(video)
if ischar(video)

% 视频是否为字符数组
avi = aviread(video);
pixels = double(cat(4,avi(1:2:end).cdata))/255;
clear avi
else
% 否则直接读取视频中的帧,间隔取帧,得到一个四维的矩阵,里面放的是彩色图像
pixels = double(cat(4,video{1:2:end}))/255;
clear video
end
% 将每一帧的彩色图像转化为灰度图像
nFrames = size(pixels,4);
for f = 1:nFrames
% F = getframe(gcf);
% [x,map]=frame2im(F);
% imwrite(x,'fln.jpg','jpg');
% end
pixel(:,:,f) = (rgb2gray(pixels(:,:,:,f)));
end
rows=240;
cols=320;
nrames=f;
% 下面就是利用帧间做差发进行运动目标的跟踪
for l = 2:nrames
% 后一帧减去前一帧
d(:,:,l)=(abs(pixel(:,:,l)-pixel(:,:,l-1)));
k=d(:,:,l);
% imagesc(k);
% drawnow;
% himage = imshow('d(:,:,l)');
% hfigure = figure;
% impixelregionpanel(hfigure, himage);
% datar=imageinfo(imagesc(d(:,:,l)));
% disp(datar);
bw(:,:,l) = im2bw(k, .2);% 二值化

bw1=bwlabel(bw(:,:,l));% 连通标记
imshow(bw(:,:,l))%显示图像
% imshow(pixelsrgb(:,:,:,l)); % 显示彩色图像,标出运动……
hold on

% % for h=1:rows
% for w=1:cols
%
% if(d(:,:,l)< 0.1)
% d(h,w,l)=0;
% end
% end
%
% end

% % disp(d(:,:,l));
% % size(d(:,:,l))
cou=1;
% 下面的目的就是得到二值图像中目标的大小,并矩形框出来,连续显示
for h=1:rows
for w=1:cols
if(bw(h,w,l)>0.5)


% disp(d(h,w,l));
toplen = h;

if (cou == 1)
tpln=toplen;

end
cou=cou+1;
break
end

end
end
disp(toplen);
coun=1;
for w=1:cols
for h=1:rows
if(bw(h,w,l)>0.5)

leftsi = w;


if (coun == 1)
lftln=leftsi;
coun=coun+1;
end
break
end

end
end
disp(leftsi);
disp(lftln);
% % drawnow;
% % d = abs(pixel(:, :, l), pixel(:, :, l-1));
% % disp(d);

% s = regionprops(bw1, 'BoundingBox');
% % centroids = cat(1, s.Centroid);
%
% % ang=s.Orientation;
%
% % plot(centroids(:,1), centroids(:,2), 'r*')
% for r = 1 : length(s)
% rectangle('Position',s(r).BoundingBox,'EdgeColor','r');
%
% % plot('position',s(r).BoundingBox,'faceregion','r');
% end
%
% % disp(ang);
% % imaqmontage(k);
widh=leftsi-lftln;
heig=toplen-tpln;
widt=widh/2;
disp(widt);
heit=heig/2;
with=lftln+widt;
heth=tpln+heit;
wth(l)=with;
hth(l)=heth;
disp(heit);
disp(widh);
disp(heig);
rectangle('Position',[lftln tpln widh heig],'EdgeColor','r');
disp(with);
disp(heth);
plot(with,heth, 'r*');
drawnow;
hold off
end;
% wh=square(abs(wth(2)-wth(nrames)));
% ht=square(abs(hth(2)-hth(nrames)));
% disp(wth(1
% distan=sqrt(wh+ht);
%
% disp(distan);
再问: 谢谢。在问下,有这样一个语句: avi = mmreader('03.avi'); video = {avi.cdata}; for a = 1:length(video) imagesc(video{a}); axis image off drawnow; end; disp('output video'); tracking(video); 其中video={avi.cdata}什么意思?运行时出现错误 No appropriate method, property, or field cdata for class mmreader.
再答: 你读入的视频03.avi包括两部分cdata和colormap ,video={avi.cdata}中的 avi.cdata就是把其中的cdata这部分数据提取出来并赋值给你设定的变量video;运行时出现错误 No appropriate method, property, or field cdata for class mmreader.这说明mmreader这一读入视频的类不支持以上提取cdata的操作,这种提取操作只支持aviread类。,建议把mmreader改成aviread。
再问: 但是,aviread不能把我拍摄的视频读出来,mmread可以读。怎么办? 有什么方法可以在用mmreader的前提下,提取出来cdata这部分数据吗?
再答: 你可能运行的matlab版本应该不是matlab 7.0 的吧,这个function d = tracking(video)是在这个版本下编写的,建议把mmreader改成aviread,再运行在matlab 7.0上应该就可以了
再问: 谢谢啦,说不好以后还得麻烦您。