首页 > 代码库 > Python OpenCV ——Matplotlib显示图片

Python OpenCV ——Matplotlib显示图片

   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.(引自文档)

import numpy as npimport cv2from matplotlib import pyplot as plt‘‘‘img = cv2.imread(‘cute.jpg‘,0)plt.imshow(img,cmap=‘gray‘,interpolation=‘bicubic‘)plt.xticks([],plt.yticks([]))  # to hide tick values on X and Y axisplt.show()‘‘‘‘‘‘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.‘‘‘img = cv2.imread(‘cute.jpg‘)b, g, r = cv2.split(img)img2 = cv2.merge([r,g,b])# img2 = img[:,:,::-1]    this can be fasterplt.subplot(121);plt.imshow(img)  # expects distorted colorplt.subplot(122);plt.imshow(img2)  # expects true colorplt.show()cv2.imshow(‘bgr image‘,img)  # expects true colorcv2.imshow(‘rgb image‘,img2)  # expects distrorted colorcv2.waitKey(0)cv2.destroyAllWindows()

  图像输出:

Matlpotlib输出:

技术分享

OpenCV输出:

技术分享

 

Python OpenCV ——Matplotlib显示图片