I am trying to view and edit classification masks, which are grayscale pngs with values equal to a class id, i.e. 1 = class1, 2 = class2 etc.
Matplotlib colormaps provide an easy way to generate a fixed palette from these integers, read in the grayscale png, apply a normalization, apply color map, scale up by 255
But, how do I go backward? I need to read in the RGB value in the image, look it up from the color map and convert it back to the original integer: (0,183,255)=2
To 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)
From 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]