Reputation: 53
I'm doing some low level image processing project and I need to locate the position of an object. In this case it's a comet. I played a bit with the thresholds and got a pretty much binary image, but what I need next is to locate a fixed point in the middle of the large group of white pixels so it can be traced or something. Any idea on how to do that? Here's a screen shot of what I got.
And is there a way to filter out all the white dots all over the picture? Some kind of function to give all small groups value of "0"? Thanks in advance!
Upvotes: 2
Views: 2058
Reputation: 221674
Maybe you are looking for this -
Code
%%// Read in image and convert to binary
img = imread(IMAGE_FILEPATH);
BW = im2bw(img);
%%// Get only the comet blob, which is the biggest blob
[L, num] = bwlabel(BW);
counts = sum(bsxfun(@eq,L(:),1:num));
[~,ind] = max(counts);
BW = (L==ind);
%%// Find the centroid of the comet blob
stats = regionprops(BW, 'Centroid');
center_point = stats.Centroid
Output
center_point =
56.7471 131.9373
Note: There's another question - Select largest object in an image that relates to this case.
Upvotes: 3
Reputation: 10682
you can use a median filter or a morphological opening filter to remove small white regions. you'll have to experiment and pick the right size for the filter kernel. assuming you have a cleaner image after filtering, you can perform a labeling and then do a regionprops to get the centroid of the blob.
Upvotes: 0
Reputation: 3526
Your problem seems to be solved with regionprops
, as others have proposed already, but if you're interested, in general, in eliminating small white artefacts in an image:
You could apply one or multiple steps of erosion to your binary image, eliminating small white spots (but also decreasing the size of your comet!).
Upvotes: 1
Reputation: 699
MATLAB regionprops is your friend, and your image is good enough for what you want to do.
regionprops does just what you need (you get the X,Y of the center of mass). Since it also gives the area, you can filter results and only retain the largest object in your image.
Upvotes: 2