简体   繁体   English

从支持 NV12 和 YUYV 的采集卡采集全范围/无损 rgb 帧 output

[英]Capture full-range/lossless rgb frame from capture card that supports NV12 and YUYV output

I am trying to make a program which captures an image, then i need to compare captured image and the input data which i displayed, both should matc pixel by pixel我正在尝试制作一个捕获图像的程序,然后我需要比较捕获的图像和我显示的输入数据,两者都应该逐像素匹配

Here are the details of my capture card这是我的采集卡的详细信息

$ v4l2-ctl --list-formats-ext -d /dev/video0 $ v4l2-ctl --list-formats-ext -d /dev/video0

ioctl: VIDIOC_ENUM_FMT
        Type: Video Capture

        [0]: 'NV12' (Y/CbCr 4:2:0)
                Size: Discrete 3840x2160
                        Interval: Discrete 0.033s (30.000 fps)
                Size: Discrete 2560x1440
                        Interval: Discrete 0.017s (60.000 fps)
                Size: Discrete 1920x1080
                        Interval: Discrete 0.017s (60.000 fps)
                Size: Discrete 1280x720
                        Interval: Discrete 0.017s (60.000 fps)
                Size: Discrete 640x480
                        Interval: Discrete 0.017s (60.000 fps)
        [1]: 'YUYV' (YUYV 4:2:2)
                Size: Discrete 2560x1440
                        Interval: Discrete 0.020s (50.000 fps)
                Size: Discrete 1920x1080
                        Interval: Discrete 0.017s (60.000 fps)
                Size: Discrete 1280x720
                        Interval: Discrete 0.017s (60.000 fps)
                Size: Discrete 640x480
                        Interval: Discrete 0.017s (60.000 fps)
        [2]: '' (30313050-0000-0010-8000-00aa003)
        [3]: '' (e436eb7e-524f-11ce-9f53-0020af0)

$ v4l2-ctl --all $ v4l2-ctl --all

Driver Info:
        Driver name      : uvcvideo
        Card type        : ITE HDMI 4K+ Bridge: ITE HDMI 4
        Bus info         : usb-0000:00:14.0-6
        Driver version   : 5.18.0
        Capabilities     : 0x84a00001
                Video Capture
                Metadata Capture
                Streaming
                Extended Pix Format
                Device Capabilities
        Device Caps      : 0x04200001
                Video Capture
                Streaming
                Extended Pix Format
Priority: 2
Video input : 0 (Camera 1: ok)
Format Video Capture:
        Width/Height      : 1920/1080
        Pixel Format      : 'YUYV' (YUYV 4:2:2)
        Field             : None
        Bytes per Line    : 3840
        Size Image        : 4147200
        Colorspace        : sRGB
        Transfer Function : Rec. 709
        YCbCr/HSV Encoding: Rec. 709
        Quantization      : Default (maps to Limited Range)
        Flags             :
Crop Capability Video Capture:
        Bounds      : Left 0, Top 0, Width 1920, Height 1080
        Default     : Left 0, Top 0, Width 1920, Height 1080
        Pixel Aspect: 1/1
Selection Video Capture: crop_default, Left 0, Top 0, Width 1920, Height 1080, Flags:
Selection Video Capture: crop_bounds, Left 0, Top 0, Width 1920, Height 1080, Flags:
Streaming Parameters Video Capture:
        Capabilities     : timeperframe
        Frames per second: 60.000 (60/1)
        Read buffers     : 0


I have tried using various methods opencv but ffmpeg came the closest我尝试过使用各种方法 opencv 但 ffmpeg 最接近

With below command i am able to get good results but not what i want使用以下命令我可以获得良好的结果但不是我想要的

ffmpeg -y -f v4l2 -pix_fmt NV12 -video_size 1920x1080 -i /dev/video0 -pix_fmt bgra -frames:v 10 webcam%03d.bmp

Reference Image参考图像

