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



我正在使用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

I认为我有解决方案,但颜色正确的参考图像会有所帮助。最简单的方法似乎是使用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 $1,substr($2,3,2),substr($2,1,2)}' | xxd -c2 -r - | convert -sampling-factor 4:2:2 -size 640x480 -depth 8 uyvy:- result.png

关键词:ImageMagick,图像处理,命令行,子采样,4:2:2,YUYV,YUY2,YUV

最新更新