I am trying to convert an image from RGB to grayscale. My image looks like this after reading
img = cv2.imread('sample.png')
plt.imshow(img)
I tried converting this to grayscale using cv2 function and the image looks as below after conversion:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
plt.imshow(gray)
As you can see image is not converted to grayscale properly. What could be the reason? Am I missing something here?
The default option for cmap in plt.imshow
is viridis
. Use plt.imshow(gray, cmap='gray')
for grayscale images. If you save the image using cv2.imwrite
you can see the image has been converted to grayscale.