3

I'm following this tutorial

The goal is to be able to spit out either: a. the center of each labeled object b. all pixels associated with each labeled object

in a way that I have an array of either 'a.' for each object, or 'b.' for each object

enter image description here

I'm really not sure how to go about this. Are there matlabl tools to help extract these set of pixels or centers - per - label?

Update

I did manage to circle 80% of what I wanted using reigionprops, however it doesn't capture label precisely, just sets a circle around them while capturing the background as well, is that really unavoidable? I'm just not sure how to access the set of pixel per each circled item.

r=regionprops(L, 'All'); imshow(imagergb); areas={r.Area}; Bboxes={r.BoundingBox};
for k=2:numel(r)
    if areas{k}>50 && areas{k} < 1100 
        rectangle('Position',Bboxes{k}, 'LineWidth',1, 'EdgeColor','b', 'Curvature', [1 1]); 
    end
end

So what I'm trying to do is for example.
enter image description here

I thought it might just be

r = regionprops(L, 'PixelIdxList')

then

element1 = r(1).PixelIdxList

but couldn't figure out how to get the position of each pixel

I also tried

Z= bwlabel(L);

but imshow(Z==1) spits out all labels and imshow(Z==2) spits out background, all labels and background. couldn't test bwlabeln since I'm not exactly sure what to enter for r and c arguments.

Community
  • 1
  • 1
Iancovici
  • 5,574
  • 7
  • 39
  • 57
  • 'is that really unavoidable' No, this code was written for a specific purpose in this answer: http://stackoverflow.com/a/22813577/2777181 There are a lot of other things you can do with the output of `regionprops`. – Cape Code Apr 17 '14 at 19:46

1 Answers1

4

Using regionprops(L, 'PixelIdxList') is correct. It gives you lists of pixel indices for each label. You can then convert them to [x,y] coordinates using (for the first label, for example)

[y,x] = ind2sub(size(L), r(1).PixelIdxList)

You can get label centers by using regionprops(L, 'Centroid'). This already gives you [x,y] coordinates for each label. Note that these are subpixel coordinates, so you may need to round them if you want to use them as indices.

buzjwa
  • 2,632
  • 2
  • 24
  • 37
  • Please mention about `Centroid`, so that I could delete mine. I didn't see your answer until I posted it. – Divakar Apr 17 '14 at 19:44
  • No problem :) here you go – buzjwa Apr 17 '14 at 19:50
  • Oh man, that really did it. Kudos to you! P.s had to switch [y,x] to [x,y] – Iancovici Apr 17 '14 at 20:13
  • 2
    In MATLAB the first index refers to rows (y in images) and the second index refers to columns (x in images). Try looking at label 7, for example, which corresponds to an object in the lower left (small x, large y). Look at the values of `x` and `y` and see for yourself which is which. – buzjwa Apr 17 '14 at 20:20