Sunday, June 6, 2010

Meanshift Tracking algorithm



Mean shift

The main function of this algorithm is histogram estimation. Since moving objects can be identified by their color histogram. Mean-shift tracking algorithm is an iterative scheme based on comparing the histogram of the original object in the current image frame and histogram of candidate regions in the next image frame. The aim is to maximize the correlation between two histograms.

Code:


Mv = aviread('StabilizationResult.avi')
rmin = 0; %min row value for search window
rmax = 0; %max row value for search window
cmin = 0; %min col value for search window
cmax = 0; %max col value for search window
numofframes = 0;
frameNo = 10; %starting frame
vidSize=[128 160]; %video size
centerold = [0 0];
centernew = [0 0];

M =Mv;

% get number of frames
numberofframes = length(M);

Frame1 = M(frameNo);
Image1 = imresize(Frame1.cdata,vidSize,'bilinear');
disp('Click and drag mouse for an initial box window');
% get search window for first frame
[ cmin, cmax, rmin, rmax ] = select( Image1 );
cmin = round(cmin);
cmax = round(cmax);
rmin = round(rmin);
rmax = round(rmax);
wsize(1) = abs(rmax - rmin);
wsize(2) = abs(cmax - cmin);

hue=Image1(:,:,1);
histogram = zeros(256);

for i=rmin:rmax
for j=cmin:cmax
index = uint8(hue(i,j)+1);
%count number of each pixel
histogram(index) = histogram(index) + 1;
end
end

% for each frame
for i = frameNo:numberofframes
Frame = M(i);
I = imresize(Frame.cdata,vidSize,'bilinear');

hue= I(:,:,1);
[rows cols] = size(hue);
probmap = zeros(rows, cols);
for r=1:rows
for c=1:cols
if(hue(r,c) ~= 0)
probmap(r,c)= histogram(hue(r,c));
end
end
end
probmap = probmap/max(max(probmap));
probmap = probmap*255;

count = 0;

rowcenter = 0; % any number just so it runs through at least twice
colcenter = 0;
rowcenterold = 30;
colcenterold = 30;
while (((abs(rowcenter - rowcenterold) > 2) && (abs(colcenter - colcenterold) > 2)) || (count < 15) )
rowcenterold = rowcenter;
colcenterold = colcenter;

[ rowcenter colcenter M00 ] = meanshift(rmin, rmax, cmin,...
cmax, probmap);

rmin = round(rowcenter - wsize(1)/2);
if rmin<1 br=""> rmin=1;
end
rmax = round(rowcenter + wsize(1)/2);
if rmax<1 br=""> rmax=1;
end
cmin = round(colcenter - wsize(2)/2);
if cmin<1 br=""> cmin=1;
end
cmax = round(colcenter + wsize(2)/2);
if cmax<1 br=""> cmax=1;
end
wsize(1) = abs(rmax - rmin);
wsize(2) = abs(cmax - cmin);

count = count + 1;
end

G = I;
trackim=G;

%make box of current search window on saved image
for r= rmin:rmax
trackim(r, cmin:cmin+2) = 0;
trackim(r, cmax-2:cmax) = 0;
end
for c= cmin:cmax
trackim(rmin:rmin+2, c) = 0;
trackim(rmax-2:rmax, c) = 0;
end

windowsize = 100 * (M00/256)^.5;
sidelength = sqrt(windowsize);
rmin = round(rowcenter-sidelength/2);
if rmin<1 br=""> rmin=1;
end
rmax = round(rowcenter+sidelength/2);
if rmax<1 br=""> rmax=1;
end
cmin = round(colcenter-sidelength/2);
if cmin<1 br=""> cmin=1;
end
cmax = round(colcenter+sidelength/2);
if cmax<1 br=""> cmax=1;
end
wsize(1) = abs(rmax - rmin);
wsize(2) = abs(cmax - cmin);


outname = sprintf('./answer/%d.jpg', i);
imwrite(trackim, outname);

figure(1),imshow(trackim); hold on
end
hold off;



