我正在尝试查看和编辑分类遮罩,这些遮罩是灰度png,其值等于类ID,即1 = class1,2 = class2等。
Matplotlib颜色图提供了一种从这些整数生成固定调色板的简单方法,可以读取灰度png,应用归一化,应用颜色图,放大255
但是,我该如何后退?我需要读入图像中的RGB值,从颜色图中查找它,并将其转换回原始整数:(0,183,255)= 2
至RGB:
img_src = Image.open(srcpath + srcfn)
im = np.array(img_src) * 20 #[1,2,3] -> [20,40,60]
im = cm(im) #[20,40,60] -> [[0.,0.7196739,1.,1.],...]
im = np.uint8(im * 255) #[[0.,0.7196739,1.,1.],...] -> [[0,183,255],...]
im = Image.fromarray(im)
从RGB:
im = np.array(img_src)
im = im/255 #[[0,183,255],...] -> [[0.,0.7196739,1.,1.],...]
###The missing step
im = uncm(im) #[[0.,0.7196739,1.,1.],...] -> [20,40,60]
im = im/20 #[20,40,60] -> [1,2,3]