5
votes

When I tried to convert an RGB image with OpenCV function cv2.cvtColor(), I got a green-like image.

I've converted the raw image read by OpenCV to RGB-format, again converted it to gray scale using cv2.cvtColor(), and tried to display it using pyplot.imshow() function.

image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
plt.imshow(image)
1
You have to explicitly set the cmap (colormap) parameter to gray, i.e. use plt.imshow(image, cmap='gray'). The default is viridis, obviously some green-ish/blue-ish colormap. - HansHirse
Thanks! Now I set the cmap parameter to gray and it works. Previously I thought that the default setting would be gray instead of some other color cuz it feels more natural. - Kaiming

1 Answers

3
votes

plt.imgshow() uses a color map for a single-channel images. You have two possible solutions, convert your grayscale to rgb (basically duplicate the grayscale 3 times), or choose a proper colormap as explained here: https://matplotlib.org/3.1.0/tutorials/colors/colormaps.html

linear colormaps