14 comments:

  1. Hi..I have trouble after this command
    >>[ cmin, cmax, rmin, rmax ] = select(Image1);

    It shows the following error;

    ??? Undefined function or method 'select' for input arguments of type 'uint8'.

    What can I do to fix it?

    ReplyDelete
  2. hello...
    >>outname = sprintf('./answer/%d.jpg', i);
    imwrite(trackim, outname);

    what is above command for? I encounter this error,

    ??? Error using ==> imwrite at 397
    Can't open file "./answer/10.jpg" for writing.
    You may not have write permission.

    Error in ==> mean_shift at 136
    imwrite(trackim, outname);

    Thank you.

    ReplyDelete
  3. hi there,
    Since the mean shift above working on video that has been saved, how to make it work for the live video using mean shift?
    Thanks.

    ReplyDelete
  4. the method select, is a method where a bounding box is selected via the users mouse click top left corner and bottom right corner.you may not have that method


    ive not posted this, but ive seperated the video to images and load each image in the ./answer folder so its either you do not have that image or your matlab does not have access to that folder, go to options and set the permission


    meanshift on a live video would mean you have to access your webcam, create a ROI manually using mouse clicks,and process, however matlab is too slow for live webcam, so you need to pause for the processing, check my post on live webcam feed here: http://areshmatlab.blogspot.com/2010/04/use-live-video-to-capture-images-for.html

    ReplyDelete
  5. hi there,
    I dont get it this error, also I working it with another video file.

    ??? Attempted to access hue(0,0); index must be a positive integer or logical.

    Error in ==> MeanShift_aresh at 34
    index = uint8(hue(i,j)+1);

    thank you

    ReplyDelete
  6. matrix index in matlab start from 1,1...since its 0,0...im assuming you did not select a ROI from your initial screen. although rmin and rmax have an initial of 0, they will change once you select a ROI with your mouse.

    ReplyDelete
  7. Hello,
    I have try to do live cam using means shift, but it stuck at the following,

    vid = videoinput('winvideo',1,'YUY2_320x240');
    set(vid,'ReturnedColorSpace','rgb');
    set(vid,'TriggerRepeat',Inf);
    handles.vid.TriggerFrameDelay = 1;
    vid_src = getselectedsource(vid);
    set(vid_src,'Tag','motion detection setup');
    set(vid_src,'FrameRate','30');

    start(vid)
    M =vid_src;
    numberofframes = length(M);

    Frame1 = M(frameNo);
    Image1 = imresize(Frame1.cdata,vidSize,'bilinear');

    any suggestion?

    ReplyDelete
  8. hello Saharkiz,
    If you don't mind, may I have your email address in response to any enquiries? Thank you in
    advance.

    ReplyDelete
  9. i suggest opencv if u want to do live camera feed as matlab is very slow for that, also for camera feed check first if matlab supports your camera

    ReplyDelete
  10. oh i see..i might dun have the time to understand the opencv language if i start now because the due date is 1+ month left. i need to mount it on motor also. any suggestion from you saharkiz?

    ReplyDelete
  11. hi,

    1. i got this error

    Undefined function or method 'meanshift' for input arguments of type 'double'

    do i need to creat menashift funct?

    2. can u help me on tracking by particle filter?

    ReplyDelete
  12. Warning: Unable to determine the number of frames in this file.

    Summary of Multimedia Reader Object for 'A.avi'.

    Video Parameters: 30.00 frames per second, RGB24 132x128.
    Unable to determine video frames available.

    ??? Index exceeds matrix dimensions.

    Error in ==> cha2 at 17
    Frame1 = M(frameNo);
    gives me this error...

    ReplyDelete
  13. Hi Friend, Error Creating for given line

    [ cmin, cmax, rmin, rmax ] = select( Image1 );

    That Error is given Bellow

    ??? Undefined function or method 'select' for input arguments of type
    'double'.

    Error in ==> sri at 26
    [ cmin, cmax, rmin, rmax ] = select( Image1 );


    How To Solve this Error Pls Reply to Me.

    ReplyDelete