使用BoundingBox生成的特定区域

问题描述:

我有 n 区域nofollow> regionprops 'BoundingBox''Image'属性。我将数据( [x,y,Δx,Δy] )从特定区域(例如14)保存到 bb1 我想继续工作,但首先我需要了解以下代码:

I have n regions generated by regionprops with 'BoundingBox' and 'Image' properties. I saved the data ([x, y, Δx, Δy]) from a specific region (14 for example) to bb1 that I want to work on but first I need to understand the following code:

bb1=floor( Ic(14,1).BoundingBox );
I1bb1=I1( bb1(2):bb1(2)+bb1(4)-1 , bb1(1):bb1(1)+bb1(3)-1 ,:);

之后,我也想了解下一个来自同一个例子的代码:

After that, also I want to understand the next code that it is from the same example:

I2=I1bb1.*repmat(  Ic(BB,1).Image , [1 1 3]);

其中 Ic 包含 n BoundingBox生成的区域

您有一个3D变量 I1 (我猜RGB图像)并且您希望从中裁剪边界框 Ic(14,1).BoundingBox 。也就是说,有一个较小的3D数组,对应于图像 I1 中的边界框内的像素。要做这个裁剪你有这些命令:

You have a 3D variable I1 (I guess RGB image) and you wish to crop the boundingbox Ic(14,1).BoundingBox from it. That is, have a smaller 3D array that corresponds to the pixels inside the bounding box in image I1. To do this cropping you have these commands:

bb1=floor( Ic(14,1).BoundingBox );

首先,确保值 [x,y,w,h边界框的是整数而不是子像素,因此您可以使用这些值进行索引。然后,您可以使用边界框索引到 I1

First, you make sure the values [x, y, w, h] of the bounding box are integers and not sub-pixels so that you can use these values for indexing. Then, you can index into I1 with the bounding box:

I1bb1=I1( bb1(2):bb1(2)+bb1(4)-1 , bb1(1):bb1(1)+bb1(3)-1 ,:);

对于 I1bb1 的行,你需要从 bb1(2) y 值)到 y + w-1的行这是 bb1(2)+ bb1(4)-1 。从 x x + h-1 的列相同。

For the rows of I1bb1 you take the rows from bb1(2) (y value) to y+w-1 which is bb1(2)+bb1(4)-1. Same for the columns from x to x+h-1.

对于第三行代码,您拥有属性 Ic(14,1).Image 。此属性是

For the third line of code, you have the property Ic(14,1).Image. This property is


返回与区域边界框大小相同的二进制图像(逻辑)。 on像素对应于区域,所有其他像素都关闭。

Returns a binary image (logical) of the same size as the bounding box of the region. The on pixels correspond to the region, and all other pixels are off.

现在你想要 I2 与边界框大小相同,但边界框中不属于该对象的所有像素都设置为 [0 0 0] (所有RGB值零)。因此,您将 Ic(14,1).Image 从2D二进制掩码转换为三个通道的3D掩码:

Now you want I2 to be the same size as the bounding box, but all pixels in the boundingbox not belonging to the object are set to [0 0 0] (all RGB values zero). Thus you convert Ic(14,1).Image from 2D binary mask to a 3D mask over three channels:

repmat(  Ic(BB,1).Image , [1 1 3])

最后,你逐元素地将膨胀面具与裁剪后的图像相乘 I1bb1

Finally you element-wise multiply the "inflated" mask with the cropped image I1bb1:

I2=I1bb1.*repmat(  Ic(BB,1).Image , [1 1 3]);






您可以获得相同的结果,且更具可读性代码:


You can achieve the same results with slightly more readable code:

Ibb1 = imcrop( I1, bb1 ); %// crop a bounding box from image 
I2 = bsxfun( @times, Ibb1, Ic(14,1).Image ); %// no need for repmat