为何OpenCV 3.4.9.31版本读取图像后显示为RGB而非BGR?
Great question—let’s break this down clearly, because this is a super common point of confusion with OpenCV!
First, let’s clear up the core misconception: cv2.imread() does read images in BGR channel order by default (even in the 3.4.9.31 version you’re using). The reason you’re seeing what looks like a "normal" RGB image is because cv2.imshow() automatically adjusts the channel order to match your system’s display requirements.
Let’s break this into actionable details:
What’s actually happening under the hood:
When you load an image withcv2.imread(filename), the resultingImgarray is stored in BGR order in memory. But when you pass this array tocv2.imshow(), the function internally converts BGR to the RGB color space your monitor uses. This conversion is invisible to you, so the image looks natural to your eyes.How to verify the channel order is still BGR:
To confirm the raw data is still BGR, try displaying the same image with a library that expects RGB input (like Matplotlib). Here’s a quick test you can run:import cv2 import matplotlib.pyplot as plt folder = 'C:/Users/xxx/PycharmProjects/Images/' picture = 'lena.png' filename = folder + picture # Load with OpenCV (stored as BGR) img_bgr = cv2.imread(filename) # Display with cv2.imshow (looks normal, since OpenCV handles conversion) cv2.imshow("OpenCV Display", img_bgr) # Display with Matplotlib (will look blue-tinted, since it expects RGB) plt.imshow(img_bgr) plt.title("Matplotlib Display (BGR treated as RGB)") plt.show() cv2.waitKey(0) cv2.destroyAllWindows()You’ll notice the Matplotlib display has a distinct blue color cast—this is concrete proof the original image data is in BGR order. If you convert it to RGB first with
cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB), Matplotlib will display it correctly.Key takeaway:
cv2.imshow()hides the BGR-to-RGB conversion for convenience. The image data in memory remains BGR, but the display is adjusted so you see the correct colors. That’s why your basic image read code shows what appears to be an RGB image—OpenCV is handling the display conversion automatically.
内容的提问来源于stack exchange,提问作者Joker

