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

I have some particles that I have identified in a large image, and you need to parse smaller images for each particle. I used the function "BoundingBox" regionprops, but so far have not been successful. How can I now make a rectangular subimage of image I using the BoundingBox? I can use the BoundingBox to draw a rectangle on the original image, but the parameters returned by the BoundingBox do not seem to have pixel dimensions (x, y, width, height), (x1, y1, x2, y2), etc., Which I would expect the border to be returned. I wrote some code examples using coins.png to facilitate understanding. can you help me with this? Thanks!

figure(1); I = imread('coins.png'); bw = im2bw(I, graythresh(I)); bw2 = imfill(bw,'holes'); imshow(bw2); figure(2); L = bwlabel(bw2); imshow(label2rgb(L, @jet, [.7 .7 .7])) figure(3); imshow(I); s = regionprops(L, 'BoundingBox'); rectangle('Position', s(1).BoundingBox); 
+6
source share
2 answers

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

So, 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))); 
+7
source

According to the REGIONPROPS documentation:

BoundingBox - [ul_corner width] , where:

  • ul_corner : is in the form [xyz ...] and indicates the upper left corner of the bounding box

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

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

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

Thus:

 s = regionprops(L, 'BoundingBox'); subImage = imcrop(I, s(1).BoundingBox); imshow(subImage) 
+12
source

Source: https://habr.com/ru/post/900343/


All Articles