简体   繁体   English

Python OpenCV - imshow 不需要从 BGR 转换为 RGB

[英]Python OpenCV - imshow doesn't need convert from BGR to RGB

As I'm lead to believe, OpenCV reads images in BGR colorspace ordering and we usually have to convert it back to RGB like this:正如我所相信的,OpenCV 以 BGR 色彩空间 顺序读取图像,我们通常必须像这样将其转换回 RGB:

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

But when I try to simply read an image and show it, the coloring seems fine (without the need to convert BGR to RGB):但是当我尝试简单地读取图像并显示它时,着色似乎很好(无需将 BGR 转换为 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)

So is imshow() changing the ordering within the function automatically (from BGR to RGB) or the ordering has been BGR all along?那么imshow()是自动更改函数内的排序(从 BGR 到 RGB)还是排序一直是 BGR?

BGR and RGB are not color spaces, they are just conventions for the order of the different color channels. BGR 和 RGB 不是颜色空间,它们只是不同颜色通道顺序的约定。 cv2.cvtColor(img, cv2.COLOR_BGR2RGB) doesn't do any computations (like a conversion to say HSV would), it just switches around the order. cv2.cvtColor(img, cv2.COLOR_BGR2RGB)不做任何计算(就像转换成 HSV 那样),它只是切换顺序。 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 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 imreadimwriteimshow确实都适用于 BGR 顺序,因此当您使用cv2.imread读取图像然后想使用cv2.imshow显示时,无需更改顺序。

While BGR is used consistently throughout OpenCV, most other image processing libraries use the RGB ordering.虽然 BGR 在整个 OpenCV 中一致使用,但大多数其他图像处理库使用 RGB 排序。 If you want to use matplotlib 's imshow but read the image with OpenCV, you would need to convert from BGR to RGB.如果您想使用matplotlibimshow但使用 OpenCV 读取图像,则需要从 BGR 转换为 RGB。

screen = cv2.cvtColor(screen, cv2.COLOR_RGB2BGR)

这一行代码将 rgb 更改为 bgr

opencv_image_with_bgr_channels = cv2.imread('path/to/color_image.jpg')

matplotlib_compatible_image_with_rgb_channels = opencv_image_with_bgr_channels[:,:, ::-1]

This converts BGR to RGB Channels Image by reversing the channels.这通过反转通道将 BGR 转换为 RGB 通道图像。

for matplotlib we need to change BGR to RGB:对于 matplotlib,我们需要将 BGR 更改为 RGB:

img = cv2.imread("image_name")
img = img[...,::-1]

plt.imshow(img)

If you do not need to use any other Image processing library (example Matplotlib's imshow), there is no need to do color scale conversion.如果不需要使用任何其他图像处理库(例如 Matplotlib 的 imshow),则无需进行色标转换。 Below code is an example, where the color scale conversion is done but when the image is loaded, it is still loaded in BGR.下面的代码是一个示例,其中完成了色阶转换,但是当加载图像时,它仍然加载在 BGR 中。 This conversion is not needed as the image is displayed using cv2.imshow().由于使用 cv2.imshow() 显示图像,因此不需要此转换。

import cv2

# read the image #
image = cv2.imread('<<Image Path>>')
                
image_rgb = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)

# write a function to draw circles on the image #
def draw_circle(event,x,y,flags,params):
    if event == cv2.EVENT_RBUTTONDOWN:
        cv2.circle(img=image_rgb,center=(x,y),radius=100,color=(0,0,255),thickness=10)

# Open CV callbacks #
cv2.namedWindow(winname='ImageWindow')
cv2.setMouseCallback('ImageWindow',draw_circle)

# display the image till the user hits the ESC key #
while True:
    cv2.imshow('ImageWindow',image_rgb)
    if cv2.waitKey(20) & 0xFF == 27:
        break
        
cv2.destroyAllWindows()

或者,您可以使用 imutils.opencv2matplotlib() 函数,该函数不需要 BGR 到 RGB 的转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM