MATLAB: Find coordinates xy of a ball in an image and track it by its centre coordinates

@image analystbackgroundballblackImage Processing Toolboximage segmentationobjectraspberry pitrackingurgentwhite

I am using the raspberry pi camera to capture the images and I have to find out the xy coordinates of a ball within the image and the by finding the centre of mass to track it in multiple images.
It is a white ball and the background is black. So did thresholding of the image so that i will detect only the white feature and erase the background. I aslo converted the image in binary so that only 1 and 0 will appear.
I also read somewhere that if i call the code following code: [rows,cols] = find(binaryImage);
i would be able to detects in an array where the 1s are.
So how i am suppose to show it in the image? How i will find the centre of mass coordinates?

Best Answer

See my Image Segmentation Tutorial at my File Exchange
Basically you said you've already thresholded your image to create the binary image. so now you just call regionprops():
% Threshold this frame of the video.
binaryImage = grayImage > someThreshold;
binaryImage = bwareafilt(binaryImage, 1); % Take the largest blob ONLY to get rid of noise.
props = regionprops(binaryImage, 'Centroid');
% Now if you're on frame #k of the video
xCentroid(k) = props.Centroid(1);
yCentroid(k) = props.Centroid(2);