Preprocessing for Computer Vision: why your binary, one-channel, or grayscale image appear colored.
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.
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:
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_images
in 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 thecmap
parameter of imshow
to gray or use the one_channel
function shown below:
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.