⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tracking_changedetection.m

📁 this code is a Mean shift example
💻 M
字号:
clear all
echo off
%fname='E:\目标跟踪\目标跟踪论文(2006年4月)\capture01.m1v';
fname='E:\目标跟踪\目标跟踪论文(2006年4月)\如心楼下.m1v';
back=zeros(288,352);
for k=1:50;
    [R1 G1 B1]=mpgread(fname,k);
    back = back+0.299.*R1 + 0.587.*G1 + 0.114*B1;
end
back=back./50;
[R1 G1 B1]=mpgread(fname,51);
%kuang_x_xia=90;kuang_x_sh=120;
%kuang_y_xia=30;kuang_y_sh=70;

RGB(:,:,1)=R1;
RGB(:,:,2)=G1;
RGB(:,:,3)=B1;
imshow(RGB)
%kuang_x_xia=160;kuang_x_sh=180;
%kuang_y_xia=40;kuang_y_sh=80;

I= 0.299.*R1 + 0.587.*G1 + 0.114*B1;
%[x,map]=gray2ind(I);
%mov(1)=im2frame(x,map); 
%m1=abs(0.299.*R1 + 0.587.*G1 + 0.114*B1-back)>0.1; 
%[x,map]=gray2ind(m1);
%movm(1)=im2frame(x,map); 

%画框 
key=waitforbuttonpress;
cp1=get(gca,'CurrentPoint');
kuang_x_xia=cp1(1,2);kuang_y_xia=cp1(1,1);
key=waitforbuttonpress;
cp2=get(gca,'CurrentPoint');
kuang_x_sh=cp2(1,2);kuang_y_sh=cp2(1,1);

R1(kuang_x_xia:kuang_x_sh,kuang_y_xia)=1;
G1(kuang_x_xia:kuang_x_sh,kuang_y_xia)=0;
B1(kuang_x_xia:kuang_x_sh,kuang_y_xia)=0;

R1(kuang_x_xia:kuang_x_sh,kuang_y_sh)=1;
G1(kuang_x_xia:kuang_x_sh,kuang_y_sh)=0;
B1(kuang_x_xia:kuang_x_sh,kuang_y_sh)=0;

R1(kuang_x_xia,kuang_y_xia:kuang_y_sh)=1;
G1(kuang_x_xia,kuang_y_xia:kuang_y_sh)=0;
B1(kuang_x_xia,kuang_y_xia:kuang_y_sh)=0;

R1(kuang_x_sh,kuang_y_xia:kuang_y_sh)=1;
G1(kuang_x_sh,kuang_y_xia:kuang_y_sh)=0;
B1(kuang_x_sh,kuang_y_xia:kuang_y_sh)=0;

RGB(:,:,1)=R1;
RGB(:,:,2)=G1;
RGB(:,:,3)=B1;
imshow(RGB)
imwrite(RGB,'001.bmp')

for k=51:1000
[R G B]=mpgread(fname,k);
 current= 0.299.*R + 0.587.*G + 0.114*B;
 m1=abs(back-current)>0.1; 
 for m=1:288;
     for n=1:352;
         if(m1(m,n)==1)
             back(m,n)=0.99*current(m,n)+0.01*back(m,n);
         end
     end
 end
 
 temp_x_sh=kuang_x_sh;
temp_x_xia=kuang_x_xia;
 temp_y_sh=kuang_y_sh;
 temp_y_xia=kuang_y_xia;
 
 %计算新的框,搜索窗各左、右、上、下扩10个象素(尝试8,5,3)
 %沿水平方向的下限
 T=0;
 for i=kuang_x_xia-1:-1:kuang_x_xia-2;
     if (i~=0)
         for j=kuang_y_xia-2:kuang_y_sh+2;
            if(j>0 && j<=352 && m1(i,j)==1)
                 T=T+1;
            end
         end
        % if(T>(kuang_y_sh-kuang_y_xia))
        if(T>1)
            temp_x_xia=i;
        end
     end
 end

 if(T==0)    %如果物体没有向上运动,temp_x_xia应该向下扩展
     T=0;
     for  i=kuang_x_xia+1:kuang_x_xia+2;
         if(i<=288)
             for j=kuang_y_xia-2:kuang_y_sh+2;
                if(j>0 && j<=352 && m1(i,j)==1)
                     T=T+1;
                end
             end
            if(T>1)
                 temp_x_xia=i; 
                break;   %跳出for i的循环
            end
         end
     end  %for i
   temp_x_xia=i;
