OpenCV用BGR通道顺序将图像文件读到内存,在做图像处理之前,要用cvtColor()函数将颜色通道顺序进行转化,否则处理会出错。范例程序如下:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import cv2, math
filename = r'images\2.jpg'
im_bgr = cv2.imread(filename)
im_rgb = cv2.cvtColor(im_bgr, cv2.COLOR_BGR2RGB) #将opencv的BGR通道顺序转换为RGB
fig, ax = plt.subplots(1,2)
ax[0].imshow(im_rgb)
ax[0].set_title("RGB:Original")
ax[1].imshow(im_bgr)
ax[1].set_title("BGR:OpenCV Channel orders")
plt.show()
运行结果:
BGR vs RGB
网友评论