Convert rgb to yuv and access channels Y, U and V

I have been looking for this conversion for a while. What are the ways to convert an RGB image to a YUV image and access the Y, U, and V channels using Python on Linux? (using opencv, skimage, etc.)

Update: I used opencv

img_yuv = cv2.cvtColor(image, cv2.COLOR_BGR2YUV)
y, u, v = cv2.split(img_yuv)

cv2.imshow('y', y)
cv2.imshow('u', u)
cv2.imshow('v', v)
cv2.waitKey(0)

and got this result, but they all seem gray. Failed to get the result presented on the wikipedia page

Am I doing something wrong?

enter image description here

+4
source share
1 answer

NB. YUV โ†” RGB OpenCV 3.2.0 buggy! -, U V . , 2.x 2.4.13.2.


, , , split ting 3- YUV . , , , , imshow . BGR.

- . (LUT). U V BGR, .

, , , .

Colormap U

:

colormap_u = np.array([[[i,255-i,0] for i in range(256)]],dtype=np.uint8)

Colormap for u channel

Colormap V-

:

colormap_v = np.array([[[0,255-i,i] for i in range(256)]],dtype=np.uint8)

Colormap for v channel

YUV

, :

import cv2
import numpy as np


def make_lut_u():
    return np.array([[[i,255-i,0] for i in range(256)]],dtype=np.uint8)

def make_lut_v():
    return np.array([[[0,255-i,i] for i in range(256)]],dtype=np.uint8)


img = cv2.imread('shed.png')

img_yuv = cv2.cvtColor(img, cv2.COLOR_BGR2YUV)
y, u, v = cv2.split(img_yuv)

lut_u, lut_v = make_lut_u(), make_lut_v()

# Convert back to BGR so we can apply the LUT and stack the images
y = cv2.cvtColor(y, cv2.COLOR_GRAY2BGR)
u = cv2.cvtColor(u, cv2.COLOR_GRAY2BGR)
v = cv2.cvtColor(v, cv2.COLOR_GRAY2BGR)

u_mapped = cv2.LUT(u, lut_u)
v_mapped = cv2.LUT(v, lut_v)

result = np.vstack([img, y, u_mapped, v_mapped])

cv2.imwrite('shed_combo.png', result)

:

Composite source and Y, U, V channels

+5

Source: https://habr.com/ru/post/1677186/


All Articles