# 代码如下,实现了,但结果可能是错误的,不是我想要的灰度,有人帮我解决,看起来很简单,但我就是不知道哪里错了
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
Run Code Online (Sandbox Code Playgroud)


灰度转换正确完成。问题在于您如何显示图像。尝试这个:
plt.imshow(gray, cmap='gray')
Run Code Online (Sandbox Code Playgroud)
默认情况下,imshow将其自己的颜色键添加到单通道图像中,以使其更易于查看。一个典型的例子是热图像,通常显示蓝色和红色,但所有颜色仅取决于一个通道。
希望这可以帮助!