title: How to create a bounding box around objects selected by ismember?
body: <p>By following this post <a href="http://blogs.mathworks.com/steve/2009/02/27/using-ismember-with-the-output-of-regionprops/" rel="nofollow noreferrer">Using ismember with the output of regionprops</a> I am able to selectively isolate the connected component I want. For example using my code below:</p>
  
  <pre><code>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 &lt;= area2) &amp; (area2 &lt;= 40));
     BW2 = ismember(label2,idx);
  figure, imshow(BW2)
  </code></pre>
  
  <p>I can easily display the output that contains ONLY the connected component whose area is between 28 and 40. As so</p>
  
  <p><img src="https://i.stack.imgur.com/yFj2Q.jpg" alt="enter image description here"></p>
  
  <p>But instead can I make a bounding box around this connected component in the original image. I mean if this is the original image:
  <img src="https://i.stack.imgur.com/XWlVr.jpg" alt="enter image description here"></p>
  
  <p>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</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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.</p>