How to track the object using centroid ?

I need to track the object in a video using centroid. By calculating the centroid of the object in different frames, I'll be able to track the object and get the count. I'm new to matlab and guide me in completeing the task

 採用された回答

Walter Roberson
Walter Roberson 2013 年 2 月 19 日

0 投票

regionprops() with the Centroid option.
You may need to convert the image to grayscale or binary first.

13 件のコメント

aarthi ramasamy
aarthi ramasamy 2013 年 2 月 19 日
編集済み: aarthi ramasamy 2013 年 2 月 19 日
I have already converted it to binary image but the centroid portion is alone the problem here.
clc
clear all
n=0;
% Read movie
SF = 1; %Starting Frame
MV = mmreader('r2.avi'); %To read Movie
EF = MV.NumberofFrames ; %Ending Frame
img = read(MV,1); %reading particular frame
A = double(rgb2gray(img)); %motion vector works on Gray Image formate
[height width] = size(A); %Movie size?
% motion estimation
h1 = figure(1);
for f = SF: EF %Frame from SF To EF
B = A;
img = read(MV,f);
A = double(rgb2gray(img));
%Foreground Detection
thresh=11;
fr_diff = abs(A-B);
for j = 1:width
for k = 1:height
if (fr_diff(k,j)>thresh)
fg(k,j) = A(k,j);
else
fg(k,j) = 0;
end
end
end
subplot(2,2,1) , imagesc(img), title (['Orignal Sequence Frame#',int2str(f)]);
subplot(2,2,2) , imshow(mat2gray(A)), title ('Previous Frame');
subplot(2,2,3) , imshow(mat2gray(B)), title ('Next Frame');
sd=imadjust(fg);
level=graythresh(sd);
m=imnoise(sd,'gaussian',0,0.025);
k=wiener2(m,[5,5]);
bw=im2bw(k,level);
bw2 = bwareaopen(bw, 20, 8);
b1= imdilate(bw2, ones(1, 1, 5));
b2=imfill(b1,'holes');
% [labeled,numObjects] = bwlabel(bw,4);
% n=n+numObjects;
subplot(2,2,4) , imagesc(b1), title (['Foreground #',n]);
hold off;
%pause (.1);
% saveas(h1,strcat('Result-Paris-',num2str(f)),'jpg');
end
disp(n);
The above stated is my code. I don't know how to proceed further. My objective is to track the different objects and count it. I terribly need the guidance/code.
Walter Roberson
Walter Roberson 2013 年 2 月 19 日
編集済み: Walter Roberson 2013 年 2 月 19 日
At the point where you have the commented bwlabel,
[labeled, numObjects] = bwlabel(b2, 4);
blobinfo = regionprops(labeled, 'Centroid');
now blobinfo(1).Centroid is the first centroid
Image Analyst
Image Analyst 2013 年 2 月 19 日
% Extract all centroids for all blobs in a 1D list.
allBlobCentroids = [blobinfo.Centroid];
% From that list, extract out the x and y coordinates of the centroids.
centroidsX = allBlobCentroids(1:2:end-1);
centroidsY = allBlobCentroids(2:2:end);
Walter Roberson
Walter Roberson 2013 年 2 月 19 日
Alternately,
allBlobCentrods = vertcat(blobinfo.Centroid);
would produce a numObjects x 2 array, first column x, second column y.
aarthi ramasamy
aarthi ramasamy 2013 年 2 月 20 日
Thanks for your guidance Mr.Roberson . How to track the object using this centroid? I can clearly get the centroid of the objects in the different frames but applying this for tracking makes a problem here. What if there are two objects present in a single frame itself?
Walter Roberson
Walter Roberson 2013 年 2 月 20 日
More than one object present in a single frame must be expected for your application.
When objects are sufficiently separated and exist separated for multiple frames, you can form a velocity estimate by associating the updated centroid positions with the nearest previous centroid position. Then when the objects come close together, instead of necessarily updating with the nearest previous centroid, you could do a position prediction based upon the velocities in order to try to figure out which of the close objects came from where.
aarthi ramasamy
aarthi ramasamy 2013 年 2 月 22 日
Mr.Roberson,I can get your idea but coding seems to be difficult part here. Do you mind giving me the code? It will be of great use to us like beginners. Kindly help us
Walter Roberson
Walter Roberson 2013 年 2 月 22 日
Yes, I do mind giving you the code, as what you are asking for is a common homework project for computer courses.
aarthi ramasamy
aarthi ramasamy 2013 年 2 月 22 日
Thanks Mr. Roberson for your kind replies
suppy
suppy 2013 年 3 月 3 日
編集済み: suppy 2013 年 3 月 3 日
hi sir.. i am very much new to matlab.. i need the code for moving object tracking and its velocity determination.
Image Analyst
Image Analyst 2015 年 5 月 13 日
suppy ask your own question and I will supply my demo program that tracks a green object in a video.
Thomas Koelen
Thomas Koelen 2015 年 5 月 13 日
Suppy posted this in 2013 Image Analyst:P
Image Analyst
Image Analyst 2015 年 5 月 13 日
Oh - didn't notice. For some reason this showed up on the page of 50 most current postings. Sometimes that can happen if someone (like a spammer) replied and then it got deleted - it will show up on the current list even though the posting that got it there has now been deleted.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by