Given the coordinates of a centroid, find the nearby pixels with the same color.

2 views (last 30 days)
I've got an RGB image with many elements, and a matrix with the coordinates of the centroids of those elements. The elements are of the "almost same" color, lets say centroid pixel is RGB(100,100,100) so almost same color would be RGB(100+-20, 100+-20, 100+-20).
Now I want to find the limits of the elements detected, so I was thinking about going 360° around the centroid in steps of 1° and analyze in a ray pattern starting from the center until the color is not similar, so I store that pixel in some array.
In the end, I should get an array with all the border pixels and I can connect all those pixels with a line and delimitate quite well the element borders with a polygon.
I made an image of what I was thinking... but I need some expert help to determine if this is actually possible or there is some other way to accomplish what I'm looking for.
Thank you, and sorry for my bad english!

Answers (2)

Image Analyst
Image Analyst on 17 Nov 2016
Edited: Image Analyst on 17 Nov 2016
You can do this with regionprops() in the Image Processing Toolbox. Just make a binary mask and call bwlabel and regionprops. Untested code:
mask = max(rgbImage, [], 3) > 0;
labeledImage = bwlabel(mask);
props = regionprops(labeledImage, 'Area');
allAreas = [props.Area];
This will get both areas. If you want just one of the areas you can segment out just one based on color, shape, size, or whatever property you want to use and get a mask with just that one shape in it.
See my Image Segmentation Tutorial for a well commented, fancy demo: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862
  2 Comments
Blizki Fernandez
Blizki Fernandez on 17 Nov 2016
Thank you, that really helped!! :D I was overthinking things! bwlabel and regionprops did the trick. Cheers.
PD: You got a lot of nice work in there!

Sign in to comment.


Greg Dionne
Greg Dionne on 17 Nov 2016

Community Treasure Hunt

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

Start Hunting!