【问题标题】:numpy: combine image mask with RGB to get colored image masknumpy:将图像蒙版与 RGB 结合以获得彩色图像蒙版
【发布时间】:2019-10-16 15:19:14
【问题描述】:

如何将二进制蒙版图像数组 (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)

要获得一个新的彩色蒙版图像数组(this_mask_colored, shape: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)

我尝试逐像素循环,当图像为 225x225 时会很慢,最好的方法是什么?

对于每张图片,我都有多层蒙版,每个蒙版层需要有不同的预定义颜色。

【问题讨论】:

    标签: python numpy image-processing


    【解决方案1】:

    这可能有效:

        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
    

    对于每个条目,1 将转换为 [1, 1, 1],0 为 [0, 0, 0]

    我这样做是因为我想利用运算 *(逐元素乘法)的好处

    这行得通:

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

    我们会得到

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

    我们想把所有的计算都交给 numpy。所以我们循环遍历数组只是为了转换,其余的都是为了numpy。

    对于 255x255 of 1,使用一个 mask_color 需要 0.2 秒,对于 1000x1000 需要 2 秒

    【讨论】:

      【解决方案2】:

      下面的函数应该做你想做的。

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

      给定以下代码:

      
      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)
      
      

      请务必注意,输出并非您所期望的。相反,内部的每个元素现在都是一个 3 维数组,其中包含 mask_color 中详述的 R G B 值

      
      print(applied)
      
      

      输出:

      
      [[[  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]]]
      
      

      我认为更多的是您正在寻找的。​​p>

      【讨论】:

      • 谢谢!这行得通。我验证了: import matplotlib.pyplot as plt plt.imshow(applied) plt.show()
      猜你喜欢
      • 2015-07-09
      • 2012-05-15
      • 2016-02-27
      • 1970-01-01
      • 2023-03-04
      • 1970-01-01
      • 2020-12-01
      • 2021-08-24
      相关资源
      最近更新 更多