RGB of Reference image参考图像的 RGB

RGB of captured image拍摄图像的 RGB

Note:- I am able to capture fine with Aforge on windows, but not with ffmpeg on linux. Would like to know if anyone has already got solution to this.注意:- 我可以在 windows 上使用 Aforge 捕获正常,但不能在 linux 上使用 ffmpeg 捕获。想知道是否有人已经解决了这个问题。

Thanks in advance.提前致谢。

We have to mark the input as BT.709 with "TV Range":我们必须将输入标记为带有“TV Range”的BT.709

ffmpeg -y -f v4l2 -pix_fmt nv12 -video_size 1920x1080 -color_primaries bt709 -color_trc bt709 -colorspace bt709 -color_range tv -i /dev/video0 -pix_fmt bgra -frames:v 10 webcam%03d.bmp


Marking the input color format as BT.709:将输入颜色格式标记为 BT.709:

By default FFmpeg assumes BT.601 color format, and the input video applies BT.709 color format, so we have to mark the video as BT.709 using -color_primaries bt709 -color_trc bt709 -colorspace bt709 arguments.默认情况下,FFmpeg 采用BT.601颜色格式,而输入视频采用 BT.709 颜色格式,因此我们必须使用-color_primaries bt709 -color_trc bt709 -colorspace bt709将视频标记为 BT.709。

When the input color format is BT.709, and FFmpeg convert it as it were BT.601, the result is wrong output colors.当输入颜色格式为BT.709,FFmpeg按BT.601格式转换,结果是错误的output colors。


Marking the range as "TV Range":将范围标记为“电视范围”:
By default FFmpeg assumes assumes "Limited Range" (TV range), but we may add -color_range tv to be sure.默认情况下 FFmpeg assumes 假设“有限范围”(电视范围),但我们可以添加-color_range tv来确定。

Note:笔记:
"TV Range" applies "Limited range" - the range of Y color channel is [16, 235] (U and V rane is [16, 240]). “TV Range”适用于“Limited range”——Y颜色通道的范围是[16, 235](U和V范围是[16, 240])。
(opposed to "Full range" or "PC Range" where YUV range is [0, 255]). (与 YUV 范围为 [0, 255] 的“完整范围”或“PC 范围”相对)。


For reproducing the issue, we may use synthetic video (instead of camera).为了重现问题,我们可能会使用合成视频(而不是相机)。

Create a reference image:创建参考图像:
ffmpeg -y -f lavfi -i testsrc=size=192x108:rate=1:duration=1 -pix_fmt bgra ref%03d.bmp

Create NV12 raw frame in BT.709, "Limited Range" (TV Range):在 BT.709,“有限范围”(电视范围)中创建 NV12 原始帧:
ffmpeg -y -f lavfi -i testsrc=size=192x108:rate=1:duration=1 -vf scale=out_color_matrix=bt709:out_range=tv -pix_fmt nv12 -f rawvideo in.nv12

Convert the raw frame to BMP without marking the color format and range (getting wrong colors):在不标记颜色格式和范围的情况下将原始帧转换为 BMP(得到错误的颜色):
ffmpeg -y -f rawvideo -pix_fmt nv12 -video_size 192x108 -i in.nv12 -pix_fmt bgra -frames:v 1 wrong_colors_out%03d.bmp

Convert the raw frame to BMP with marking the color format and range (getting correct colors):通过标记颜色格式和范围(获得正确的颜色)将原始帧转换为 BMP:
ffmpeg -y -f rawvideo -pix_fmt nv12 -video_size 192x108 -color_primaries bt709 -color_trc bt709 -colorspace bt709 -color_range tv -i in.nv12 -pix_fmt bgra -frames:v 1 out%03d.bmp


Ordered left to right:从左到右排列:
Reference image, "wrong colors" and "correct colors":参考图像,“错误的颜色”和“正确的颜色”:
在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

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

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