首页 文章

如何从Matlab中的regionprops(Image,'BoundingBox')获取矩形子图像?

提问于
浏览
6

我有一些粒子,我已经在一个更大的图像中识别出来,需要为每个粒子解析成更小的图像 . 我已经使用了regionprops的“BoundingBox”函数,但还没有成功 . 我现在如何使用BoundingBox制作图像的矩形子图像?我可以使用BoundingBox在原始图像上绘制一个矩形,但BoundingBox返回的参数似乎不是像素尺寸(x,y,宽度,高度),(x1,y1,x2,y2)等,我期望一个边界框返回 . 我已经使用coins.png编写了一些示例代码,以方便任何人理解 . 你能帮帮我吗?谢谢!

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);

2 回答

  • 7

    regionprops返回的参数在矩阵坐标中为 [y,x,width,height] (另请参见"unexpected Matlab" .

    因此,要提取矩形,您可以写:

    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)));
    
  • 13

    根据REGIONPROPS的文件:

    BoundingBox是[ul_corner width],其中:ul_corner:格式为[xyz ...]并指定边界框宽度的左上角:格式为[x_width y_width ...]并指定宽度每个维度的边界框

    现在你可以使用IMCROP函数作为 imcrop(I, rect) ,其中:

    rect是四元素位置向量[xmin ymin width height],指定裁剪矩形的大小和位置 .

    从而:

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

相关问题