如何将一组图像中的像素(R,G,B)映射到不同的pixelcolorvalue索引?

2024-09-30 08:21:07 发布

您现在位置:Python中文网/ 问答频道 /正文

假设有600个注释语义分割掩码图像,其中包含10种不同的颜色,每个颜色代表一个实体。这些图像是一个numpy数组的形状(600,3,72,96),其中n=600,3=RGB通道,72=高度,96=宽度。在

如何将numpy数组中的每个RGB像素映射到一个颜色索引值?例如,一个颜色列表将是[(128,128,0),(240,128,0),…n],并且numpy数组中的所有(240,128,0)像素都将转换为唯一映射(=1)中的索引值。在

如何以更少的代码高效地完成这项工作?我想出了一个解决方案,但速度很慢。在

# Input imgs.shape = (N, 3, H, W), where (N = count, W = width, H = height)
def unique_map_pixels(imgs):
  original_shape = imgs.shape

  # imgs.shape = (N, H, W, 3)
  imgs = imgs.transpose(0, 2, 3, 1)

  # tupleview.shape = (N, H, W, 1); contains tuples [(R, G, B), (R, G, B)]
  tupleview = imgs.reshape(-1, 3).view(imgs.dtype.descr * imgs.shape[3])

  # get unique pixel values in images, [(R, G, B), ...]
  uniques = list(np.unique(tupleview))

  # map uniques into hashed list ({"RXBXG": 0, "RXBXG": 1}, ...)
  uniqmap = {}
  idx = 0
  for x in uniques:
    uniqmap["%sX%sX%s" % (x[0], x[1], x[2])] = idx
    idx = idx + 1
    if idx >= np.iinfo(np.uint16).max:
      raise Exception("Can handle only %s distinct colors" % np.iinfo(np.uint16).max)

  # imgs1d.shape = (N), contains RGB tuples
  imgs1d = tupleview.reshape(np.prod(tupleview.shape))

  # imgsmapped.shape = (N), contains uniques-index values
  imgsmapped = np.empty((len(imgs1d))).astype(np.uint16)

  # map each pixel into unique-pixel-ID
  idx = 0
  for x in imgs1d:
    str = ("%sX%sX%s" % (x[0], x[1] ,x[2]))
    imgsmapped[idx] = uniqmap[str]
    idx = idx + 1

  imgsmapped.shape = (original_shape[0], original_shape[2], original_shape[3]) # (N, H, W)
  return (imgsmapped, uniques)

测试:

^{pr2}$

Tags: numpymap颜色nprgb数组uniqueshape
2条回答

我有三个频道的图像。我有3个通道的像素值,如果一个像素在它的3个通道中有这3个值,那么它就属于a类。 基本上,我想生成一个通道数组,每个通道中的每个类都是分开的。 这是可以做到的

seg_channel = np.zeros((image.shape[0], image.shape[1], num_classes))
pixel_class_dict={'1': [128, 64, 128]. '2': [230, 50, 140]} #num_classes=2
for channel in range(num_classes):
    pixel_value= pixel_class_dict[str(channel)]
    for i in range(image.shape[0]):
        for j in range(image.shape[1]):
            if list(image[i][j])==pixel_value:
                classes_channel[i,j,channel]=1

还有另一种方法可以有效地做到这一点

^{pr2}$

这里有一个简洁的矢量化方法,没有那些错误检查-

def unique_map_pixels_vectorized(imgs):
    N,H,W = len(imgs), imgs.shape[2], imgs.shape[3]
    img2D = imgs.transpose(0, 2, 3, 1).reshape(-1,3)
    ID = np.ravel_multi_index(img2D.T,img2D.max(0)+1)
    _, firstidx, tags = np.unique(ID,return_index=True,return_inverse=True)
    return tags.reshape(N,H,W), img2D[firstidx]

运行时测试和验证-

^{pr2}$

相关问题 更多 >

    热门问题