视频捕获并使用OpenCV保存
Video capture and save using OpenCV
下面给出的是我使用的代码,用于从网络摄像头获取视频并将其保存在硬盘上。在运行程序时,它说"视频作者没有打开"。我要去哪里?
#include <opencvcv.h>
#include <opencv2highguihighgui.hpp>
#include <opencv2imgprocimgproc.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;
}
}
}
我已经阅读了这里和这里给出的答案,但不明白我会在哪里出错。
尝试其他编解码器
cv_fourcc('p','i','m','1')= mpeg-1 codec
cv_fourcc('m','j','p','g')=运动 - jpeg编解码器(效果不佳)
cv_fourcc('m','p','4','2')= mpeg-4.2 codec
cv_fourcc('d','i','v','3')= mpeg-4.3 codec
cv_fourcc('d','i','v','x')= mpeg-4 codec
cv_fourcc('u','2','6','3')= H263编解码器
cv_fourcc('i','2','6','3')= h263i codec
cv_fourcc('f','l','v','1')= flv1 codec
复制从这里粘贴。我设法用cv_fourcc('f','l','v','1'编写视频。
顺便说一句,当然应该在计算机上安装编解码器。
根据您的代码,我无法理解为什么在循环时每次创建窗口Display
,也可以每次都会初始化VideoWriter
对象。我对您的代码进行了稍微修改,请尝试,它可能会帮助您
#include <opencvcv.h>
#include <opencv2highguihighgui.hpp>
#include <opencv2imgprocimgproc.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;
}
namedWindow("Display",CV_WINDOW_AUTOSIZE);
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH);
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
Size frameSize(static_cast<int>(dWidth), static_cast<int>(dHeight));
VideoWriter oVideoWriter ("c:\out.avi", CV_FOURCC('P','I','M','1'), 20, frameSize,true);
if ( !oVideoWriter.isOpened() )
{
cout << "ERROR: Failed to write the video" << endl;
return -1;
}
while(true)
{
Mat frame;
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "ERROR: Cannot read a frame from video file" << endl;
break;
}
oVideoWriter.write(frame); //writer the frame into the file
imshow("Display", frame);
if (waitKey(10) == 27)
{
cout << "esc key is pressed by user" << endl;
break;
}
}
}
相关文章:
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- 无法从Java读取C++中保存的Opencv Mat映像
- 在opencv c++目录中从/加载/保存多个图像
- 如果我想通过OpenCV dnn模块加载PyTorch的模型,我应该如何保存它
- OpenCV imwrite不会保存图像
- 如何将捕获的图像存储在OpenCV中(将图片保存到计算机)
- 有没有办法拿一个opencv显示窗口并将其保存到GUI窗口?
- 对 OpenCV C++ 程序进行单元测试,该程序拍摄照片并将其保存到文件中
- Opencv 保存视频
- 我想将 OpenCV 中处理后的图像保存为“input_name+enhanced.input_image_format
- OpenCV - 在矢量<Mat>中保存垫子
- 视频编写器功能不会使用 OpenCV-3.0.0 保存文件
- 使用 OpenCV 保存 .pgm 图像时出错
- 视频捕获并使用OpenCV保存
- OpenCV 3.1.0:保存并加载经过训练的SVM
- 训练 SVM 并使用 OpenCV 3.0 保存它
- 无法使用OpenCV保存图像
- OpenCV保存CV_32FC1图像
- Opencv保存从网络摄像头捕获的图像
- 使用OpenCV保存整数CV_32S图像