python - matplotlib 为图像添加蓝色阴影

标签 python opencv image-processing matplotlib

<分区>

我正在尝试使用 matplotlib 进行基本操作,但我发现每当我尝试使用 matplotlib 显示图像时,图像都会添加蓝色阴影。

例如,

代码:

# import the necessary packages
import numpy as np
import cv2
import matplotlib.pyplot as plt

image = cv2.imread("./data/images/cat_and_dog.jpg")
cv2.imshow('image',image) # Display the picture


plt.imshow(image)
plt.show()

cv2.waitKey(0) # wait for closing
cv2.destroyAllWindows() # Ok, destroy the window

并且opencv gui 和matplotlib 显示的图像是不同的。

enter image description here

甚至直方图都失真了,而不仅仅是显示

enter image description here

如何避免图像上出现这种蓝色阴影

最佳答案

参见:http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_gui/py_image_display/py_image_display.html#using-matplotlib

Warning: Color image loaded by OpenCV is in BGR mode. But Matplotlib displays in RGB mode. So color images will not be displayed correctly in Matplotlib if image is read with OpenCV. Please see the exercises for more details.

您可以将 plt.imshow(image) 行替换为以下行:

im2 = image.copy()
im2[:, :, 0] = image[:, :, 2]
im2[:, :, 2] = image[:, :, 0]
plt.imshow(im2)

关于python - matplotlib 为图像添加蓝色阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40067243/

相关文章:

python - cv2.matchTemplate 函数与模板和图像不匹配后,如何阻止程序执行?

python - KIVY语言: multiple commands in a single line

python - 模型未在 init.py django 中导入

c++ - 在 OpenCV 中编译警告并且没有输出

ios - OpenCV - 无法访问 Mat 中的所有像素

python-2.7 - 用于 3D 图像体积的 SSIM

python - SubfieldBase 已被弃用。使用 Field.from_db_value 代替

opencv - opencv在linux Mint的编译(安装)错误?

iphone - 如何在iOS中分割图像以去除背景并保留前景图片

Javascript Canvas 上下文过滤器仅适用于模糊