Centroid of alot of images in a single run?

1 view (last 30 days)
A have 100 images(frames) of an object moving in a video. I know how to find centroid of a single image. But when I try a loop to find centroid of these 100 images, it doesn't work. Help please!
  3 Comments
Geoff Hayes
Geoff Hayes on 14 Nov 2014
Hamza - what have you tried for looping over the 100 images? When you say it doesn't work, what exactly is the problem? Please post your code and describe the errors or unexpected behaviour that you have observed.
Hamza Ahmed
Hamza Ahmed on 15 Nov 2014
Edited: Geoff Hayes on 15 Nov 2014
Geoff - this is the portion of code which is giving me trouble:
%Cropping all the pictures and saving them (again in working directory)
for ii = 1:length(sortedImageNames)
img = imread(fullfile(workingDir,'images',sortedImageNames{ii}));
imgc = imcrop(img, rect);
%Attempt to find centroid of each cropped image.
Ibw = im2bw(imgc);
Ibw = imfill(Ibw,'holes');
Ilabel = bwlabel(Ibw);
stat = regionprops(~Ilabel,'centroid');
imshow(imgc); hold on;
for x = 1: numel(stat)
plot(stat(x).Centroid(1),stat(x).Centroid(2),'ro');
end
% Write out to a JPEG file
imwrite(imgc,fullfile(workingDir,'images',sprintf('imgc%d.jpg',ii)));
end
Problem: This just gives me all the cropped frames from the video in my working directory. I wanted the centroid to show on every single cropped image. Can you kindly see where i'm making the error? Thanks

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 15 Nov 2014
That won't work. You're not finding the centroid of the entire frame, but of all the various objects in the frame. I have no idea what I2 is - what is it? What I would do is
grayImage = read(videoObject, frameNumber);
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
grayImage = rgb2gray(grayImage);
end
mask = true(rows, columns);
measurements = regionprops(logical(mask), grayImage, 'WeightedCentroid');
This will give you the centroid of the entire frame instead of just whatever blobs happened to get thresholded.
  3 Comments
Hamza Ahmed
Hamza Ahmed on 15 Nov 2014
And I see why you thought I wanted centroid of the entire image, it was mentioned in my question like that. I was actually thinking of the centroid of the moving object in that image, my bad.
Image Analyst
Image Analyst on 15 Nov 2014
For a series of separate images, see the FAQ for sample code snippets: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F
For a video file, see my attached demo file.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!