Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

cv2.cvtcolor bgr2gray seems get an error image

Writer Emily Wong

# the code is as follows, implemented, but the result is possibly wrong, it is not the grayscale i wanted, someone gonna help me with that, it's seems quite simple, but i just don't know what wrong

import cv2 import matplotlib.pyplot as plt import numpy as np import matplotlib.image as mpimg img = cv2.imread('calibration_test.png')

# i want simply convert the rgb image to grayscale and then print it out
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
plt.imshow(gray)
print(gray.shape)
# but the outcome is a colorful image

the original image i used

the ugly outcome

1 Answer

The grayscale conversion was done correctly. The problem is in how you are displaying the image. Try this:

plt.imshow(gray, cmap='gray')

By default imshow adds it's own colour key to single channel images, to make it easier to view. A typical example is thermal images which usually show blue and red, but all the colours are only dependend on one channel.

Hope this helps!

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy