1

I have a set of shapes in an image I would like to label according to their area, I have used bwboundaries to find them, and regionprops to determine their area. I would like to label them such that they are labelled different based on whether their area is above or below the threshold i have determined.

I've thought about using inserObjectAnnotation, but I'm not sure how to add on a condition based on their area into the function?

Divakar
  • 218,885
  • 19
  • 262
  • 358
KRS-fun
  • 696
  • 3
  • 9
  • 20

2 Answers2

1

Assuming TH to be the threshold area and BW to be the binary image and if you are okay with labeling them as o's and x's with matlab figure text at their centers (centroids to be exact), based on the thresholding, see if this satisfies your needs -

stats = regionprops(BW,'Area')
stats2 = regionprops(BW,'Centroid')

figure,imshow(BW)
for k = 1:numel(stats)
    xy = stats2(k).Centroid
    if (stats(k).Area>TH)
        text(xy(1),xy(2),'L') %// Large Shape
    else
        text(xy(1),xy(2),'S') %// Small Shape
    end
end

Sample output -

enter image description here

Divakar
  • 218,885
  • 19
  • 262
  • 358
  • It works! But unfortunately my shapes are a little too small to include the letters clearly, is there anyway to colour them in instead of inserting text? – KRS-fun Jul 16 '14 at 15:22
  • @KRS-fun Color the shapes you mean, i.e. fill shapes with some color? – Divakar Jul 16 '14 at 15:25
  • @KRS-fun So in the output image we would have two colored shapes only, one color for smaller than threshold area shapes and rest of the shapes would be filled with the second color? – Divakar Jul 16 '14 at 15:34
  • I've already ticked your answer as you've answered my original question – KRS-fun Jul 16 '14 at 15:35
0

You could use CC = bwconncomp(BW,conn).

To get the number of pixels of every connected compontent you can use:

numPixels = cellfun(@numel,CC.PixelIdxList);

In CC.PixelIdxList you have a list of all found objects and the indices of the pixels belonging to the components. I guess to label your areas you could do something like:

for ind = 1:size(CC.PixelIdxList,2)
   Image(CC.PixelIdxList{ind}) = ind;
end
nightlyop
  • 7,675
  • 5
  • 27
  • 36
  • By labeling, OP probably means annotating, that's why the mention of `insertobjectannotation` – Divakar Jul 16 '14 at 13:21
  • Ok, sounds correct. I understood something else with labeling because i didn't know `insertobjectannotation` (which i don't have in Matlab 2012a). – nightlyop Jul 16 '14 at 13:26
  • yeah I understood the samething you did, until I saw that longish term. In fact you can look into its online documentation - http://www.mathworks.in/help/vision/ref/insertobjectannotation.html – Divakar Jul 16 '14 at 13:27