初始化CamShift算法的问题

时间:2018-06-21 08:43:00

标签: matlab image-processing video-processing mean-shift

我使用matlab使用凸轮平移来实现跟踪器。我需要自己实现它,因此我没有使用内置方法。

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

我在第一帧中选择我的脸: What I choose

它收敛到我的肩膀并开始追踪它: After first iteration

现在在整个视频中总体上可以进行跟踪,所以我认为问题在于初始化阶段。

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

0 个答案:

没有答案