Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plt.imshow shows color images for grayscale images in IPython

I have the following RGB image imRGB

import cv2
import matplotlib.pyplot as plt

#Loading the RGB image
imRGB = cv2.imread('*path*')

imRGB.shape
>>(128L, 128L, 3L)

#plotting
plt.imshow(imRGB)

imRGB

I convert this to a grayscale image imGray

imGray = cv2.cvtColor(imRGB, cv2.COLOR_BGR2GRAY)

imGray.shape
>>(128L, 128L)

#plotting
plt.imshow(imGray)

imGray


Question: Why does the grayscale image imGray is shown in color?

like image 261
akilat90 Avatar asked Dec 21 '16 14:12

akilat90


People also ask

How do I change my color on Imshow?

The most direct way is to just render your array to RGB using the colormap, and then change the pixels you want.


1 Answers

imRGB was a 3D matrix (height x width x 3).
Now imGray is a 2D matrix in which there are no colors, only a "luminosity" value. So matplotlib applied a colormap by default.

Read this page and try to apply a grayscale colormap or a different colormap, check the results.

plt.imshow(imGray, cmap="gray")

Use this page as reference for colormaps. Some colormap could not work, if it occurs try another colormap.

like image 110
marcoresk Avatar answered Oct 03 '22 10:10

marcoresk