将多类分割灰度标签图转RGB三通道图

***************************************************

码字不易,收藏之余,别忘了给我点个赞吧!

***************************************************

---------Start

灰度标签图:

0,1,2,3,4分别代表:background,car,wheel,lights,window五个类别。显示全黑。
在这里插入图片描述

代码:

prediction代表读入的标签numpy数组(灰度图读入)

		prediction = cv.imread('test.png',flag=0)
        a1 = copy.deepcopy(prediction)
        a2 = copy.deepcopy(prediction)
        a3 = copy.deepcopy(prediction)

        a1[a1 == 1] = 255
        a1[a1 == 2] = 0
        a1[a1 == 3] = 255
        a1[a1 == 4] = 20

        a2[a2 == 1] = 255
        a2[a2 == 2] = 255
        a2[a2 == 3] = 0
        a2[a2 == 4] = 10

        a3[a3 == 1] = 255
        a3[a3 == 2] = 77
        a3[a3 == 3] = 0
        a3[a3 == 4] = 120

        a1 = Image.fromarray(np.uint8(a1)).convert('L')
        a2 = Image.fromarray(np.uint8(a2)).convert('L')
        a3 = Image.fromarray(np.uint8(a3)).convert('L')
        prediction = Image.merge('RGB', [a1, a2, a3])
        prediction.save('test2.png')

转换的RGB图片:

通过手动设置三通道的RGB值,实现(0,1,2,3,4)到五种RGB颜色的转换。
在这里插入图片描述
至此,设置完毕!

猜你喜欢

转载自blog.csdn.net/qq_37652891/article/details/123474460