0

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]
user15741
  • 1,392
  • 16
  • 27
  • 2
    I think this is the same as [How to decode color mapping in matplotlib's Colormap?](https://stackoverflow.com/questions/45177154/how-to-decode-color-mapping-in-matplotlibs-colormap/45178154#45178154). – ImportanceOfBeingErnest Apr 13 '19 at 09:46
  • That is a good way to extract the palette for values, is there a way to replace the RGBA dimension in a tensor with an integer one based on palette lookup? – user15741 Apr 15 '19 at 15:19

0 Answers0