效果图
示例代码
#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图像数据采集软件内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网