0

I am trying to convert a color image (RGB, 24 bit, png) to grayscale using OpenCV. But I am getting unexpected results. I have tried the following 3 approaches, which all give the same result.
Method 1:

img=cv2.imread(file)
img=cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
plt.imshow(img)
temp=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
plt.imshow(temp)

Method 2:

img=cv2.imread(file)
plt.imshow(img)
temp=cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
plt.imshow(temp)

Method 3:

img=cv2.imread(file,0)
img=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
plt.imshow(img)

The original image is:
enter image description here
The image I am getting after the above conversions is:
enter image description here

I assume the problem lies in the channels (BGR being default for OpenCV imread). I need a proper grayscale image. Any help is appreciated. Thank You!

3
  • 1
    Have a read here... stackoverflow.com/a/61505879/2836621 Commented May 3, 2020 at 13:57
  • 2
    Also, you need to use cmap='gray' else you get the Viridis colormap. Commented May 3, 2020 at 13:59
  • Thank you so much! The problem as mentioned in the link was that I was using another library. Commented May 3, 2020 at 14:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.