end
 
 
 
 T=0;
 for i=kuang_x_sh+1:kuang_x_sh+2;
     if(i<=288)
        for j=kuang_y_xia-2:kuang_y_sh+2;
            if(j>0 && j<=352 && m1(i,j)==1)
                T=T+1;
            end
        end
        %if(T>(kuang_y_sh-kuang_y_xia))
        if(T>1)
            temp_x_sh=i;
        end
     end
 end
  if(T==0)    %如果物体没有向下运动,temp_x_xia应该向上扩展
     T=0;
     for  i=kuang_x_sh-1:-1:kuang_x_sh-2;
         if(i>0 && i<=288)
             for j=kuang_y_xia-2:kuang_y_sh+2;
                 if(j>0 && j<352 && m1(i,j)==1)
                     T=T+1;
                 end
             end
             if(T>1)
                 temp_x_sh=i;
                 break;
             end
         end
     end %for i 
     temp_x_sh=i;
 end
 
 
 
 T=0;
 for j=kuang_y_xia-1:-1:kuang_y_xia-2;
     if(j>0 && j<=352)
         for i=kuang_x_xia-2:kuang_x_sh+2;
             if(i>0 && i<=288 && m1(i,j)==1)
                 T=T+1;
             end
         end
       %  if(T>(kuang_x_sh-kuang_x_xia))
           if(T>1)
             temp_y_xia=j;
           end
     end
 end
 if(T==0)    %如果物体没有向左运动,temp_x_xia应该向右扩展
     T=0;
     for  j=kuang_y_xia+1:kuang_y_xia+2;
        if(j>0 && j<=352)
             for i=kuang_x_xia-2:kuang_x_sh+2;
                 if(i>0 && i<=288 && m1(i,j)==1)
                     T=T+1;
                 end
             end
             if(T>1)
                 temp_y_xia=j;
                 break;
             end
        end
     end  %for j
     temp_y_xia=j;
  end
 
  
 T=0;
 for j=kuang_y_sh+1:kuang_y_sh+2;
     if(j>0 && j<=352)
         for i=kuang_x_xia-2:kuang_x_sh+2;
             if(i>0 && i<=288 && m1(i,j)==1)
                 T=T+1;
             end
         end
      %   if(T>(kuang_x_sh-kuang_x_xia))
          if(T>1)
             temp_y_sh=j;
          end
     end
 end
 if(T==0)    %如果物体没有向右运动,temp_x_xia应该向左扩展
     T=0;
     for  j=kuang_y_sh-1:-1:kuang_y_sh-2;
         if (j>0 && j<=352)
             for i=kuang_x_xia-2:kuang_x_sh+2;
                 if(i>0 && i<=288 && m1(i,j)==1)
                     T=T+1;
                 end
             end
             if(T>1)
                 temp_y_xia=j;
                 break;
             end
         end
     end
     temp_y_sh=j;
 end

if(temp_x_xia>temp_x_sh)
    temp_x_xia=temp_x_sh;
end
if(temp_y_xia>temp_y_sh)
    temp_y_xia=temp_y_sh;
end
R(temp_x_xia:temp_x_sh,temp_y_xia)=1;
G(temp_x_xia:temp_x_sh,temp_y_xia)=0;
B(temp_x_xia:temp_x_sh,temp_y_xia)=0;

R(temp_x_xia:temp_x_sh,temp_y_sh)=1;
G(temp_x_xia:temp_x_sh,temp_y_sh)=0;
B(temp_x_xia:temp_x_sh,temp_y_sh)=0;

R(temp_x_xia,temp_y_xia:temp_y_sh)=1;
G(temp_x_xia,temp_y_xia:temp_y_sh)=0;
B(temp_x_xia,temp_y_xia:temp_y_sh)=0;

R(temp_x_sh,temp_y_xia:temp_y_sh)=1;
G(temp_x_sh,temp_y_xia:temp_y_sh)=0;
B(temp_x_sh,temp_y_xia:temp_y_sh)=0;
%I= 0.299.*R + 0.587.*G + 0.114*B;
% [x,map]=gray2ind(I);
% mov(k-9)=im2frame(x,map);
 
% m1(temp_x_xia:temp_x_sh,temp_y_xia)=1;
% m1(temp_x_xia:temp_x_sh,temp_y_sh)=1;
% m1(temp_x_xia,temp_y_xia:temp_y_sh)=1;
% m1(temp_x_sh,temp_y_xia:temp_y_sh)=1;
% [x,map]=gray2ind(m1);
% movm(k-9)=im2frame(x,map);

RGB(:,:,1)=R;
RGB(:,:,2)=G;
RGB(:,:,3)=B;
imshow(RGB)
 if (k-9) < 10
              prefix = '00';
          elseif (k-9) < 100
              prefix = '0';
          else
             prefix = '';
    end
    imwrite(RGB,strcat(prefix,int2str(k-9),'.bmp'));
    
 kuang_x_sh=temp_x_sh;
 kuang_x_xia=temp_x_xia;
 kuang_y_sh=temp_y_sh;
 kuang_y_xia=temp_y_xia;
    
end   
%mpgwrite(mov,map,'e:\eye\media\abs.mpg');
%mpgwrite(mov,map,'E:\目标跟踪\目标跟踪论文(2006年4月)\2_ChangeDetection.m1v');
%mpgwrite(movm,map,'E:\目标跟踪\目标跟踪论文(2006年4月)\2_ChangeDetection_m1.m1v');

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -