首页 > 解决方案 > 将具有特定颜色映射的 3d 图像转换为具有特定 int 值的 2d 图像

问题描述

对于这个3d 图像,它有 6 个类,它们是:

Impervious surfaces (RGB: 255, 255, 255)
Building (RGB: 0, 0, 255)
Low vegetation (RGB: 0, 255, 255)
Tree (RGB: 0, 255, 0)
Car (RGB: 255, 255, 0)
Clutter/background (RGB: 255, 0, 0)

我想将此图像转换为 2d 图像,其中

Impervious surfaces --> 0
Building --> 1
Low vegetation --> 2
Tree --> 3
Car --> 4
Clutter/background --> 5

我只能想办法使用 for 循环:

im = imageio.imread('kPUoO.png')
w,h = im.shape[:2]
im_ = np.zeros((w,h), dtype=np.uint8)
for i in range(w):
    for j in range(h):
        if list(im[i,j]) == [0,0,255]:
            im_[i,j] = 1
        if list(im[i,j]) == [0,255,255]:
            im_[i,j] = 2
        if list(im[i,j]) == [0,255,0]:
            im_[i,j] = 3
        if list(im[i,j]) == [255,255,0]:
            im_[i,j] = 4
        if list(im[i,j]) == [255,0,0]:
            im_[i,j] = 5

我想知道这项工作有没有更简单的方法。谢谢!

标签: pythonnumpy

解决方案


我试图考虑更普遍的问题,你可以在每个波段中存在从 0 到 255 的任何值,甚至超过 3 个波段......

我们可以通过对每列应用不同的位移来编码 0 和 255 的位置(0、1 和/或 2 列中的 0 为 0 到 3 位,第 0、1 和/或列中的 255 为 4 到 6 位)或 2):

a = (im == 0) << numpy.array([0,1,2], numpy.uint8)
a += (im == 255) << numpy.array([3,4,5], numpy.uint8)

然后沿最后一个轴的总和对类进行唯一编码。除以 7 不是必需的,它只是给出更简单的类标签。

numpy.add.reduce(a, -1) // 7

从那里它是一个标准的 1:1 映射来重新标记类。我认为对于较大的图像或大量图像,这种方法可能会更快。

要查看这是如何工作的:

0,0,0 = 1<<0 + 1<<1 + 1<<2 + 0<<3 + 0<<4 + 0<<5 = 7, /7 = 1
0,0,255 = 1<<0 + 1<<1 + 0<<2 + 0<<3 + 0<<4 + 1<<5 = 35, /7 = 5
0,255,255 = 1<<0 + 0<<1 + 0<<2 + 0<<3 + 1<<4 + 1<<5 = 49, /7 = 7
255,255,255 = 0<<0 + 0<<1 + 0<<2 + 1<<3 + 1<<4 + 1<<5 = 56, /7 = 8
etc...

一个等效的公式是:

a = (im == 0) * numpy.array([1,2,4], numpy.uint8)
a += (im == 255) * numpy.array([8,16,32], numpy.uint8)
numpy.add.reduce(a, -1) //7

推荐阅读