本文介绍了使用 numpy 将二进制掩码应用于 RGB 图像的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 numpy 将二进制掩码应用于 RGB 图像

I'm trying to apply a binary mask to an RGB image with numpy

我发现了这个https://stackoverflow.com/a/26843467/4628384,但我没有权限写评论呢.无论如何,我遇到了问题;非常感谢任何帮助.

I found this https://stackoverflow.com/a/26843467/4628384 but I don't have permissions to write a comment yet.Anyway I'm running into a problem; Any help much appreciated.

def masktoRGB(self,image,image_mask):

        # create mask with same dimensions as image
        mask = np.zeros_like(image)
        # copy your image_mask to all dimensions (i.e. colors) of your image
        for i in range(3):
            mask[:,:,i] = image_mask.copy()

        # apply the mask to your image

        # tried to swap axes, not a solution 
        #image = image.swapaxes(0,1)
        #this gives the error:
        masked_image = image[mask]

        print(mask.shape)
        print(image.shape)
        print(image_mask.shape)



        return masked_image

这给了我:

IndexError: 索引 213 超出轴 0 的边界,大小为 212

IndexError: index 213 is out of bounds for axis 0 with size 212

打印输出:

(188, 212, 3)(188, 212, 3)(188, 212)

(188, 212, 3)(188, 212, 3)(188, 212)

image 和 image_mask 是同一张图片,除了第一个是 RGB,第二个是模式 L

image and image_mask are the same image, except the first is RGB and the second is mode L

推荐答案

尝试使用广播和乘法:

image * image_mask[..., None]

我假设 image_mask 的类型是映射到数字 0 和 1 的 bool.因此,图像和掩码的成对乘法会将掩码值设置为零.

I assume that the type of image_mask is bool that maps to numbers 0 and 1. Therefore pairwise multiplication of image and mask will set masked values to zero.

使用 np.where() 或 & 可以实现类似的效果.操作员.

Similar efect can be achieved with np.where() or & operator.

问题是 image 和 image_mask 的形状不兼容.Numpy 将首先在形状的头部添加额外的维度,直到两个张量具有相同的形状.因此 image_mask 被从 (188, 212) 重塑为 (1,188, 212).这个新形状与图像的形状不兼容(188, 212, 3).

The issue is that shapes of image and image_mask are not compatible. Numpy will first add extra dimensions at the head of shape until both tensor have the same shape. Thus image_mask is reshaped from (188, 212) to (1,188, 212). This new shape is not compatible with the shape of image(188, 212, 3).

诀窍是使用花哨的索引来重塑 image_mask.您可以使用 None 作为索引在形状的末尾添加大小为 1 的虚拟维度.操作 image_mask[..., None] 会将其从 (188, 212) 重塑为 (188, 212,1).

The trick is to reshape image_mask using fancy indexing. You can use None as index to add a dummy dimension of size 1 at the end of shape. Operation image_mask[..., None] will reshape it from (188, 212) to (188, 212,1).

广播规则告诉大小 1 的维度是通过沿广播维度重复所有值来扩展的.因此 numoy 会自动将张量从 (188, 212,1) 重塑为 (188, 212,3).操作非常快,因为没有创建副本.

Broadcast rules tells that dimensions of size 1 are expanded by repeating all values along broadcast dimension. Thus numoy will automatically reshape tensir from (188, 212,1) to (188, 212,3). Operation is very fast because no copy is created.

现在可以将位张量相乘以产生所需的结果.

Now bith tensors can be multiplied producing desired result.

这篇关于使用 numpy 将二进制掩码应用于 RGB 图像的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 05:41