【问题标题】:Map scalars to arrays based on values : Image Processing 2D to 3D - NumPy / Python根据值将标量映射到数组:图像处理 2D 到 3D - NumPy / Python
【发布时间】:2020-08-22 22:12:00
【问题描述】:

给定一个形状为 (height, width) 的 Numpy 矩阵,我正在寻找创建另一个形状为 (height, width, 4) 的 Numpy 矩阵的最快方法,其中 4 代表 RGBA 值。我想做这个基于价值的;因此,对于第一个矩阵中的所有 0 值,我希望在第二个矩阵中的同一位置有一个值 [255, 255, 255, 0]

我想用 NumPy 做到这一点,而不需要像下面这样缓慢地迭代:

for i in range(0, height):
    for j in range(0, width):
        if image[i][j] = 0:
            new_image[i][j] = [255, 255, 255, 0]
        elif image[i][j] = 1:
            new_image[i][j] = [0, 255, 0, 0.5]

如您所见,我正在创建一个矩阵,其中值 0 变为透明白色,1 变为绿色,alpha 为 0.5;有更快的 NumPy 解决方案吗?

我猜numpy.where 应该会极大地帮助加快这个过程,但我还没有弄清楚多重和多重价值转换的正确实现。

【问题讨论】:

    标签: python numpy matrix vectorization rgba


    【解决方案1】:

    你是对的np.where 是你解决这个问题的方法。矢量化函数在哪里,所以它应该比您的解决方案快得多。

    我在这里假设它没有我知道的 elif,但您可以通过嵌套 where 语句来解决这个问题。

     new_image = np.where(
               image == 0,
               [255, 255, 255, 0],
               np.where(
                    image == 1,
                    [0, 255, 0, 0.5],
                    np.nan
               )
          )
    

    【讨论】:

      【解决方案2】:

      为了更简洁的解决方案,尤其是在使用多个标签时,我们可以使用np.searchsorted 来追溯映射的值,就像这样 -

      # Edit to include more labels and values here            
      label_ar = np.array([0,1]) # sorted label array
      val_ar = np.array([[255, 255, 255, 0],[0, 255, 0, 0.5]])
      
      # Get output array
      out = val_ar[np.searchsorted(label_ar, image)]
      

      请注意,这假定来自image 的所有唯一标签都在label_ar 中。

      所以,现在假设我们在image 中还有两个标签23,类似这样 -

      for i in range(0, height):
          for j in range(0, width):
              if image[i,j] == 0:
                  new_image[i,j] = [255, 255, 255, 0]
              elif image[i,j] == 1:
                  new_image[i,j] = [0, 255, 0, 0.5]
              elif image[i,j] == 2:
                  new_image[i,j] = [0, 255, 255, 0.5]
              elif image[i,j] == 3:
                  new_image[i,j] = [255, 255, 255, 0.5]
      

      我们将相应地编辑标签和值并使用相同的searchsorted 解决方案-

      label_ar = np.array([0,1,2,3]) # sorted label array
      val_ar = np.array([
          [255, 255, 255, 0],
          [0, 255, 0, 0.5],
          [0, 255, 255, 0.5],
          [255, 255, 255, 0.5]])
      

      【讨论】:

      • 感谢 Divakar 提供的帮助。这是一个快速的解决方案,正是我想要的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-31
      • 1970-01-01
      • 2016-04-07
      • 2012-12-09
      • 1970-01-01
      • 2022-10-04
      相关资源
      最近更新 更多