Finding the centroid of a binary image
3 Comments
Accepted Answer
"I'm only using the first frame of the video"
No, you're using whatever is in grayImage before you run the code above. If before you ran that code you did
grayImage = zeros(10,10);
You'd be using that. You are not using the image created by:
vid_c111=read(v,1); J = imcrop( vid_c111,[766 212 80 150]);
If you intended to use that as the grayImage then you need to move the if test up in your code, before you start using grayImage. I'd move it just after the first two lines, and also add an else:
vid_c111=read(v,1);
J = imcrop( vid_c111,[766 212 80 150]);
if size(J, 3) > 1
grayImage = J(:, :, 3); %only use blue channel
else
grayImage = J;
end
With regards to your actual problem, by convention for binary images the background is the black part, and the object is the white part. The centroid of the big white blob is indeed very close to the image centre.
If you intended to find the centroid of the small black blobs, then you need to invert your binary image before you do any processing. The easiest way to do that is to invert your comparison test:
binaryImage = grayImage <= 150;
More Answers (0)
See Also
Categories
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!