如何在 OpenCV 中从 YUV 文件读取帧?
如何在 OpenCV 中从 YUV 文件读取帧?
How to read a frame from YUV file in OpenCV?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何在 OpenCV 中从 YUV 文件读取帧?
How to read a frame from YUV file in OpenCV?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(6)
我编写了一个非常简单的 python 代码来从二进制文件读取 YUV NV21 流。
I wrote a very simple python code to read YUV NV21 stream from binary file.
如前所述,YUV 格式有多种类型:
http://www.fourcc.org/yuv.php< /a>
在 OpenCV 中从 YUV 格式转换为 RGB 非常简单:
以下是 YV12 格式的 YUV 缓冲区的示例:
关键技巧是在之前定义 RGB Mat 的尺寸/em> 你转换。
As mentioned, there are MANY types of YUV formats:
http://www.fourcc.org/yuv.php
To convert to RGB from a YUV format in OpenCV is very simple:
Here is an example for a YUV buffer in YV12 format:
The key trick is to define the dimensions of your RGB Mat before you convert.
更新这里有更新版本的代码:https://github.com/ chelyaev/opencv-yuv
我发布了一些代码,它将读取单个 YUV 4:2:0 平面图像文件。您可以直接将其应用于大多数 YUV 文件(只需继续从同一
FILE
对象读取)。 例外是在处理具有标头的 YUV 文件时 (通常,它们具有*.y4m
扩展名)。如果您想处理此类文件,您有两个选择:FILE
对象中的标头数据它也不适用于任何其他形式的 YUV 格式(非平面、不同色度抽取)。正如 @Stephane 指出的那样,有很多这样的格式(其中大多数没有任何标识标头),这可能就是 OpenCV 不支持它们的原因。
但使用它们相当简单:
cvCvtColor
。最后是代码:
UPDATE there's a newer version of the code here: https://github.com/chelyaev/opencv-yuv
I'm posting some code that will read a single YUV 4:2:0 planar image file. You can directly apply this to most YUV files (just keep reading from the same
FILE
object). The exception to this is when dealing with YUV files that have a header (typically, they have a*.y4m
extension). If you want to deal with such files, you have two options:FILE
object before using the code belowffmpeg
or similar tool). This is the option I prefer since it's the simplest.It also will not work for any other form of YUV format (non-planar, different chroma decimation). As @Stephane pointed out, there are many such formats (and most of them don't have any identifying headers), which is probably why OpenCV doesn't support them out of the box.
But working with them is fairly simple:
cvCvtColor
.Finally, the code:
我认为至少在当前版本中这是不可能的。当然,这并不难做到,但这并不是一个有趣的功能,因为:
不过,使用
cvCvtColor()
仍然可以进行转换,这意味着无论如何它还是有一定意义的。I don't think it is possible to do, at least with the current version. Of course, it wouldn't be that difficult to do, but it is not such an interesting feature, as:
Conversions are still possible though, using
cvCvtColor()
, which means that it is of some interest anyway.我遇到了同样的问题。我的解决方案是
1.读取一个yuv帧(如I420)到一个字符串对象“yuv”。
2.将yuv帧转换为BGR24格式。我使用 libyuv 来做到这一点。为 libyuv 函数编写 python 包装器很容易。现在你得到另一个 BGR24 格式的字符串对象“bgr”。
3.使用numpy.fromstring从“bgr”字符串对象中获取图像对象。您需要更改图像对象的形状。
下面是一个简单的yuv查看器供大家参考。
I encountered the same problem. My solution is
1. read one yuv frame (such as I420) to a string object "yuv".
2. convert the yuv frame to BGR24 format. I use libyuv to do it. It is easy to write a python wrapper for libyuv functions. now you get another string object "bgr" with BGR24 format.
3. use numpy.fromstring to get image object from the "bgr" string object. you need to change the shape of the image object.
Below is a simple yuv viewer for your reference.
供将来参考:我已将 @xianyanlin 的出色答案转换为 Python 3。以下代码适用于从 Raspberry Pi 拍摄的视频相机并似乎输出正确的颜色和纵横比。
警告:它使用numpy格式来指定高度*宽度的分辨率,例如1080 * 1920、480 * 640。
For future reference: I have converted @xianyanlin's brilliant answer to Python 3. The below code works with videos taken from the Raspberry Pi camera and seems to output correct color and aspect ratio.
Warning: it uses the numpy format for specifying resolution of height * width, e.g. 1080 * 1920, 480 * 640.