rgb 到 yuv 转换和访问 Y、U 和 V 通道

rgb to yuv conversion and accessing Y, U and V channels

我一直在寻找这个转换。在Linux上使用Python将RGB图像转换为YUV图像并访问Y、U和V通道的方法有哪些? (使用 opencv、skimage 等...)

更新: 我用的是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)

得到了这个结果,但它们看起来都是灰色的。无法获得如 wikipedia page

中所示的结果

我是不是做错了什么?

NB: OpenCV 3.2.0 之前的 YUV <-> RGB 转换是 buggy!其一,在许多情况下,U 通道和 V 通道的顺序被调换了。据我所知,2.x 在 2.4.13.2 发布时仍然有问题。


它们显示为灰度的原因是,在 split设置 3 通道 YUV 图像时,您创建了三个 1 通道图像。由于包含像素的数据结构不存储任何关于 什么 值表示的信息,因此 imshow 将任何 1 通道图像视为灰度显示。同样,它会将任何 3 通道图像视为 BGR。

您在 Wikipedia example is a false color rendering of the chrominance channels. In order to achieve this, you need to either apply a pre-defined colormap or use a custom look-up table (LUT) 中看到的内容。这会将 U 和 V 值映射到适当的 BGR 值,然后可以显示这些值。

事实证明,用于维基百科示例的颜色映射相当简单。

U 通道的颜色图

绿色和蓝色之间的简单渐变:

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

V 通道的色图

绿色和红色之间的简单渐变:

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

像示例一样可视化 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)

结果:

按照描述使用 LUT 值可能正是维基百科文章图像的制作方式,但描述暗示它是任意的,使用它可能是因为它很简单。这不是任意的;结果基本上与 RGB <-> YUV 转换的工作方式相匹配。如果您使用的是 OpenCV,那么 BGR2YUV 和 YUV2BGR 方法使用同一篇维基百科 YUV 文章中的转换公式给出结果。 (我使用 Java 生成的图像稍微暗一点,否则相同。)

附录:在 Dan Mašek 通过向我们展示查找 table 技巧完美而敏锐地回答问题后,我选择了他,这让我感到很遗憾。维基百科 YUV 文章的作者在描述文章中显示的绿-蓝和绿-红渐变方面做得不错,但正如 Dan Mašek 指出的那样,它并不完美。 U 和 V 的颜色图像确实有点类似于实际发生的情况,所以我称它们为夸张的颜色而不是假色。维基百科关于 YCrCb 的文章相似但有所不同。

// most of the Java program which should work in other languages with OpenCV:
// everything duplicated to do both the U and V at the same time
Mat src = new Mat();
Mat dstA = new Mat();
Mat dstB = new Mat();
src = Imgcodecs.imread("shed.jpg", Imgcodecs.IMREAD_COLOR);

List<Mat> channelsYUVa = new ArrayList<Mat>();
List<Mat> channelsYUVb = new ArrayList<Mat>();

Imgproc.cvtColor(src, dstA, Imgproc.COLOR_BGR2YUV); // convert bgr image to yuv
Imgproc.cvtColor(src, dstB, Imgproc.COLOR_BGR2YUV);

Core.split(dstA, channelsYUVa); // isolate the channels y u v
Core.split(dstB, channelsYUVb);

// zero the 2 channels we do not want to see isolating the 1 channel we want to see
channelsYUVa.set(0, Mat.zeros(channelsYUVa.get(0).rows(),channelsYUVa.get(0).cols(),channelsYUVa.get(0).type()));
channelsYUVa.set(1, Mat.zeros(channelsYUVa.get(0).rows(),channelsYUVa.get(0).cols(),channelsYUVa.get(0).type()));

channelsYUVb.set(0, Mat.zeros(channelsYUVb.get(0).rows(),channelsYUVb.get(0).cols(),channelsYUVb.get(0).type()));
channelsYUVb.set(2, Mat.zeros(channelsYUVb.get(0).rows(),channelsYUVb.get(0).cols(),channelsYUVb.get(0).type()));

Core.merge(channelsYUVa, dstA); // combine channels (two of which are zero)
Core.merge(channelsYUVb, dstB);

Imgproc.cvtColor(dstA, dstA, Imgproc.COLOR_YUV2BGR); // convert to bgr so it can be displayed
Imgproc.cvtColor(dstB, dstB, Imgproc.COLOR_YUV2BGR);

HighGui.imshow("V channel", dstA); // display the image
HighGui.imshow("U channel", dstB);

HighGui.waitKey(0);