python - 将具有特定颜色映射的 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
我想知道这项工作有没有更简单的方法。谢谢!
解决方案
我试图考虑更普遍的问题,你可以在每个波段中存在从 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
推荐阅读
- java - 访问 google 电子表格 api 以使用过滤器获取数据
- ios - SwiftUI TextField 未在兄弟视图中更新(带视频)
- excel - 在同一作业中使用不同文件运行 ibm 数据阶段作业
- activeadmin - 在活动管理控制器中设置多租户(Searchkick 索引需要)
- python - 如何确保 Fernet 密钥在初始化时保持不变?
- python - 根据自定义 QTableView 模型中的规则为 int 和 float 单元格着色
- r - 如何通过连接 R 中相邻位置的线的粗细或颜色来可视化地图上的距离矩阵?
- i18next - 如何使用函数调用提取 react-i18next 翻译?
- python - 使用 pyopengl glTexImage2D 时出现 OSError
- c++ - 如何计算 AVL 树中字符串的出现次数?