使用 opencv 和 Qt 录制视频
Posted
技术标签:
【中文标题】使用 opencv 和 Qt 录制视频【英文标题】:Video record using opencv and Qt 【发布时间】:2017-04-20 20:35:45 【问题描述】:我似乎在代码中找不到错误。代码编译完美,我可以开始视频,但我无法录制视频。
图像输出窗口
视频文件在所需目录中创建,但只有 10 kb。我已经尝试将four_cc更改为MPEG仍然没有效果。
#include "camerafeed.h"
#include "ui_camerafeed.h"
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <opencv/cv.h>
#include <QtCore>
using namespace cv;
using namespace std;
bool recording = false;
camerafeed::camerafeed(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::camerafeed)
ui->setupUi(this);
cap1.open(0);
Starttrigger = new QTimer(this);
// connect(Starttrigger,SIGNAL(timeout()),this,SLOT(processGui()));
camerafeed::~camerafeed()
delete ui;
void camerafeed::processGui()
int fcc = CV_FOURCC('D','I','V','X');
int fps = 20;
cap1.read(frame1);
Size frameSize(cap1.get(CV_CAP_PROP_FRAME_WIDTH),cap1.get(CV_CAP_PROP_FRAME_HEIGHT));
string filename1 = "D:\\Camera1\\video1.avi";
writer1 = VideoWriter(filename1,fcc,fps,frameSize);
bool bSuccess = cap1.read(frame1);
if(recording==true)
writer1.write(frame1);
putText(frame1,"[REC]",Point(0,30),5,1,Scalar(0,0,225));
cv::cvtColor(frame1,frame1,CV_BGR2RGB);
QImage qframe1((uchar*)frame1.data,frame1.cols,frame1.rows,frame1.step,QImage::Format_RGB888);
ui->camera1->setPixmap(QPixmap::fromImage(qframe1));
void camerafeed::on_Start_clicked()
Starttrigger->start(20);
connect(Starttrigger,SIGNAL(timeout()),this,SLOT(processGui()));
void camerafeed::on_stop_clicked()
Starttrigger->stop();
recording = false;
disconnect(Starttrigger,SIGNAL(timeout()),this,SLOT(processGui()));
void camerafeed::on_record_clicked()
recording = !recording;
【问题讨论】:
【参考方案1】:您的代码的一个问题是您正在使用该功能重新启动视频对象,我建议您仅在按下开始按钮时运行它。为此,最好创建一个执行此操作的函数。
CameraFeed::CameraFeed(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
ui->setupUi(this);
isRecorded = false;
trigger = new QTimer(this);
connect(trigger, &QTimer::timeout, this, &CameraFeed::processGui);
void CameraFeed::initCamera()
cap.open(0);
if(cap.isOpened())
qDebug() << "Camera successfully connected.";
void CameraFeed::initVideo()
std::string filename = "video.avi";
int fcc = CV_FOURCC('D','I','V','X');
int fps = 20;
cv::Size frameSize(cap.get(CV_CAP_PROP_FRAME_WIDTH),cap.get(CV_CAP_PROP_FRAME_HEIGHT));
writer = cv::VideoWriter(filename,fcc,fps,frameSize);
void CameraFeed::processGui()
if(cap.read(frame))
cv::cvtColor(frame,frame,CV_BGR2RGB);
if(isRecorded)
writer.write(frame);
cv::putText(frame,"[REC]",cv::Point(0,30),5,1,cv::Scalar(0,0,225));
QImage qframe((uchar*)frame.data,frame.cols,frame.rows,frame.step,QImage::Format_RGB888);
ui->camera1->setPixmap(QPixmap::fromImage(qframe));
void CameraFeed::on_start_clicked()
initCamera();
trigger->start(100);
void CameraFeed::on_stop_clicked()
if(cap.isOpened())
// Disconnect camera
cap.release();
qDebug() << "Camera successfully disconnected.";
trigger->stop();
void CameraFeed::on_record_clicked()
isRecorded = !isRecorded;
if(isRecorded)
initVideo();
完成code
【讨论】:
感谢解决了我的问题,但 isRecorded 必须是全局变量 全局是什么意思? 如果我在设置中初始化了 isRecorded,我无法在 CameraFeed::on_record_clicked() 中访问它,所以我不得不在外面声明它 IsRecorded 是该类的成员,您已在github.com/eyllanesc/***/tree/master/CameraFeed查看完整代码 哦,我没有看到完整的代码。谢谢 。只有这对我来说就足够了以上是关于使用 opencv 和 Qt 录制视频的主要内容,如果未能解决你的问题,请参考以下文章