尝试使用 OpenCV 写入视频文件

发布于 2024-08-21 17:16:53 字数 1049 浏览 8 评论 0原文

我正在尝试使用 OpenCV 编写视频文件。我有一个简单的程序,它从视频文件加载帧,然后接受保存它们。

首先,cvCreateVideoWrite 总是返回 NULL。我从你们的小组得到答复说它返回单独的图像并尝试将文件名更改为 test0001.png,这有效。

但现在cvWriteFrame函数总是失败,代码是

 CString path;
 path="d:\\mice\\Test_Day26_2.avi";

   CvCapture* capture = cvCaptureFromAVI(path);
   IplImage* img = 0;

 CvVideoWriter *writer = 0;
 int isColor = 1;
 int fps     = 25;  // or 30
 int frameW  = 640; // 744 for firewire cameras
 int frameH  = 480; // 480 for firewire cameras
 writer=cvCreateVideoWriter("d:\\mice\\test0001.png",CV_FOURCC('P','I','M','1'),
                           fps,cvSize(frameW,frameH),isColor);
 if (writer==0)
  MessageBox("could not open writter");

int nFrames = 50;
for(int i=0;i<nFrames;i++){
  if (!cvGrabFrame(capture))
   MessageBox("could not grab frame");
  img=cvRetrieveFrame(capture);  // retrieve the captured frame
  if (img==0)
   MessageBox("could not retrive data");
  if (!cvWriteFrame(writer,img) )
   MessageBox("could not write frame");
}
cvReleaseVideoWriter(&writer);

I’m trying to use OpenCV to write a video file. I have a simple program that loads frames from a video file then accepts to save them

At first the cvCreateVideoWrite always return NULL. I got a answer from your group saying it returns separate images and to try to change the file name to test0001.png, this worked.

But now the cvWriteFrame function always fails, the code is

 CString path;
 path="d:\\mice\\Test_Day26_2.avi";

   CvCapture* capture = cvCaptureFromAVI(path);
   IplImage* img = 0;

 CvVideoWriter *writer = 0;
 int isColor = 1;
 int fps     = 25;  // or 30
 int frameW  = 640; // 744 for firewire cameras
 int frameH  = 480; // 480 for firewire cameras
 writer=cvCreateVideoWriter("d:\\mice\\test0001.png",CV_FOURCC('P','I','M','1'),
                           fps,cvSize(frameW,frameH),isColor);
 if (writer==0)
  MessageBox("could not open writter");

int nFrames = 50;
for(int i=0;i<nFrames;i++){
  if (!cvGrabFrame(capture))
   MessageBox("could not grab frame");
  img=cvRetrieveFrame(capture);  // retrieve the captured frame
  if (img==0)
   MessageBox("could not retrive data");
  if (!cvWriteFrame(writer,img) )
   MessageBox("could not write frame");
}
cvReleaseVideoWriter(&writer);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

垂暮老矣 2024-08-28 17:16:53

尝试 CV_FOURCC('D', 'I', 'V', 'X'), CV_FOURCC('f', 'f', 'd', 's') code>(使用 *.avi 文件名)或 CV_FOURCC_DEFAULT(使用 *.mpg)。 opencv中视频写的还是比较乱>>_>

Try CV_FOURCC('D', 'I', 'V', 'X'), CV_FOURCC('f', 'f', 'd', 's') (with *.avi filename) or CV_FOURCC_DEFAULT (with *.mpg). Video writing is still quite messy in opencv >_>

难以启齿的温柔 2024-08-28 17:16:53

我在 OpenCV 中也看到了许多编写视频的问题。我发现英特尔 iYUV 格式非常适合我的需要。

I've seen many issues with writing video as well in OpenCV. I found intel iYUV format worked well for what I needed.

故事与诗 2024-08-28 17:16:53

您的库是使用定义的 HAVE_FFMPEG 构建的吗?
如果不是,您可能需要使用该选项重新编译 opencv。您应该在配置步骤中看到类似这样的内容:

...
Video I/O--------------
Use QuickTime      no
Use xine           no
Use ffmpeg:        yes
Use v4l            yes
...

如果您没有 ffmpeg,您可以从 此处

Was your library built with HAVE_FFMPEG defined?
If it wasn't,you might need to recompile opencv with that option.You should see something like this in the configure step:

...
Video I/O--------------
Use QuickTime      no
Use xine           no
Use ffmpeg:        yes
Use v4l            yes
...

If you don't have ffmpeg,you can get it from here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文