Preprocessing for Computer Vision: why your binary, one-channel, or grayscale image appear colored.

Mustapha Unubi Momoh
2 min readDec 22, 2021

--

Almazroa, Ahmed in RIGA Dataset

Pseudocolored channels? Blame Jet

Once we are through separating the channels of the color image or extracting the desired channel, the usual next step is to view these channels. Below is a sample code for the separating and viewing the channels.

output is shown below:
pseudocolored channels by author from pseudo_ch.py

Oops! The channels are pseudocolored?!

Why?

No, you are probably wrong about your monitor — your screen isn’t faulty, neither is your color setting culpable. The notorious culprit is the default colormap of your visualization library (matplotlib)— Jet! Rainbow color maps such as jet and hsv have been criticized over the years for failing to properly represent image colors. Some of these criticisms include abrupt changes in luminance and pseudocoloring of one-channel images. Below is an illustration of pseudocoloring:

By author from jet_culprit.py

How pseudocoloring happens?

One channel (or binary) images have one intensity value per pixel. However, when a one-channel tensor is passed to imshow — with the default settings — or any implementation that is based on it such as show_image or show_imagesin fastai, the jet colormap (i.e. matplotlib’s default cmap) broadcasts the single intensity value across the other channels in the pixel, thereby giving the image a false color.

Choosing the right colormap

To correctly display the one-channel image, set thecmapparameter of imshowto gray or use the one_channel function shown below:

By author from one_channel.py

Conclusion

The imshow function by default has cmap set to ’jet’, as a result it automatically applies this color map to any array it receives. To display a grayscale image or a one-channel image, change the colormap of your plot function to gray or use the one_channel function presented above.

Please follow me for more articles on preprocessing for computer vision.

--

--

Mustapha Unubi Momoh
Mustapha Unubi Momoh

Written by Mustapha Unubi Momoh

A Python Developer and Data Scientist passionate about Computer vision and NLP

No responses yet