简体   繁体   English

numpy:将图像蒙版与RGB结合以获得彩色图像蒙版

[英]numpy: combine image mask with RGB to get colored image mask

how can I combine a binary mask image array ( this_mask - shape:4,4) with a predefined color array ( mask_color , shape:3)如何将二进制掩码图像数组( this_mask - shape:4,4)与预定义的颜色数组( mask_color ,shape:3)结合起来

this_mask = np.array([
[0,1,0,0],
[0,0,0,0],
[0,0,0,0],
[0,0,0,0],
])
this_mask.shape # (4,4)

mask_color = np.array([128, 128, 64])
mask_color.shape # (3)

to get a new color mask image array ( this_mask_colored , shape:4,4,3)?获得一个新的颜色掩码图像数组( this_mask_colored ,形状:4,4,3)?

this_mask_colored = # do something with `this_mask` and `mask_color`
# [
#  [
#   [0,128,0],
#   [0,0,0],
#   [0,0,0],
#   [0,0,0]
#  ],
#  [
#   [0,128,0],
#   [0,0,0],
#   [0,0,0],
#   [0,0,0]
#  ],
#  [
#   [0,64,0],
#   [0,0,0],
#   [0,0,0],
#   [0,0,0]
#  ],
# ]
this_mask_colored.shape # (4,4,3)

I tried for loop through pixel by pixel, is it slow when when image is 225x225, what is best way to do this?我尝试逐像素循环,当图像为 225x225 时会很慢,最好的方法是什么?

For each image, I have multiple layers of mask, and each mask layer needs to have a different predefine color.对于每张图片,我都有多层蒙版,每个蒙版层需要有不同的预定义颜色。

This might work:这可能有效:

    this_mask = np.array([
        [0,1,0,0],
        [0,0,0,0],
        [0,0,0,0],
        [0,0,0,0],
    ])
    mask_color = np.array([128, 128, 64])

    res = []
    for row in new:
        tmp = []
        for col in row:
            tmp.append(np.array([1,1,1]) * col)
        res.append(np.array(tmp))

    res = res * mask_color

For each entry, 1 will be converted to [1, 1, 1] and 0 is [0, 0, 0]对于每个条目,1 将转换为 [1, 1, 1] 并且 0 是 [0, 0, 0]

I do this because I want to use the benefit of the operation * (element-wise multiplication)我这样做是因为我想利用操作 *(逐元素乘法)的好处

This works:这有效:

    test = np.array([[0, 0, 0],
                     [1, 1, 1],
                     [0, 0, 0],
                     [0, 0, 0]])

    test * np.array([128, 128, 64])

We'll get我们会得到

    array([[  0,   0,   0],
           [128, 128,  64],
           [  0,   0,   0],
           [  0,   0,   0]])

And we want to put all the calculation to the numpy's side.我们想把所有的计算都放在 numpy 这边。 So we loop through the array just for conversion and the rest is for numpy.所以我们循环遍历数组只是为了转换,rest 用于 numpy。

This takes 0.2 secs for 255x255 of 1 with one mask_color and 2 secs for 1000x1000对于 255x255 of 1,使用一个 mask_color 需要 0.2 秒,对于 1000x1000 需要 2 秒

The following function should do what you want.下面的 function 应该做你想做的。


def apply_mask_color(mask, mask_color):
    return np.concatenate(([mask[ ... , np.newaxis] * color for color in mask_color]), axis=2)

Given the following code:给定以下代码:


this_mask = np.array([
    [0,1,0,0],
    [0,0,0,0],
    [0,0,0,0],
    [0,0,0,0],
    ])

mask_color = np.array([128, 128, 64])

applied = apply_mask_color(this_mask, mask_color)
print(applied.shape) #(4, 4, 3)

It is important to note that the output isn't QUITE what you expected.重要的是要注意 output 并不是您所期望的。 Rather, every element inside is now a 3 dimensional array housing the R GB values detailed in mask_color相反,内部的每个元素现在都是一个 3 维数组,其中包含掩码颜色中详述的 R GB 值


print(applied)

Output: Output:


[[[  0   0   0]
  [128 128  64]
  [  0   0   0]
  [  0   0   0]]

 [[  0   0   0]
  [  0   0   0]
  [  0   0   0]
  [  0   0   0]]

 [[  0   0   0]
  [  0   0   0]
  [  0   0   0]
  [  0   0   0]]

 [[  0   0   0]
  [  0   0   0]
  [  0   0   0]
  [  0   0   0]]]

I think is is more what you're looking for.我认为这更多是您正在寻找的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM