【问题标题】:How to convert a Label matrix to colour matrix for image segmentation?如何将标签矩阵转换为颜色矩阵以进行图像分割?
【发布时间】:2019-07-22 12:51:10
【问题描述】:

例如,我有一个 256*256 的标签矩阵。班级是0-11所以12班。我想将标签矩阵转换为颜色矩阵。我尝试在这样的代码中做到这一点

`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]。有没有更好的办法?

【问题讨论】:

    标签: python image-processing computer-vision pytorch image-segmentation


    【解决方案1】:

    您正在查看索引 RGB 图像 - 一个 RGB 图像,其中您有一个固定的颜色“托盘”,每个像素索引到托盘的一种颜色。请参阅this page 了解更多信息。

    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()
    

    【讨论】:

    • is '255, 255, 255' 假设在 [ [255, 255, 255] , [ 255, 255, 0 ] , ... ] 之类的列表中?
    • @FarshidRayhan 请查看答案中的链接
    • @FarshidRayhan https://effbot.org/ 处于中断状态。我们可能会从您的回答中遗漏哪些信息?无论如何,是否有针对此类问题的基于类数的彩虹调色板生成器?
    猜你喜欢
    • 2015-08-18
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 2012-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多