如何将Label矩阵转换为颜色矩阵以进行图像分割? [英] How to convert a Label matrix to colour matrix for image segmentation?
本文介绍了如何将Label矩阵转换为颜色矩阵以进行图像分割?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,我的标签矩阵为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],虚拟值为[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()
这篇关于如何将Label矩阵转换为颜色矩阵以进行图像分割?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文