In this video we will Work with RGB (and Hex) Masks for Semantic Segmentation

RGB to HEX: (Hexadecimel – base 16)

This number divided by sixteen (integer division; ignoring any remainder) gives

the first hexadecimal digit (between 0 and F, where the letters A to F represent

the numbers 10 to 15). The remainder gives the second hexadecimal digit.

0-9 – 0-9

10-15 – A-F

Example: RGB – R=201, G=, B=

R = 201/16 = 12 with remainder of 9. So hex code for R is C9 (remember C=12)

Calculating RGB from HEX: #3C1098

3C = 3*16 + 12 = 60

10 = 1*16 + 0 = 16

90 = 9*16 + 8 = 152

###############################

#CODE

#########################

#Convert Hex to RGB (numpy array)

Building = ‘#3C1098 ‘.lstrip(’#’)

Building = np.array(tuple(int(Building[i:i+2], 16) for i in (0, 2, 4))) ## 60, 16, 152

Land = ‘#8429F6 ‘.lstrip(’#’)

Land = np.array(tuple(int(Land[i:i+2], 16) for i in (0, 2, 4))) #132 , 41, 246

Road = ‘#6EC1E4 ‘.lstrip(’#’)

Road = np.array(tuple(int(Road[i:i+2], 16) for i in (0, 2, 4))) #110 , 193, 228

Vegetation = ‘FEDD3A’.lstrip(‘#’)

Vegetation = np.array(tuple(int(Vegetation[i:i+2], 16) for i in (0, 2, 4))) #254 , 221, 58

Water = ‘E2A929’.lstrip(‘#’)

Water = np.array(tuple(int(Water[i:i+2], 16) for i in (0, 2, 4))) #226 , 169, 41

Unlabeled = ‘#9B9B9B ‘.lstrip(’#’)

Unlabeled = np.array(tuple(int(Unlabeled[i:i+2], 16) for i in (0, 2, 4))) #155 , 155, 155

#########################

Now replace RGB to integer values to be used as labels.

#Find pixels with combination of RGB for the above defined arrays…

#if matches then replace all values in that pixel with a specific integer

def rgb_to_2D_label(label):

“”"

Supply our labale masks as input in RGB format.

Replace pixels with specific RGB values …

“”"

label_seg = np.zeros(label.shape,dtype=np.uint8)

label_seg [np.all(label == Building,axis=-1)] = 0

label_seg [np.all(label==Land,axis=-1)] = 1

label_seg [np.all(label==Road,axis=-1)] = 2

label_seg [np.all(label==Vegetation,axis=-1)] = 3

label_seg [np.all(label==Water,axis=-1)] = 4

label_seg [np.all(label==Unlabeled,axis=-1)] = 5

label_seg = label_seg[:,:,0] #Just take the first channel, no need for all 3 channels

return label_seg

labels = []

for i in range(mask_dataset.shape[0]):

label = rgb_to_2D_label(mask_dataset[i])

labels.append(label)

labels = np.array(labels)

labels = np.expand_dims(labels, axis=3)

print("Unique labels in label dataset are: ", np.unique(labels))

Subscribe: https://www.youtube.com/channel/UC34rW-HtPJulxr5wp2Xa04w/featured

#python

Python Tips and Tricks :  Working with RGB (and Hex) Masks for Semantic Segmentation
2.65 GEEK