首页 > 代码库 > 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显示图片
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。