matplotlib.pyplot与cv2、Image.open 读取图片显示的差异

时间:2022-06-28 08:35:32

简介

In the Python bindings of OpenCV, images are represented as NumPy arrays in BGR order. This works fine when using the cv2.imshow function. However, if you intend on using Matplotlib, the plt.imshow function assumes the image is in RGB order. A simple call to cv2.cvtColor will resolve this problem, or you can use the opencv2matplotlib convenience function.

cv2.imshow读取图片的NumPy arrays数组是以 BGR order形式保存的,而Matplotlib中的plt.imshow 是以RGB顺序保存的。若要以plt显示需要用imutils.opencv2matplotlib函数,或者要将Image.open 读取的图片正常显示需要cv2.cvtColor来调用。

图片Gaussian pyramid(一)Image.open 读取
http://blog.csdn.net/u011463646/article/details/77415047

代码

import matplotlib.pyplot  as plt
import cv2
import imutils


cactus = cv2.imread('')
# INCORRECT: show the image without converting color spaces
plt.figure("Incorrect")
plt.imshow(cactus)

# CORRECT: convert color spaces before using plt.imshow
plt.figure("Correct")
plt.imshow(imutils.opencv2matplotlib(cactus))
plt.show()

效果

matplotlib.pyplot与cv2、Image.open 读取图片显示的差异

参考

【1】图片Gaussian pyramid(一)Image.open 读取
http://blog.csdn.net/u011463646/article/details/77415047
【2】https://github.com/jrosebr1/imutils