-2

According to the official documentation, reading an image using opencv decodes an in BGR order. But using the version (opencv-python==4.7.0.72), its returning the image in RGB order in a default mode.

import cv2
if __name__ == "__main__":
    image_path = '/home/dell/Downloads/God_Ganesha.jpg'

    image = cv2.imread(image_path)
    img_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    cv2.namedWindow('cv2 bgr', cv2.WINDOW_NORMAL)
    cv2.imshow('cv2 bgr', image)
    cv2.namedWindow('cv2 rgb', cv2.WINDOW_NORMAL)
    cv2.imshow('cv2 rgb', img_rgb)
    cv2.waitKey(0)

Reading an image using opencv should have returned the image in BGR order (official doc) but it's returning in RGB order, and when I did cv2.COLOR_BGR2RGB, it returned in BGR order. Opencv has not mentioned that their default has been changed into RGB. Is there anything I am missing here or error?

Screenshot of the image on both color order

fabian
  • 80,457
  • 12
  • 86
  • 114
  • 2
    Opencv reads images in BGR and alle the functions that use color (imshow, imsrite, etc.) assume BGR ordering. – Micka May 23 '23 at 08:28
  • 1
    OpenCV reads images and holds them internally in BGR format. Its `cv2.imshow()` equally expects images in BGR format. So it is entirely consistent within itself. – Mark Setchell May 23 '23 at 08:29
  • 1
    Why tag that C++? All of your code shown here is `python` code... – fabian May 23 '23 at 08:33
  • Thank for the answers. That means while changing the color space to HSV, we can simply do cv2.COLOR_BGR2HSV right? without thinking about RGB. I was experimenting RGB2HSV and BGR2HSV and results were different. – Shreejan Shrestha May 23 '23 at 08:39
  • yes, after reading from a file, use `cv2.COLOR_BGR2HSV`. You should then see the same results as when you first use `cv2.COLOR_BGR2RGB` followed by a `cv2.COLOR_RGB2HSV` – Micka May 23 '23 at 09:30

1 Answers1

0

If you want to read , show image in open cv to cv you do not need to convert to RGB format. But if you want to read CV and show in Matplotlib , Pillow then you have to convert to RGB