如何在OpenCV中将输出视频保存到文件中
我想将输出视频保存到文件中,而不是显示它,并尝试使用cvcaptureimage,但仍然无法获得结果如何在OpenCV中将输出视频保存到文件中,c,video,opencv,C,Video,Opencv,我想将输出视频保存到文件中,而不是显示它,并尝试使用cvcaptureimage,但仍然无法获得结果 #include <highgui.h> int main(int argc, char *argv[]) { // Create a "Capture" object from 1st command line argument CvCapture *video = cvCaptureFromFile(argv[1]); // this is the st
#include <highgui.h>
int main(int argc, char *argv[])
{
// Create a "Capture" object from 1st command line argument
CvCapture *video = cvCaptureFromFile(argv[1]);
// this is the structure that will store the frames
IplImage *frame = NULL;
// create a window to display the video
cvNamedWindow("Video", CV_WINDOW_AUTOSIZE);
// get the next frame
while (frame = cvQueryFrame(video) )
{
// display it in the window we created
cvShowImage("Video", frame);
// wait 1000/fps milliseconds
cvWaitKey((int)(1000/cvGetCaptureProperty(video, CV_CAP_PROP_FPS)));
//reading into .avi file
CvCapture*capture=0
capture=cvcreateFileCapture(arg[1]);
if(!capture)
{
// intiate the video read
IpLImage *bgr_frame=cvQueryFrame(capture);
double fps=cvGetCaptureProperty(capture,CV_CAP_PROP_FPS);
}
}
}
我能知道我在代码中重复的错误吗?您似乎在为循环中的每一帧创建一次视频文件
在开始时创建一次avi文件,然后使用cvWriteFrame将每个新帧添加到该文件中,如果需要检查参考代码并开始使用 无论如何,下面的代码显示网络摄像头的视频。如果你仔细观察,你会发现有一个从彩色框架到灰度版本的转换,并且灰度显示在窗口上 灰度帧也记录在磁盘上名为out.avi的文件中。您必须知道,这段代码无法在带有OpenCV 2.1的Mac OS X上运行,因为OpenCV需要使用ffmpeg进行编译,以允许将视频编码到文件中 在Windows上,cvCreateVideoWriter将显示一个对话框,供您选择保存视频时要使用的适当编解码器。可以通过设置一个参数来更改此函数调用,该参数将对您首选的编解码器进行硬编码 我写得很匆忙,但我知道它是有效的
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cv.h>
#include <highgui.h>
#include <cxtypes.h>
#include <cvaux.h>
int main()
{
int camera_index = 0;
IplImage *color_frame = NULL;
int exit_key_press = 0;
CvCapture *capture = NULL;
capture = cvCaptureFromCAM(camera_index);
if (!capture)
{
printf("!!! ERROR: cvCaptureFromCAM\n");
return -1;
}
double cam_w = cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH);
double cam_h = cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);
double fps = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
printf("* Capture properties: %f x %f - %f fps\n", cam_w, cam_h, fps);
cvNamedWindow("Grayscale video", CV_WINDOW_AUTOSIZE);
CvVideoWriter* writer = NULL;
writer = cvCreateVideoWriter("out.avi", -1, fps, cvSize((int)cam_w,(int)cam_h), 1);
if (writer == NULL)
{
printf("!!! ERROR: cvCreateVideoWriter\n");
return -1;
}
while (exit_key_press != 'q')
{
color_frame = cvQueryFrame(capture);
if (color_frame == NULL)
{
printf("!!! ERROR: cvQueryFrame\n");
break;
}
IplImage* gray_frame = cvCreateImage(cvSize(color_frame->width, color_frame->height), color_frame->depth, 1);
if (gray_frame == NULL)
{
printf("!!! ERROR: cvCreateImage\n");
continue;
}
cvCvtColor(color_frame, gray_frame, CV_BGR2GRAY);
cvShowImage("Grayscale video", gray_frame);
cvWriteFrame(writer, gray_frame);
cvReleaseImage(&gray_frame);
exit_key_press = cvWaitKey(1);
}
cvReleaseVideoWriter(&writer);
cvDestroyWindow("Grayscale video");
cvReleaseCapture(&capture);
return 0;
}
我知道这已经很老了,但我不能同意关于OSX的说法 您可以在OSX上轻松构建和编译FFMPEG。无需使用QTKIT/Quicktime。实际上我在OSX上写视频没有任何问题 另一个建议是使用新的OpenCV 2.0+结构,而不是使用那些不推荐使用的结构。使用Mat代替IplImage/cvMat,使用VideoWriter和VideoCapture
Mat image;
VideoCapture capture;
VideoWriter out;
capture = VideoCapture(-1);
image << capture;
out.write(image);
因为你还没有发布图像。这将导致内存中断。
您需要释放循环中的图像。请使用代码标记。现在难以辨认。更新了我的答案并附上了您正在寻找的源代码。我得到了错误-->捕获属性:1280.000000 x 720.000000-0.000000 fps OpenCV错误:断言失败scn==3 | | scn==4,在cvtColor中,file/Users/venushka/Documents/OpenCV/modules/imgproc/src/color.cpp,第3648行libc++abi.dylib:终止于cv::exception:/Users/venushka/Documents/opencv/modules/imgproc/src/color.cpp:3648:错误:-215 scn==3 | | scn==4函数CVTColor这应该是注释而不是答案。请添加更多描述,使其成为一个好答案。@Raju对此表示抱歉。我是新来的。有没有一个简单的C/C++库来读/写未压缩的视频文件?