简体   繁体   中英

Convert RGB image to YUV and YCbCr color space image in Opencv Python

谁能帮助我使用opencv Python将RGB颜色空间图像转换为YUV颜色空间图像和YCbCr颜色空间图像?

Use cv2.cvtColor(src, code) to convert Color-Space, the code starts with COLOR_ .

You can use this to look for the color code.

import cv2
## get all color codes
codes = [x for x in dir(cv2) if x.startswith("COLOR_")]

## print first three color codes
print(codes[:3])
# ['COLOR_BAYER_BG2BGR', 'COLOR_BAYER_BG2BGRA', 'COLOR_BAYER_BG2BGR_EA']

## print all color codes
print(codes)

If you read the image into BGR space, then use cv2.COLOR_BGR2YUV and cv2.COLOR_BGR2YCrCb :

#cv2.COLOR_BGR2YUV
#cv2.COLOR_BGR2YCrCb

img = cv2.imread("test.png")
yuv = cv2.cvtColor(img, cv2.COLOR_BGR2YUV)
cv2.imwrite("yuv.png", yuv)

If you read the image into RGB space, then use cv2.COLOR_RGB2YUV and cv2.COLOR_RGB2YCrCb .


Here is an example image(in BGR-HSV-YUV-YCRCB color spaces):

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM