Thursday 1 February 2024

OpenCV uses BGR as default color space

OpenCV use  BGR (Blue, Green, Red) color space as its default color representation for images. But other libraries like Matplotlib use RGB color space (RGB) as default.

What is meant by OpenCV BGR representation?

when you load an image using OpenCV, it's typically stored as a NumPy array with the channels ordered as BGR.

 

image = cv.imread('image.png')

 

For example, when you read an image using OpenCV's cv2.imread function, the result is a NumPy array where the color channels are arranged as BGR format.

 

When you display the same image numpy array in Matplotlib (which use RGB as default color space), Red become blue and blue become red. You can confirm the same from below application.

 

Original image

 


 

default_color_space.py

import cv2 as cv
import matplotlib.pyplot as plt

image = cv.imread('rgb.png')

cv.imshow('image in opencv', image)

plt.imshow(image)
plt.title('Image in matplotlib')
plt.show()

# Close the OpenCV windows
cv.destroyAllWindows()

Output

 

Image in opencv


 


Same image array in Matplotlib.

 

 


Note

When you convert an image from BGR to RGB, the pixel values themselves do not change. If the BGR image has pixel values (1, 2, 3) for a specific pixel, the corresponding RGB image will also have the same pixel values (1, 2, 3) for that pixel. The conversion only involves rearranging the color channels in terms of interpretation.

 

For example, if the BGR image has (1, 2, 3) for a pixel, the equivalent pixel in the RGB image will also be (1, 2, 3).


Previous                                                    Next                                                    Home

No comments:

Post a Comment