Meanshift 目标跟踪

track.m

function [lftln, tpln, widh, heig] = track(video)
% 功能:跟踪视频中的运动目标并显示
% 输入:video-待跟踪的视频
% 输出:d-差值图像序列

% 读入视频图像
if ischar(video)
    avi =VideoReader(video);
    images=read(avi);
    pixels = double(images(:,:,:,[1:2:end]))/255;
    clear avi
else
    pixels = double(images(:,:,:,[1:2:end]))/255;
    clear video
end
% 将RGB图像转换成灰度图像
nFrames = size(pixels,4);
for f = 1:nFrames
   pixel(:,:,f) = (rgb2gray(pixels(:,:,:,f)));  
end
 [rows,cols]=size(pixel(:,:,1));
nrames=f;
% 将相邻两帧图像进行作差,并将差值图像转换为二值图像
for l =5
d(:,:,l)=(abs(pixel(:,:,l)-pixel(:,:,l-1)));
k=d(:,:,l);
   bw(:,:,l) = im2bw(k, .2);
   bw1=bwlabel(bw(:,:,l));
   imshow(bw(:,:,l))
   hold on
% 标记运动物体的位置并显示
cou=1;
for h=1:rows
    for w=1:cols
     if(bw(h,w,l)>0.5)
      toplen = h;
           if (cou == 1)
            tpln=toplen;
         end
         cou=cou+1;
      break
     end
     end
end
disp(strcat('tpln=',num2str(tpln)));
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(strcat('leftsi=',num2str(leftsi)));
disp(strcat('lftln=',num2str(lftln)));
widh=leftsi-lftln;
heig=toplen-tpln;
widt=widh/2;
disp(strcat('widt=',num2str(widt)));
heit=heig/2;
with=lftln+widt;
heth=tpln+heit;
wth(l)=with;
hth(l)=heth;
 
disp(strcat('heit=',num2str(heit)));
disp(strcat('widh=',num2str(widh)));
disp(strcat('heig=',num2str(heig)));
rectangle('Position',[lftln tpln widh heig],'EdgeColor','r');
disp(strcat('with=',num2str(with)));
disp(strcat('heth=',num2str(heth)));
plot(with,heth, 'r*');
drawnow;
hold off
end

select.m

%function [] = select(video)
video='test.mp4';
close all;

%%%%%%%%%%%%%%%%%%运用帧差法进行目标的锁定%%%%%%%%%%%%%%%%%%%%%%%



avi=VideoReader(video);
frame1=read(avi,1);%选定视频的第一帧进行目标锁定
[lftln, tpln, widh, heig] = track(video);%调用track函数
[temp,rect]=imcrop(frame1,[lftln, tpln, widh, heig]);
[a,b,c]=size(temp); 		%a:row,b:col


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%计算目标图像的权值矩阵%%%%%%%%%%%%%%%%%%%%%%%
y(1)=a/2;
y(2)=b/2;
tic_x=rect(1)+rect(3)/2;
tic_y=rect(2)+rect(4)/2;
m_wei=zeros(a,b);%权值矩阵
h=y(1)^2+y(2)^2 ;%带宽


for i=1:a
    for j=1:b
        dist=(i-y(1))^2+(j-y(2))^2;
        m_wei(i,j)=1-dist/h; %epanechnikov profile
    end
end
C=1/sum(sum(m_wei));%归一化系数


%计算目标权值直方图qu
%hist1=C*wei_hist(temp,m_wei,a,b);%target model
hist1=zeros(1,4096);
for i=1:a
    for j=1:b
        %rgb颜色空间量化为16*16*16 bins
        q_r=fix(double(temp(i,j,1))/16);  %fix为趋近0取整函数
        q_g=fix(double(temp(i,j,2))/16);
        q_b=fix(double(temp(i,j,3))/16);
        q_temp=q_r*256+q_g*16+q_b;            %设置每个像素点红色、绿色、蓝色分量所占比重
        hist1(q_temp+1)= hist1(q_temp+1)+m_wei(i,j);    %计算直方图统计中每个像素点占的权重
    end
