问题描述
我相信,OpenCV会读取BGR色彩空间中的图像,我们通常必须像这样将其转换回RGB:
As I'm lead to believe, OpenCV reads images in the BGR colorspace and we usually have to convert it back to RGB like this:
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
但是当我尝试简单地读取图像并将其显示时,着色看起来很好(不需要将BGR转换为RGB):
But when I try to simply read an image and show it, the coloring seems fine (without the need to convert BGR to RGB):
img_bgr = cv2.imread(image_path)
cv2.imshow('BGR Image',img_bgr)
img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
cv2.imshow('RGB Image',img_rgb )
cv2.waitkey(0)
那么imshow()
是自动在函数中更改色彩空间(从BGR到RGB)还是一直是BGR?
So is imshow()
changing the colorspace within the function automatically (from BGR to RGB) or the colorspace has been BGR all along?
推荐答案
BGR和RGB不是颜色空间,它们只是不同颜色通道顺序的约定. cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
不执行任何计算(例如说HSV的转换),它只是在顺序之间切换.任何排序都是有效的-实际上,三个值(红色,绿色和蓝色)被堆叠以形成一个像素.您可以按照自己喜欢的方式安排它们,只要您告诉显示器您给它指定了什么顺序即可.
BGR and RGB are not color spaces, they are just conventions for the order of the different color channels. cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
doesn't do any computations (like a conversion to say HSV would), it just switches around the order. Any ordering would be valid - in reality, the three values (red, green and blue) are stacked to form one pixel. You can arrange them any way you like, as long as you tell the display what order you gave it.
OpenCV imread
,imwrite
和imshow
确实都可以使用BGR顺序,因此,当您使用cv2.imread
读取图像并想通过.
OpenCV imread
, imwrite
and imshow
indeed all work with the BGR order, so there is no need to change the order when you read an image with cv2.imread
and then want to show it with cv2.imshow
.
虽然在整个OpenCV中始终使用BGR,但大多数其他图像处理库都使用RGB排序.如果要使用matplotlib
的imshow
,但要使用OpenCV读取图像,则需要从BGR转换为RGB.
While BGR is used consistently throughout OpenCV, most other image processing libraries use the RGB ordering. If you want to use matplotlib
's imshow
but read the image with OpenCV, you would need to convert from BGR to RGB.
这篇关于Python OpenCV-imshow不需要从BGR转换为RGB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!