ML0004基于Matlab实现的经典Mean shift 目标跟踪算法

2021年10月19日 0 条评论 828 次阅读 0 人点赞

 


function [x,y,loss,f,f_indx] = MeanShift_Tracking(q,I2,Lmap,...
    height,width,f_thresh,max_it,x0,y0,H,W,k,gx,gy,f,f_indx,...
    loss)
% Initialization in the next frame from the
% same location than in the current frame.
y = y0;
x = x0;
T2 = I2(y:y+H-1,x:x+W-1);
p = Density_estim(T2,Lmap,k,H,W,0);
% Number of iterations
step = 1;
% Computation of the similarity value
% between the two PDF.
[fi,w] = Simil_func(q,p,T2,k,H,W);
f = cat(2,f,fi);
% Applying Mean-shift algorithm
while f(f_indx)<f_thresh && step<max_it
    step = step+1;
    f_indx = f_indx+1;
    num_x = 0;
    num_y = 0;
    den = 0;
    for i = 1:H
        for j=1:W
            num_x = num_x+i*w(i,j)*gx(i,j);
            num_y = num_y+j*w(i,j)*gy(i,j);
            den = den+w(i,j)*norm([gx(i,j) gy(i,j)]);
        end
    end
    % Displacement vector (dx,dy) on the gradient ascent
    if den ~= 0
        dx = round(num_x/den);
        dy = round(num_y/den);
        y = y+dy;
        x = x+dx;
    end
    % Detection of target loss or out of frame boundaries
    if (y<1 || y>height-H) || (x<1 || x>width-W)
        loss = 1;
        Target_Loss_Dialog_Box();
        uiwait(Target_Loss_Dialog_Box);
        break;
    end
    % Update the target
    T2 = I2(y:y+H-1,x:x+W-1);
    p = Density_estim(T2,Lmap,k,H,W,0);
    [fi,w] = Simil_func(q,p,T2,k,H,W);
    f = cat(2,f,fi);
end

提取码:3mce

极寒钛

别给思维设限