如何在Matlab中从regionprops(Image,'BoundingBox')获取矩形子图像? [英] How to get a rectangular subimage from regionprops(Image,'BoundingBox') in Matlab?

查看:4663
本文介绍了如何在Matlab中从regionprops(Image,'BoundingBox')获取矩形子图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some particles that I've identified in a larger image, and need to parse into smaller images for each particle. I've used the regionprops 'BoundingBox' function, but haven't been successful yet. How can I now make a rectangular subimage of image I using BoundingBox? I can use BoundingBox to draw a rectangle on the original image, but the parameters returned by BoundingBox seem not to be of pixel dimension (x,y, width, height), (x1, y1, x2, y2), etc, which I would expect a bounding box to return. I've written some sample code using coins.png to make it easier for anyone to understand. 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);


推荐答案

regionprops返回的参数是 [y,x,width,height] 矩阵坐标中(另请参阅意外的Matlab

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

这篇关于如何在Matlab中从regionprops(Image,'BoundingBox')获取矩形子图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