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

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.

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

JIRA workflow automation using GitHub web hooks

PRM & PRM*

7 REASONS TO HIRE MAGENTO DEVELOPERS IN INDIA

magento developers india

Linting C# in 2019 — StyleCop, Sonar, Resharper and Roslyn

Navigating the Git Terminal With GitHub and Unity3D

How to be an uncommonly good web developer

Journey of DLithe Bootcamp .NET Full Stack Developer - Week 6

First Project: Terminal Wordle.py

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mustapha Unubi Momoh

Mustapha Unubi Momoh

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

More from Medium

Try to get your own cutie portrait using MMGEN-FaceStylor

Light-weight People Detection on Microcontrollers using Custom Neural Networks | ignitarium.com

Lets use Active Learning to train YoloV5 on Autonomous driving data.

FPGA vs. GPU for Deep Learning —Introduction