OpenCV和Matplotlib读取方式的区别

 一、读取图像

import cv2
from matplotlib import pyplot as plt

img_path = './test_img2.jpeg'
img = cv2.imread(img_path)
# img = plt.imread(img_path)

cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

plt.imshow(img)

二、OpnCV读取,分别显示图像

OpenCV和Matplotlib读取方式的区别_第1张图片(OpenCV)OpenCV和Matplotlib读取方式的区别_第2张图片(Matplotlib)

 

三、Matplotlib读取,分别显示图像

OpenCV和Matplotlib读取方式的区别_第3张图片(OpenCV)OpenCV和Matplotlib读取方式的区别_第4张图片(Matplotlib)

四、原因

cv2是以BGR形式读取图片的,而plt则是以RGB形式。cv2的imshow()函数显示图片跟原图一样,是因为cv2的imshow()是把BGR转回RGB再显示。

五、通道转换

import cv2
from matplotlib import pyplot as plt

img_path = './test_img2.jpeg'
img = cv2.imread(img_path)
# img = plt.imread(img_path)

cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

img2 = img[:,:, ::-1] # 对数组进行左右翻转:实现BGR通道转换为RGB通道

plt.imshow(img2)

显示结果:

OpenCV和Matplotlib读取方式的区别_第5张图片 (OpenCV)OpenCV和Matplotlib读取方式的区别_第6张图片(Matplotlib)

经过图像的通道转换,cv2.imshow()和plt.imshow()的显示一致。

 

你可能感兴趣的:(图像处理)