OpenCV implements BGR to RGB

1. Problems

When using the opencv function imread() to read a picture, the color order is BGR (blue, green, red), and the color order of Pillow is RGB, so we may need to convert BGR to RGB.

Two, conversion

BGR to RGB can be realized through the following methods

import cv2
import numpy as np
from PIL import Image

# 方法一
im_bgr = cv2.imread('data/src/lena.jpg')

im_rgb = im_bgr[:, :, [2, 1, 0]]
Image.fromarray(im_rgb).save('data/dst/lena_swap.jpg')
# 方法二
im_bgr = cv2.imread('data/src/lena.jpg')

im_rgb = im_bgr[:, :, ::-1]
Image.fromarray(im_rgb).save('data/dst/lena_swap_2.jpg')
# 方法三
im_cv = cv2.imread('data/src/lena.jpg')

im_rgb = cv2.cvtColor(im_cv, cv2.COLOR_BGR2RGB)
Image.fromarray(im_rgb).save('data/dst/lena_rgb_pillow.jpg')

The benefits of this article, free to receive Qt development learning materials package, technical video, including (Qt actual combat project, C++ language foundation, C++ design mode, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓

Guess you like

Origin blog.csdn.net/m0_73443478/article/details/131987289
Recommended