首頁 > 軟體

基於Qt OpenCV實現影象資料採集軟體

2022-07-04 18:04:55

效果圖

範例程式碼

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include<QTimer>
#include<QDebug>
#include<QDateTime>
#include<opencv2/opencv.hpp>

using namespace cv;
using namespace std;

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private slots:
    void updateLabel();//重新整理label
    void on_button_getimage_start_clicked();

    void on_button_getimage_end_clicked();

    void on_button_opencapture_clicked();

private:
    Ui::Widget *ui;
    VideoCapture capture;
    QTimer *updatetimer;//視訊重新整理定時器
    Mat src;
    QImage image_src;
    bool saveflag;
    int i=0;
};
#endif // WIDGET_H

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    saveflag=false;
    this->setWindowTitle("image capturer");
    //關於重新整理
    updatetimer=new QTimer(this);
    connect(updatetimer,&QTimer::timeout,this,&Widget::updateLabel);
}

Widget::~Widget()
{
    delete ui;
}

//視訊重新整理
void Widget::updateLabel()
{
    if(!capture.isOpened())
    {
        qDebug()<<"capture error!";
        return;
    }
    else
    {
        capture>>src;
        if(src.empty())
        {
            qDebug()<<"src is empty!";
            return;
        }
        else
        {
            if(saveflag)
            {
                QDateTime now_time = QDateTime::currentDateTime();
                QString file_n_qstr="D:\image_get\cup\"+now_time.toString("yyyyMMddhhmmss")+QString::number(i)+".jpg";
                string file_n_str=file_n_qstr.toStdString();
                imwrite(file_n_str,src);
                qDebug()<<file_n_qstr;
                i++;
                //save
            }
            //原圖
            image_src =QImage((const unsigned char*)(src.data),src.cols,src.rows,src.cols*src.channels(),QImage::Format_BGR888);
            ui->label_show->setPixmap(QPixmap::fromImage(image_src));
        }
    }
}

void Widget::on_button_getimage_start_clicked()
{
    qDebug()<<"on_button_getimage_start_clicked";
    saveflag=true;
}

void Widget::on_button_getimage_end_clicked()
{
    qDebug()<<"on_button_getimage_end_clicked";
    saveflag=false;
}

void Widget::on_button_opencapture_clicked()
{
    qDebug()<<"start button is clicked";

    capture.open(0);
    if(!capture.isOpened())
    {
        qDebug()<<"open capture failed!";
    }
    else
    {
        updatetimer->start(30);
        qDebug()<<"open capture success!";
    }
}

到此這篇關於基於Qt OpenCV實現影象資料採集軟體的文章就介紹到這了,更多相關Qt OpenCV影象資料採集軟體內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com


IT145.com E-mail:sddin#qq.com