Skip to content

Mr-TalhaIlyas/Converting-Grayscale-Semantic-Masks-to-Color

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 

Repository files navigation

License: MIT PyPI DOI Downloads Hits

Grayscale to Color Semantic Mask Converotr

This lib converts the grayscale semantic masks obtained at the output a CNN and fills it with colors for example in case of cityscape dataset you have 30 channels at the output of CNN and after using argmax to create one channel semantic mask you get the following output

alt text which you can use for measuring IOU, Dice or other evaluation metrics. But it is a bit difficult for human visualization so this package converts the above output to following ouptut easy to visualize. alt text

Dependencies

numpy
cv2
tensorflow
python >= 3.6

Installation

Pypi

pip install gray2color

Usage

Update

Get matplot color pallets

import matplotlib.pyplot as plt
# get matplot tab10 pallet RGB values
tab10 = np.array(plt.cm.tab10.colors)
# add [0,0,0] at 0th index for BG
tab10 = np.concatenate((np.array([[0,0,0]]), tab10), axis=0)

tab10 = tab10.reshape(1, 11, 3)
# now pass this as custom pallet
import cv2
from gray2color import gray2color

mask = cv2.imread('../gray.png', 0)
rgb = gray2color(mask, use_pallet='cityscape', custom_pallet=None)
## Lambda Function

from gray2color import gray2color
g2c = lambda x : gray2color(x, use_pallet='cityscape',
                            custom_pallet=np.asarray(config['pallet']).reshape(1,-1,3)/255)

Available Pallets

Available Pallets are ade20k, cityscape, lip, pannuke, pascal, vistas

You can also define your custom color Pallets as follows

# values are in order [R, G, B] ranging from [0, 255]

pallet_cityscape = np.array([[[128, 64, 128],
                            [244, 35, 232],
                            [70, 70, 70],
                            [102, 102, 156],
                            [190, 153, 153]]], np.uint8) / 255

Returns

A uint8 image with values ranging from [0, 255] you can save via

import cv2

rgb = cv2.cvtColor(rgb, cv2.COLOR_BGR2RGB)  #   becaues cv2 will change color channels before writing
cv2.imwrite('../rgb.png', rgb)

Raises Errors

PalletNotDefined: if pallet is not specified

NotEnoughColors: if grayscale mask has more classes present than the colors in the pallet