【发布时间】:2026-01-14 07:50:01
【问题描述】:
我面临的问题是,当我尝试使用 Videowriter 保存图像时,它会创建视频文件,但大小为 0。我创建了一个标头,声明了一个保存视频的函数和一个单独的 .cpp 文件定义函数。当我只在一个文件中编写整个代码时,而不是像以前那样在单独的文件中,包括 ViceoCapture 和 VideoWriter,它运行良好,甚至以适当的文件大小保存视频文件。
当我开始调试时,我发现每次收到一帧但 VideoWriter.open 为 null 或者它说没有为 opencv_highgui.dll 加载符号
**savevideo.cpp file**
#include"SaveVideo.h"
int CSaveVideo::savevideo()//string InpVideo, int pinstatus)
{
VideoCapture oVcam(0);
Mat vFrame;
string OutPath = "C:\\Users\\20080031\\Desktop\\";
string Filename = "Vout.avi";
int vfourcc = CV_FOURCC('M', 'J', 'P', 'G');
int vfps = 20;
VideoWriter oVWrite;
oVWrite.open(Filename, vfourcc, vfps, Size(480, 640), true);
if (!oVcam.isOpened())
{
cout << "Camera not opened" << endl;
}
while (1)
{
oVcam.read(vFrame);
imshow("Input", vFrame);
waitKey(1);
oVWrite.write(vFrame);
}
}
CSaveVideo::CSaveVideo()
{
cout << "Inside Constructor" << endl;
}
CSaveVideo::~CSaveVideo()
{
//VideoCapture Vcam0;
cout << "Inside Distructor" << endl;
//Vcam0.release();
}
**saveVideo.h**
#ifndef SAVEVIDEO_H
#define SAVEVIDEO_H
#include<iostream>
#include<stdio.h>
#include<string>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/core/core.hpp>
#include <opencv2\opencv.hpp>
using namespace std;
using namespace cv;
class CSaveVideo
{
public:
CSaveVideo();
~CSaveVideo();
//string m_sGPIOpinstatus;
//char m_cSTOP;
int savevideo();//string PinStatus, char End, );
};
#endif SAVEVIDEO_H
**main.cpp**
#include"SaveVideo.h"
int main()
{
CSaveVideo save;
save.savevideo();
/*cout << out<<endl;*/
return 0;
}
【问题讨论】:
-
在 Windows 系统上:您的应用程序是否有权访问 opencv_ffmpeg dll 文件?请将其复制到应用程序文件夹或相应地设置 PATH 变量。如果找不到此 dll,OpenCV 不会抛出错误,但没有它,它将无法对视频进行编码。
-
你的图片尺寸是多少?我猜它是 640x480(宽 x 高)?那你得换成
Size(640, 480) -
你的循环中应该有一些术语标准。您应该关闭视频文件以确保系统正确写入并且播放器可以打开它。在循环之后尝试
if(waitKey(1) == 27) break;,然后是oVWrite.release();。然后在处理过程中按 esc 停止并关闭。 -
感谢 Micka,它起作用了。是的,您会建议这些更改。 VideoWriter.write 内部是否不会根据传入的帧调整大小?
-
尺寸不对