How to get a rectangular subimage from regionprops(Image,'BoundingBox') in Matlab?

一曲冷凌霜 提交于 2019-11-30 04:04:42
Jonas

The parameters returned by regionprops are [y,x,width,height] in matrix coordinates (see also "unexpected Matlab".

Thus, to extract the rectangle, you write:

subImage = I(round(s(1).BoundingBox(2):s(1).BoundingBox(2)+s(1).BoundingBox(4)),...
       round(s(1).BoundingBox(1):s(1).BoundingBox(1)+s(1).BoundingBox(3)));

According to the documentation of REGIONPROPS:

BoundingBox is [ul_corner width], where:

  • ul_corner: is in the form [x y z ...] and specifies the upper-left corner of the bounding box

  • width: is in the form [x_width y_width ...] and specifies the width of the bounding box along each dimension

Now you can use IMCROP functions as imcrop(I, rect) where:

rect is a four-element position vector [xmin ymin width height] that specifies the size and position of the crop rectangle.

Thus:

s = regionprops(L, 'BoundingBox');

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