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

前端之家收集整理的这篇文章主要介绍了python – 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相关文章