基于帧间差法的运动检测

       run.m

clear data
disp('input video');


avi = aviread('samplevideo.avi');
video = {avi.cdata};
for a = 1:length(video)
    imagesc(video{a});
    axis image off
    drawnow;
end;
disp('output video');
tracking(video);


函数 tracking.m

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)));%隔帧差分
%d(:,:,l)=(abs(pixel(:,:,l)-pixel(:,:,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);%二值化,阈值为0.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);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值