从命令行将 YUY2 (YUYV) 转换为 png

Convert YUY2 (YUYV) to png from the command line

我正在使用 v4l2-ctl(1) 从我的网络摄像头捕捉图像,我正在尝试 convert(1) 将原始格式转换为 png。

这是我运行.

的命令
# This is the format my camera outputs.
$ v4l2-ctl --device /dev/video0 --list-formats-ext
ioctl: VIDIOC_ENUM_FMT
        Type: Video Capture

        [1]: 'YUYV' (YUYV 4:2:2)
                Size: Discrete 640x480
                        Interval: Discrete 0.033s (30.000 fps)

# This is how I'm capturing an image.
$ v4l2-ctl --device /dev/video0 --set-fmt-video=width=640,height=480,pixelformat=YUYV
$ v4l2-ctl --device /dev/video0 --stream-mmap --stream-to=frame.raw --stream-count=1

# This is how I tried to convert. (This didn't work.)
$ convert -size 640x480 -depth 8 -sampling-factor 4:2:2 -colorspace YUV yuv:frame.raw frame.png

这给了我一个绿色和粉红色的 png。

我也试过用这个命令显示原始图像。

$ display -size 640x480 -depth 8 -sampling-factor 4:2:2 -colorspace yuv yuv:frame.raw

图像在那里看起来好一点,但整个图像上有一个蓝色滤镜。

这是我的网络摄像头的示例图像。 http://s000.tinyupload.com/?file_id=36420855739943963603

认为 我有解决方案,但正确着色的参考图像会有所帮助。最简单的似乎是使用 ffmpeg 将采样因子为 4:2:2 的原始 YUYV 转换为 PNG,如下所示:

ffmpeg -f rawvideo -s 640x480 -pix_fmt yuyv422 -i frame.raw result.png

如果你想用 ImageMagick 来做,你需要使用它的 uyvy 像素格式,但是你的字节被交换了,所以你需要将它们交换到ImageMagick 在输入它们之前期望的顺序 - 我在这里使用 dd 及其 conv=swab 选项:

dd if=frame.raw conv=swab | convert -sampling-factor 4:2:2 -size 640x480 -depth 8 uyvy:- result.png

如果您需要以其他更复杂的方式交换和重新排序字节,您可以使用 xxdawk 非常简单地完成:

xxd -c2 frame.raw  | awk '{print ,substr(,3,2),substr(,1,2)}' | xxd -c2 -r - | convert -sampling-factor 4:2:2 -size 640x480 -depth 8 uyvy:- result.png

关键词: ImageMagick, 图像处理, 命令行, sub-sampled, 4:2:2, YUYV, YUY2, YUV