end
hist1=hist1*C;
rect(3)=ceil(rect(3));
rect(4)=ceil(rect(4));




%%%%%%%%%%%%%%%%%%%%%%%%%读取序列图像
%%myfile=dir('I:\matlab practise\image\*.jpg');
obj=VideoReader(video);%%用VideoReader读取视频
myfile=repmat(struct('name','','folder','','date','','bytes','','isdir','','datenum',''),obj.numberofframes,1);%%创建一个struct,大小为obj.numberofframes*1,用来存放每一帧的信息
for i=1:obj.Numberofframes%%视频的总帧数
    frame=read(obj,i);%%读取视频的每一帧
    imwrite(frame,strcat('E:\matlab项目\meanshift/image/',num2str(i),'.jpg'),'jpg');%%把视频的每一帧写入到image中
end


for i=1:obj.Numberofframes
    myfile(i)=dir(strcat('E:\matlab项目\meanshift/','image/',num2str(i),'.jpg'));%%把每一帧的信息放入到myfile中
end
lengthfile=length(myfile);


for l=1:lengthfile
    Im=imread(strcat(myfile(l).folder,'\',myfile(l).name));
    num=0;
    Y=[2,2];
    
    
    %%%%%%%mean shift迭代
    while((Y(1)^2+Y(2)^2>0.5)&num<20)   %迭代条件
        num=num+1;
        temp1=imcrop(Im,rect);
        %计算侯选区域直方图
        %hist2=C*wei_hist(temp1,m_wei,a,b);%target candidates pu
        hist2=zeros(1,4096);
        for i=1:a
            for j=1:b
                q_r=fix(double(temp1(i,j,1))/16);
                q_g=fix(double(temp1(i,j,2))/16);
                q_b=fix(double(temp1(i,j,3))/16);
                q_temp1(i,j)=q_r*256+q_g*16+q_b;
                hist2(q_temp1(i,j)+1)= hist2(q_temp1(i,j)+1)+m_wei(i,j);
            end
        end
        hist2=hist2*C;
        figure(2);
        subplot(1,3,1);
        title('直方图显示')
        plot(hist2);
        hold on;
        
        w=zeros(1,4096);
        for i=1:4096
            if(hist2(i)~=0) %不等于
                w(i)=sqrt(hist1(i)/hist2(i));
            else
                w(i)=0;
            end
        end
        
        
        
        %变量初始化
        sum_w=0;
        xw=[0,0];
        for i=1:a;
            for j=1:b
                sum_w=sum_w+w(uint32(q_temp1(i,j))+1);
                xw=xw+w(uint32(q_temp1(i,j))+1)*[i-y(1)-0.5,j-y(2)-0.5];
            end
        end
        Y=xw/sum_w;
        %中心点位置更新
        rect(1)=rect(1)+Y(2);
        rect(2)=rect(2)+Y(1);
    end
    
    
    %%%跟踪轨迹矩阵%%%
    tic_x=[tic_x;rect(1)+rect(3)/2];
    tic_y=[tic_y;rect(2)+rect(4)/2];
    
    v1=rect(1);
    v2=rect(2);
    v3=rect(3);
    v4=rect(4);
    %%%显示跟踪结果%%%
    subplot(1,3,3);
    imshow(uint8(Im));
    title('目标跟踪结果及其运动轨迹');
    hold on;
    plot([v1,v1+v3],[v2,v2],[v1,v1],[v2,v2+v4],[v1,v1+v3],[v2+v4,v2+v4],[v1+v3,v1+v3],[v2,v2+v4],'LineWidth',2,'Color','r');
    plot(tic_x,tic_y,'LineWidth',2,'Color','b');
    subplot(1,3,2);
    I=read(obj,l);
     imshow(I);
      title('原始运动视频');
    drawnow;
    hold off
    
    
end


 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值