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

前端 未结 4 1730
没有蜡笔的小新
没有蜡笔的小新 2020-12-14 14:53

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:

img = cv2.cv         


        
相关标签:
4条回答
  • 2020-12-14 15:31

    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. Below code is an example, where the color scale conversion is done but when the image is loaded, it is still loaded in BGR. This conversion is not needed as the image is displayed using 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()
    
    
    0 讨论(0)
  • 2020-12-14 15:36

    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 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.

    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.

    0 讨论(0)
  • 2020-12-14 15:36
    screen = cv2.cvtColor(screen, cv2.COLOR_RGB2BGR)
    

    this one line code changes rgb to bgr

    0 讨论(0)
  • 2020-12-14 15:37

    Alternatively, you can use imutils.opencv2matplotlib() function, which does not need BGR to RGB conversion.

    0 讨论(0)
提交回复
热议问题