简体   繁体   English

使用 OpenCV Python 从 BGR 转换为 YUYV

[英]Conversion from BGR to YUYV with OpenCV Python

I have been trying to convert a BGR captured frame into the YUYV format.我一直在尝试将 BGR 捕获的帧转换为 YUYV 格式。

In OpenCV Python I can do convert YUYV into BGR with COLOR_YUV2BGR_YUY2 conversion code but I cannot do the reverse of this operation (there is no conversion code for this operation, I have tried COLOR_BGR2YUV but it is not converting correctly).在 OpenCV Python 中,我可以使用COLOR_YUV2BGR_YUY2转换代码将 YUYV 转换为 BGR,但我无法执行此操作的相反操作(此操作没有转换代码,但我尝试过无法正确转换COLOR_BGR2YUV )。 I am curious about how to convert 3-channel BGR frame into the 2-channel YUYV frame.我很好奇如何将3通道BGR帧转换为2通道YUYV帧。

Here you can see the code that I am using to change camera mode to capture YUYV and converting it into BGR, I am looking for the replacement of the cap.set(cv2.CAP_PROP_CONVERT_RGB, 0) so I can capture BGR and convert it into YUYV without cap.set(cv2.CAP_PROP_CONVERT_RGB, 0) (Because it is an optional capture setting and Windows DirectShow ignores this flag)在这里您可以看到我用来更改相机模式以捕获 YUYV 并将其转换为 BGR 的代码,我正在寻找替换cap.set(cv2.CAP_PROP_CONVERT_RGB, 0)以便我可以捕获 BGR 并将其转换为YUYV without cap.set(cv2.CAP_PROP_CONVERT_RGB, 0) (因为它是一个可选的捕获设置,Windows DirectShow 忽略了这个标志)

import cv2
import numpy as np

cap = cv2.VideoCapture(4)
_, frame = cap.read()
cap.set(cv2.CAP_PROP_CONVERT_RGB, 0)  # How to replace this line with another BGR to YUYV conversion?
_, frame_rgb_off = cap.read()
bgr_cvt = cv2.cvtColor(frame_rgb_off, cv2.COLOR_YUV2BGR_YUY2)

And here is the debugger output to show the frame contents:这是调试器 output 以显示帧内容:

Link to the screenshot链接到屏幕截图

I tried to understand the YUYV format but it has only 2-channels and down sampling process is quite complicated.我试图理解 YUYV 格式,但它只有 2 个通道,下采样过程相当复杂。 I have checked the YUYV to BGR conversion methods but I could not find and mathematical conversion approaches (solutions are generally using ffmpeg utility from command line but I need to do it mathematically with numpy arrays I think)我已经检查了 YUYV 到 BGR 的转换方法,但我找不到数学转换方法(解决方案通常使用命令行中的 ffmpeg 实用程序,但我需要使用 numpy ZA3CBC3F9D0CE2F2C11D9E1B671 进行数学转换)

You can use the following code to convert your image to YUV and after that create YUYV from YUV.您可以使用以下代码将图像转换为 YUV,然后从 YUV 创建 YUYV。 In this example an image is given as input to the program:在此示例中,将图像作为程序的输入:

import cv2
import numpy as np

# Load sample image
img_bgr = cv2.imread("home.jpg")
cv2.imshow("original", img_bgr)
cv2.waitKey(0)

# Convert from BGR to YUV
img_yuv = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2YUV)

# Converting directly back from YUV to BGR results in an (almost) identical image
img_bgr_restored = cv2.cvtColor(img_yuv, cv2.COLOR_YUV2BGR)
cv2.imshow("converted", img_bgr_restored)
cv2.waitKey(0)
diff = img_bgr.astype(np.int16) - img_bgr_restored
print("mean/stddev diff (BGR => YUV => BGR)", np.mean(diff), np.std(diff))

# Create YUYV from YUV
y0 = np.expand_dims(img_yuv[...,0][::,::2], axis=2)
u = np.expand_dims(img_yuv[...,1][::,::2], axis=2)
y1 = np.expand_dims(img_yuv[...,0][::,1::2], axis=2)
v = np.expand_dims(img_yuv[...,2][::,::2], axis=2)
img_yuyv = np.concatenate((y0, u, y1, v), axis=2)
img_yuyv_cvt = img_yuyv.reshape(img_yuyv.shape[0], img_yuyv.shape[1] * 2, 
int(img_yuyv.shape[2] / 2))

# Convert back to BGR results in more saturated image.
img_bgr_restored = cv2.cvtColor(img_yuyv_cvt, cv2.COLOR_YUV2BGR_YUYV)
cv2.imshow("converted", img_bgr_restored)
cv2.waitKey(0)

diff = img_bgr.astype(np.int16) - img_bgr_restored
print("mean/stddev diff (BGR => YUV => YUYV => BGR)", np.mean(diff), np.std(diff))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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