How to find the center co-ordinates of different regions

Hi there,
Can anyone assist me how to find the center co-ordinates automatically and save the co-ordinate (x and y position) values in an array automatically? The shapes are different for the regions but each region has same pixel value.
Please have a look at my image and guide me how to find the center co-ordinates of each region and save them in an array automatically without using manual operation.
Any help will be highly appreciated.

 Réponse acceptée

Image Analyst
Image Analyst le 11 Mai 2015
Modifié(e) : Image Analyst le 11 Mai 2015
I thought you'd seen it before, but I guess not. See my image segmentation tutorial in my File Exchange http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862
Basically it's something like
grayImage = rgb2gray(rgbImage);
[labeledImage, numberOfBlobs] = bwlabel(grayImage ~= 0);
measurements = regionprops(labeledImage, 'Centroid');
allCentroids = [measurements.Centroid];
xCentroids = allCentroids(1:2:end);
yCentroids = allCentroids(2:2:end);

7 commentaires

Tania
Tania le 11 Mai 2015
Modifié(e) : Tania le 11 Mai 2015
Thank you very much however; I am only getting 4 co-ordinates from the image. No clue why is that like this:
xCentroids =
96.0000 228.5272 362.5419 415.0248
yCentroids =
61.0000 120.2121 206.5058 70.5421
I have tried the other way using
s = regionprops(BW,'centroid');
centroids = cat(1,s.Centroid)
centroids =
96.0000 61.0000
228.5272 120.2121
362.5419 206.5058
415.0248 70.5421
Could you please tell me why it is giving me only 4 co-ordinates instead of 6?
Thank you in advance.
Guillaume
Guillaume le 11 Mai 2015
Modifié(e) : Guillaume le 11 Mai 2015
I only count 5 shapes in your image.
In any case, check that your binary image has indeed 5 (or 6) regions in it. Most likely, your segmentation has gone wrong.
Tania
Tania le 11 Mai 2015
Modifié(e) : Tania le 11 Mai 2015
Now I am getting 5 co-ordinates. Should not I get 6 (should not I consider black region as well) instead of 5?
centroids =
98.5119 81.5119
167.4810 263.8270
230.5137 121.0732
364.5173 208.5051
417.0518 72.5668
Not sure what I have done wrong here.
Please advice me. Thanks
You didn't follow my recommendations, that's what. I said to do this:
grayImage = rgb2gray(rgbImage);
I did not say to use im2double() or im2bw(). Also, do not use image as the name of your filename string. image is the name of a built-in function. Call it fullFileName or something like that.
Tania
Tania le 11 Mai 2015
Modifié(e) : Tania le 11 Mai 2015
I have figured it out to get 5 regions so far. I can not figure out how to get the black one.
If you can advice me then it would be great.
I appreciate your time and help.
Thank you very much.
If you want to treat the black background as its own object, then you'll have to do that separately (well, that's the simplest and most straightforward way).
grayImage = rgb2gray(rgbImage);
[labeledImage, numberOfBlobs] = bwlabel(grayImage == 0);
measurements = regionprops(labeledImage, 'Centroid');
allCentroids = [measurements.Centroid];
xCentroids = allCentroids(1:2:end);
yCentroids = allCentroids(2:2:end);
Note the == 0 instead of ~= 0 so that we pick up only pure black pixels.
Thank you very much. That solved my problem.
Great help

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by