【发布时间】:2017-11-25 05:13:50
【问题描述】:
在 MacOSX 上使用 OpenCV 2.4.13.2 上的 C API 创建和保存视频后,我无法打开视频,并且我查看了许多已过时的教程,因为现在 C++ API 更受 OpenCV 的青睐。我花了两天时间在教程上,特别是我试图在这里重现这个例子http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/opencv-intro.html#SECTION00073000000000000000
我可以启动相机并开始录制和写入文件“out.avi”,但是我无法使用 VLC 或 Quicktime 打开它。
这是我的整个程序:
#include <stdio.h>
#include <cv.h>
#include "highgui.h"
#include "cxcore.h"
int main(int argc, char **argv) {
int key = 0;
int i = 0;
CvCapture *capture = 0; //The camera
IplImage* frame = 0; //The images you bring out of the camera
capture = cvCaptureFromCAM(0); //Open the camera
if (!capture ){
printf("Could not connect to camera\n" ); return 1;
}
cvQueryFrame(capture);
double fps = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
int frame_height = cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);
int frame_width = cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH);
cvNamedWindow("win", CV_WINDOW_AUTOSIZE);
cvMoveWindow("win", 100, 100);
CvVideoWriter *writer = cvCreateVideoWriter("out.avi", CV_FOURCC('D', 'I', 'V', 'X'), fps, cvSize(frame_width, frame_height), 1);
if (!writer) {
printf("CvVideoWriter failed\n");
exit(1);
}
IplImage *img = 0;
int nframes = 1000;
for (i = 0; i < nframes; i++) {
if (!cvGrabFrame(capture)) {
printf("Unable to grab frame!\n");
exit(0);
}
img = cvRetrieveFrame(capture, 0);
cvWriteFrame(writer, img);
cvShowImage("win", img);
key = cvWaitKey(20);
}
return 0;
}
我做错了什么?提前致谢。
【问题讨论】:
-
不需要发布吗? IE。
cvReleaseVideoWriter(&writer); -
@AlexanderReynolds 是的,我需要释放 VideoWriter 并销毁 Window,但在一个小程序中,这不是问题,因为它会在视频完成后发布。问题是,在写入“out.avi”后,该文件无法播放——但我拥有的其他 avi 文件可以播放。
-
创建程序退出时,'out.avi' 文件的大小是多少?
-
@AlexanderReynolds 原来是 OpenCV 中的一个错误,最新的 2.4 分支现在包含影响 MacOS 的修复
标签: c opencv computer-vision video-capture