下面给出了我用于从网络摄像头获取视频并将其保存在硬盘上的代码 . 在运行该程序时,它说“视频编写器没有打开” . 我哪里错了?
#include <opencv\cv.h>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <stdio.h>
#include <iostream>
#pragma comment(lib, "Ws2_32.lib")
#define default_buflen 1024
using namespace std;
using namespace cv;
#define default_port "1234"
int main(int argc, char** argv)
{
Mat capture;
VideoCapture cap(0);
if(!cap.isOpened())
{
cout<<"Cannot connect to camera"<<endl;
getchar();
return -1;
}
double fps=30;
Size s=Size((int)cap.get(CV_CAP_PROP_FRAME_WIDTH),(int)cap.get(CV_CAP_PROP_FRAME_WIDTH));
VideoWriter vidcapt;
vidcapt.open("c:\\out.avi",CV_FOURCC('D','I','V','X'),cap.get(CV_CAP_PROP_FPS),s,true);
if(!vidcapt.isOpened())
{
cout<<"Video writer not opening"<<endl;
getchar();
return -1;
}
while(true)
{
cap>>capture;
namedWindow("Display",1);
imshow("Display",capture);
vidcapt<<capture;
int ch=waitKey(5);
if(char(ch)==27)
{
break;
}
}
}
2 回答
根据你的代码,我无法理解为什么你每次在while循环中都创建了窗口
Display
,并且每次出错时都会初始化VideoWriter
对象 . 我稍微修改了您的代码,请尝试,它可能会帮助您尝试其他编解码器
复制粘贴自here . 我设法用CV_FOURCC('F','L','V','1')写视频 .
顺便说一下,当然应该在你的机器上安装编解码器 .