Python analysis: exploring the conversion between RGB and YUV color spaces

insert image description here

In Python, we can use the opencv library to convert between RGB and YUV.

install opencv

pip install opencv-python

Here is a simple example showing how to do color space conversion:

import cv2
import numpy as np

# 创建一个随机的RGB图像
rgb_image = np.random.randint(0, 256, (100, 100, 3)).astype('uint8')

# 将RGB图像转换为YUV图像
yuv_image = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2YUV)

# 将YUV图像转回RGB图像
rgb_image_again = cv2.cvtColor(yuv_image, cv2.COLOR_YUV2RGB)

print('原始RGB图像:')
print(rgb_image)

print('转换后的YUV图像:')
print(yuv_image)

print('再次转换后的RGB图像:')
print(rgb_image_again)

This code first creates a random RGB image, then uses the cv2.cvtColor function to convert it to YUV color space, and then back to RGB color space. You can see that the final RGB image is the same as the original RGB image, which means our conversion is correct.

Guess you like

Origin blog.csdn.net/tuzajun/article/details/130980246