How to convert a Label matrix to colour matrix for image segmentation?(如何将标签矩阵转换为颜色矩阵以进行图像分割?)
问题描述
例如,我有一个 256*256 的标签矩阵.班级是 0-11,所以 12 个班级.我想将标签矩阵转换为颜色矩阵.我试着用这样的代码来做
I have a label matrix of 256*256 for example. And the classes are 0-11 so 12 classes. I want to convert the label matrix to colour matrix. I tried do it in a code like this
`for i in range(256):
for j in range(256):
if x[i][j] == 11:
dummy[i][j] = [255,255,255]
if x[i][j] == 1:
dummy[i][j] = [144,0,0]
if x[i][j] == 2:
dummy[i][j] = [0,255,0]
if x[i][j] == 3:
dummy[i][j] = [0,0,255]
if x[i][j] == 4:
dummy[i][j] = [144,255,0]
if x[i][j] == 5:
dummy[i][j] = [144,0,255]
if x[i][j] == 6:
dummy[i][j] = [0,255,255]
if x[i][j] == 7:
dummy[i][j] = [122,0,0]
if x[i][j] == 8:
dummy[i][j] = [0,122,0]
if x[i][j] == 9:
dummy[i][j] = [0,0,122]
if x[i][j] == 10:
dummy[i][j] = [122,0,122]
if x[i][j] == 11:
dummy[i][j] = [122,122,0]
`
效率极低.PS:x 的形状是 [256 256],dummy 是 [256 256 3].有什么更好的方法吗?
It is highly inefficient. PS: the shape of x is [256 256] and dummy is [256 256 3]. Is there any better way to do it ?
推荐答案
您正在研究索引 RGB 图像 - RGB 图像,其中有固定的颜色调色板",每个像素索引调色板的一种颜色.请参阅此页面了解更多信息.
You are looking into indexed RGB images - an RGB image where you have a fixed "pallet" of colors, each pixel indexes to one of the colors of the pallet. See this page for more information.
from PIL import Image
img = Image.fromarray(x, mode="P")
img.putpalette([
255, 255, 255, # index 0
144, 0, 0, # index 1
0, 255, 0, # index 2
0, 0, 255, # index 3
# ... and so on, you can take it from here.
])
img.show()
这篇关于如何将标签矩阵转换为颜色矩阵以进行图像分割?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将标签矩阵转换为颜色矩阵以进行图像分割


- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01