Pergunta

By following this post Using ismember with the output of regionprops I am able to selectively isolate the connected component I want. For example using my code below:

img = rgb2gray(imread('W1\Writer1_01_02.jpg'));
bw_normal2 = im2bw(img, graythresh(img));
bw22 = imcomplement(bw_normal2);
bw3 = bwmorph(bw22, 'dilate');
[label2,n2] = bwlabel(bw3);
stats2 = regionprops(label2, {'Area', 'BoundingBox'});
area2 = [stats2.Area];

idx = find((28 <= area2) & (area2 <= 40));
   BW2 = ismember(label2,idx);
figure, imshow(BW2)

I can easily display the output that contains ONLY the connected component whose area is between 28 and 40. As so

enter image description here

But instead can I make a bounding box around this connected component in the original image. I mean if this is the original image: enter image description here

Can I make a bounding box around my desired component on the original image? I know that this is the code for making a bounding box around all of my connected components

imshow(img);
for j=1:n2

    hold on
    rectangle('Position',[stats(j).BoundingBox(1),stats(j).BoundingBox(2),stats(j).BoundingBox(3),stats(j).BoundingBox(4)],...
'EdgeColor','r','LineWidth',2 );
end

But how do I only make a bounding box only around the element having area between 28 and 40? Rather than producing a completely different image as shown above.

Foi útil?

Solução

Keep a if condition in your second half of the code...

imshow(img);
for j=1:n2
 hold on
 area2 = stats2(j).Area;
 if((28 <= area2) & (area2 <= 40))
  rectangle('Position',[stats2(j).BoundingBox(1),stats2(j).BoundingBox(2),stats2(j).BoundingBox(3),stats2(j).BoundingBox(4)],...'EdgeColor','r','LineWidth',2 );
 end
end
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top