5

I've been trying to convert an image to grayscale using opencv in Python but it converts the image to some kind of thermal camera image. What am I doing wrong? Here is the code for image below:

img =X_tr[9999]
plt.imshow(img)
plt.show()
img = cv2.cvtColor(img.astype(np.uint8), cv2.COLOR_RGB2GRAY)
plt.imshow(img)
plt.show()
img.shape

This image is taken from CIFAR10 dataset. Thanks. Code and result after using opencv

Neeraj Negi
  • 100
  • 2
  • 6
  • I just saw that there is actually already a similar question (not using opencv though). I will still leave my answer here, as the point about the normalization does not become apparent from the other thread – ImportanceOfBeingErnest Apr 01 '17 at 14:28

1 Answers1

20

Gray scale images, i.e. images with only one colorchannel, are interpreted by imshow as to be plotted using a colormap. You therefore need to specify the colormap you want to use (and the normalization, if it matters).

plt.imshow(img, cmap="gray", vmin=0, vmax=255)
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712