Convert BGR and RGB with Python and OpenCV

OpenCV is a library of programming functions primarily for real-time computer vision. The library is cross-platform and licensed as free and open source software under the Apache License. It is written in C++ and has Python bindings that make it easy to use in Python applications.

In this article, we will learn how to convert between BGR and RGB color formats using Python and OpenCV. Before proceeding, let's understand what BGR and RGB are and why conversion is necessary.

What are BGR and RGB?

RGB and BGR are both color models used to represent colors in digital images. The main difference between these two color models is the order in which the color channels are arranged:

  • RGB (Red, Green, Blue): Colors are represented in the order Red, Green, and Blue. This is the standard color format used by most image processing libraries and display systems.

  • BGR (Blue, Green, Red): Colors are represented in the order Blue, Green, and Red. This is the default color format used by OpenCV when reading images.

The primary distinction between RGB and BGR is the arrangement of the Red and Blue channels. While BGR is essentially the reverse of RGB, there is no difference in the vibrancy and accuracy of the colors only the channel order changes.

Why Convert Between BGR and RGB?

OpenCV reads images in BGR format by default, while libraries like Matplotlib expect RGB format. Converting between these formats ensures proper color display when using different visualization libraries.

Step-by-Step Conversion Process

Step 1: Importing Required Libraries

We need to import OpenCV for image manipulation and NumPy for array operations:

import cv2
import numpy as np

Step 2: Creating Sample Data

Since we cannot read external image files in this environment, let's create a sample BGR image array:

import cv2
import numpy as np

# Create a sample BGR image (100x100 pixels with different colors)
bgr_image = np.zeros((100, 100, 3), dtype=np.uint8)
bgr_image[:, :33, :] = [255, 0, 0]    # Blue region
bgr_image[:, 33:66, :] = [0, 255, 0]  # Green region  
bgr_image[:, 66:, :] = [0, 0, 255]    # Red region

print("BGR image shape:", bgr_image.shape)
print("Sample BGR pixel values:", bgr_image[50, 16])  # Blue region pixel
BGR image shape: (100, 100, 3)
Sample BGR pixel values: [255   0   0]

Step 3: Converting BGR to RGB

To convert from BGR to RGB, we use cv2.COLOR_BGR2RGB flag:

import cv2
import numpy as np

# Create sample BGR image
bgr_image = np.zeros((100, 100, 3), dtype=np.uint8)
bgr_image[:, :33, :] = [255, 0, 0]    # Blue in BGR
bgr_image[:, 33:66, :] = [0, 255, 0]  # Green in BGR
bgr_image[:, 66:, :] = [0, 0, 255]    # Red in BGR

# Convert BGR to RGB
rgb_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2RGB)

print("Original BGR pixel:", bgr_image[50, 16])  # Blue region
print("Converted RGB pixel:", rgb_image[50, 16])  # Same pixel in RGB
Original BGR pixel: [255   0   0]
Converted RGB pixel: [  0   0 255]

Step 4: Converting RGB to BGR

To convert from RGB to BGR, we use cv2.COLOR_RGB2BGR flag:

import cv2
import numpy as np

# Create sample RGB image
rgb_image = np.zeros((100, 100, 3), dtype=np.uint8)
rgb_image[:, :33, :] = [255, 0, 0]    # Red in RGB
rgb_image[:, 33:66, :] = [0, 255, 0]  # Green in RGB
rgb_image[:, 66:, :] = [0, 0, 255]    # Blue in RGB

# Convert RGB to BGR
bgr_converted = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR)

print("Original RGB pixel:", rgb_image[50, 16])  # Red region
print("Converted BGR pixel:", bgr_converted[50, 16])  # Same pixel in BGR
Original RGB pixel: [255   0   0]
Converted BGR pixel: [  0   0 255]

Complete Example

Here's a complete example demonstrating both conversion directions:

import cv2
import numpy as np

# Create a sample image with distinct colors
height, width = 150, 150
image = np.zeros((height, width, 3), dtype=np.uint8)

# Create colored regions in BGR format
image[:50, :, :] = [255, 0, 0]      # Blue region
image[50:100, :, :] = [0, 255, 0]   # Green region  
image[100:, :, :] = [0, 0, 255]     # Red region

print("Original BGR image shape:", image.shape)
print("BGR pixel values (Blue region):", image[25, 75])
print("BGR pixel values (Green region):", image[75, 75])
print("BGR pixel values (Red region):", image[125, 75])

# Convert BGR to RGB
rgb_converted = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
print("\nAfter BGR to RGB conversion:")
print("RGB pixel values (Blue region):", rgb_converted[25, 75])
print("RGB pixel values (Green region):", rgb_converted[75, 75])  
print("RGB pixel values (Red region):", rgb_converted[125, 75])

# Convert back RGB to BGR
bgr_back = cv2.cvtColor(rgb_converted, cv2.COLOR_RGB2BGR)
print("\nAfter RGB back to BGR conversion:")
print("BGR pixel values (Blue region):", bgr_back[25, 75])

# Verify the conversion is lossless
print("\nConversion verification:")
print("Original equals converted back:", np.array_equal(image, bgr_back))
Original BGR image shape: (150, 150, 3)
BGR pixel values (Blue region): [255   0   0]
BGR pixel values (Green region): [  0 255   0]
BGR pixel values (Red region): [  0   0 255]

After BGR to RGB conversion:
RGB pixel values (Blue region): [  0   0 255]
RGB pixel values (Green region): [  0 255   0]
RGB pixel values (Red region): [255   0   0]

After RGB back to BGR conversion:
BGR pixel values (Blue region): [255   0   0]

Conversion verification:
Original equals converted back: True

Key Points

  • OpenCV Default: OpenCV reads images in BGR format by default

  • Lossless Conversion: Converting between BGR and RGB is completely lossless

  • Channel Order: Only the order of Red and Blue channels is swapped; Green remains in the middle

  • Display Libraries: Matplotlib expects RGB format, so convert BGR images before plotting

Common Use Cases

  • Converting OpenCV images for display with Matplotlib

  • Preparing images for machine learning models that expect RGB format

  • Interfacing between different image processing libraries

Conclusion

Converting between BGR and RGB formats is essential when working with OpenCV and other image processing libraries. Use cv2.COLOR_BGR2RGB to convert from BGR to RGB, and cv2.COLOR_RGB2BGR for the reverse conversion. The process is lossless and only changes the order of color channels.

Updated on: 2026-03-27T01:28:28+05:30

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements