How can I find the centroid of an moving object in a video (all of its centroids..and put them in a vector)?

7 views (last 30 days)
%%Read Video
videoReader = vision.VideoFileReader('BA.avi');
%%Create Video Player
videoPlayer = vision.VideoPlayer;
%%Create Foreground Detector (Background Subtraction)
foregroundDetector = vision.ForegroundDetector('NumGaussians', 3,'NumTrainingFrames', 100);
%%Run on first 100 frames to learn background
for i = 1:75
videoFrame = step(videoReader);
foreground = step(foregroundDetector,videoFrame);
% figure, imshow(I);
end
% display 75th frame and foreground image
while ~isDone(videoReader)
%Get the next frame
videoFrame = step(videoReader);
%Detect foreground pixels
foreground = step(foregroundDetector,videoFrame);
cc = bwconncomp(foreground);
stats = regionprops(cc, 'Area');
idx = find([stats.Area] > 50);
foreground2 = ismember(labelmatrix(cc), idx);
s = regionprops(foreground2, 'centroid');
centroids = cat(1, s.Centroid);
mycentroid=[00.00,00.00];
mycentroid = cat(1,centroids,mycentroid);
%figure, imshow(foreground2);
end

Answers (1)

Image Analyst
Image Analyst on 17 May 2015
See my attached demo which does just that. It tracks the two green plastic parts of a green sharpie marker.

Community Treasure Hunt

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

Start Hunting!