Bounding box using MATLAB for the image

此生再无相见时 提交于 2019-12-01 09:04:50

Pseduo -

  • Pick largest y, largest x, smallest x, smallest y with in the blob. That is, points on the blob. These are your coordinates that you can use to build the bounding box.

assuming top left of image as (0,0)

(smallestX,smallestY)-----------------(largestX,smallestY)    
      |                                      |
      |                                      |          
      |                                      | 
      |                                      |
(smallestX,largestY)------------------(largestX,largestY)    

And for finding minimum/maximum values and indices.

[r,c]=find(img==min(min(img)))
[r,c]=find(img==max(max(img)))

r,c represent row and column in the img matrix.

  • I have marked the points on your image that you can use to create the bounding box.
  • Zoomed Image to get a better view.

As the 20,000th viewer of this question, I'll answer what I think the asker is actually asking.

To render a rectangle on the page, you just need to properly interpret the bounding box for the shape. You've already computed this in s(idx).BoundingBox

Thus, add these two lines to your script:

bb = s(idx).BoundingBox;
rectangle('Position',[bb(1) bb(2) bb(3) bb(4)],'EdgeColor','green');

and you'll get:

Have you tried regionprops from Image Toolbox?

TreeStar

I think you can try to use bwboundries

boundaries = bwboundaries(blob);

numberOfBoundaries = size(boundaries);

for k = 1 : numberOfBoundaries

    thisBoundary = boundaries{k};

    plot(thisBoundary(:,2), thisBoundary(:,1), 'g', 'LineWidth', 2);

end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!