使用opencv和Qt的视频记录

时间:2017-04-20 20:35:45

标签: c++ qt opencv video

我似乎无法在代码中找到错误。代码编译完美,我能够启动视频,但我无法录制视频。

图像输出窗口

视频文件是在所需的目录中创建的,但它只有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 个答案:

答案 0 :(得分:0)

您的代码的一个问题是您正在使用该功能重新启动视频对象,我建议您仅在按下启动按钮时运行它。为此,最好创建一个执行此操作的函数。

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