Convert BGR to RGB in OpenCV for Computer Vision
Use OpenCV's
cv2.cvtColor(image, cv2.COLOR_BGR2RGB) to convert an image from BGR to RGB color format.Examples
InputA BGR image array with pixel [255, 0, 0] (blue)
OutputRGB image array with pixel [0, 0, 255] (red)
InputA BGR image array with pixel [0, 255, 0] (green)
OutputRGB image array with pixel [0, 255, 0] (green)
InputA BGR image array with pixel [0, 0, 255] (red)
OutputRGB image array with pixel [255, 0, 0] (blue)
How to Think About It
To convert BGR to RGB, you swap the first and third color channels of the image. OpenCV stores images in BGR order by default, but many libraries and displays expect RGB order. Using
cv2.cvtColor with the COLOR_BGR2RGB flag handles this channel swap automatically.Algorithm
1
Get the input image in BGR format.2
Use OpenCV's color conversion function with the BGR to RGB flag.3
Return the converted image in RGB format.Code
python
import cv2 import numpy as np # Create a sample BGR image (blue pixel) image_bgr = np.array([[[255, 0, 0]]], dtype=np.uint8) # Convert BGR to RGB image_rgb = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB) print('BGR pixel:', image_bgr[0,0]) print('RGB pixel:', image_rgb[0,0])
Output
BGR pixel: [255 0 0]
RGB pixel: [ 0 0 255]
Dry Run
Let's trace the conversion of a single blue pixel from BGR to RGB.
1
Input BGR pixel
[255, 0, 0] (blue in BGR)
2
Apply cvtColor with COLOR_BGR2RGB
Channels swapped to [0, 0, 255]
3
Output RGB pixel
[0, 0, 255] (red in RGB)
| Step | BGR Pixel | RGB Pixel |
|---|---|---|
| Input | [255, 0, 0] | N/A |
| Conversion | N/A | [0, 0, 255] |
Why This Works
Step 1: Why BGR to RGB conversion is needed
OpenCV loads images in BGR order, but many systems expect RGB order, so colors appear wrong without conversion.
Step 2: How cvtColor works
cv2.cvtColor swaps the first and third channels automatically when using COLOR_BGR2RGB.
Step 3: Result of conversion
The image colors display correctly in RGB format after conversion.
Alternative Approaches
Manual channel swap using numpy
python
import numpy as np image_rgb = image_bgr[:, :, ::-1]
This reverses the last dimension (channels) manually; it's faster but less readable than cvtColor.
Using OpenCV split and merge
python
b, g, r = cv2.split(image_bgr) image_rgb = cv2.merge([r, g, b])
Splits channels and merges in RGB order; more verbose but explicit.
Complexity: O(n) time, O(n) space
Time Complexity
The conversion processes each pixel once, so time grows linearly with image size.
Space Complexity
A new image array is created for RGB, so space also grows linearly with image size.
Which Approach is Fastest?
Using cv2.cvtColor is optimized in OpenCV and usually faster than manual numpy slicing or splitting.
| Approach | Time | Space | Best For |
|---|---|---|---|
| cv2.cvtColor | O(n) | O(n) | Reliable and optimized conversion |
| Numpy channel reverse | O(n) | O(n) | Fast and concise for simple swaps |
| cv2 split and merge | O(n) | O(n) | Explicit channel control, more verbose |
Use
cv2.cvtColor(image, cv2.COLOR_BGR2RGB) for a simple and reliable conversion.Forgetting that OpenCV uses BGR by default and skipping the conversion, causing wrong colors.