简体   繁体   English

为什么使用 OpenCV 将 RGB 图像转换为灰度图像时会得到绿色图像?

[英]Why do I get a green image when I convert an RGB image to grayscale using OpenCV?

When I tried to convert an RGB image with OpenCV function cv2.cvtColor(), I got a green-like image.当我尝试使用 OpenCV 函数 cv2.cvtColor() 转换 RGB 图像时,我得到了一个类似绿色的图像。

I've converted the raw image read by OpenCV to RGB-format, again converted it to gray scale using cv2.cvtColor(), and tried to display it using pyplot.imshow() function.我已经将 OpenCV 读取的原始图像转换为 RGB 格式,再次使用 cv2.cvtColor() 将其转换为灰度,并尝试使用 pyplot.imshow() 函数显示它。

image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
plt.imshow(image)

plt.imgshow() uses a color map for a single-channel images. plt.imgshow()使用单通道图像的颜色映射。 You have two possible solutions, convert your grayscale to rgb (basically duplicate the grayscale 3 times), or choose a proper colormap as explained here: https://matplotlib.org/3.1.0/tutorials/colors/colormaps.html您有两种可能的解决方案,将您的灰度转换为 rgb(基本上将灰度复制 3 次),或者按照此处的说明选择合适的颜色图: https : //matplotlib.org/3.1.0/tutorials/colors/colormaps.html

线性颜色图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM