首页 > 解决方案 > 初始化 CamShift 算法的问题

问题描述

我使用 matlab 来实现一个使用 cam shift 的跟踪器。我需要自己实现它,因此我没有使用内置方法。

我让用户选择要跟踪的对象,在第一次迭代后收敛使其开始跟踪不同的对象。图片胜于雄辩,所以:

我在第一帧中选择了我的脸: 我选择什么

它会聚到我的肩膀上并开始跟踪它: 第一次迭代后

现在整个视频的跟踪总体上没问题,所以我认为问题出在初始化阶段。

SomeCode:从用户那里获取位置:

function [pos] = getObjectPosition(F)
f = figure;
imshow(F);
pos = getPosition(imrect(gca));
close(f);
end

主循环:

firstFrame = readFrame(input);
pos = getObjectPosition(firstFrame);

while hasFrame(input)
% init next frame
nextFrame = readFrame(input);
[currFrameHSV, ~, ~] = rgb2hsv(nextFrame);
frameHue = double(currFrameHSV(:,:,1));

% init while loop vars 
prevX = -1;
prevY = -1;
i = 0;
while (i < numOfIterations) && ~(prevX == pos(1) && prevY == pos(2))
    % updating the curr iteration number
    i = i + 1;
    % getting search window location and dimensions
    envRect = round(getEnvRect(pos,pos(4)/yRatio,pos(3)/xRatio,height,width));

    [X,Y] = meshgrid(envRect(1):envRect(1) + envRect(3),...
                     envRect(2):envRect(2) + envRect(4));

    % getting the sub image
    I = imcrop(frameHue,envRect);

    M00 = sum(sum(I));
    M10 = sum(sum(X.*I));
    M01 = sum(sum(Y.*I));

    % getting center mass location
    Xc = round(M10/M00);
    Yc = round(M01/M00);

    % updating object position
    prevX = pos(1);
    prevY = pos(2);
    pos(1) = floor(Xc - pos(3)/2);
    pos(2) = floor(Yc - pos(4)/2);

end

% adding the object's bounding box to the frame to write
% show the image
end

getEnv 方法:

function [ envRect ] = getEnvRect( feature,hShift,wShift,rows,cols )
hEnvSize = feature(4) + 2*hShift; 
wEnvSize = feature(3) + 2*wShift;
xPatch  = feature(1);
xEnv    = max(1, xPatch - wShift);
yPatch  = feature(2);
yEnv    = max(1, yPatch - hShift);
width   = min(cols, xEnv + wEnvSize) - xEnv;
height  = min(rows, yEnv + hEnvSize) - yEnv;

envRect = [xEnv, yEnv, width, height];
end

标签: matlabimage-processingvideo-processingmean-shift

解决方案


推荐阅读