1.新建一个QT项目QT_VS_Capture.sln(Qt Application)
2.设置头文件路径
项目-属性-VC++目录
(1)设置包含目录QT5.6\5.6\msvc2015_64\include
(2)设置库目录QT5.6\5.6\msvc2015_64\lib
3.点击qt_vs_capture.ui
4.添加一个label控件,如下图
label控件属性设置如下
6.返回VS2015,在qt_vs_capture.h的QT_VS_Capture类中声明成员变量和槽函数如下:
#ifndef QT_VS_CAPTURE_H
#define QT_VS_CAPTURE_H
#include
#include "GeneratedFiles\ui_qt_vs_capture.h"
#include
#include
#include
using namespace cv;
class QT_VS_Capture : public QMainWindow
{
Q_OBJECT
public:
QT_VS_Capture(QWidget *parent = 0);
~QT_VS_Capture();
private:
Ui::QT_VS_CaptureClass ui;
QTimer *time_clock;
QImage img;
private slots:
void readcamera();
};
#endif // QT_VS_CAPTURE_H
7.在qt_vs_capture.cpp中定义如下:
#include "qt_vs_capture.h"
VideoCapture capture(0);
QT_VS_Capture::QT_VS_Capture(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
time_clock = new QTimer();
time_clock->setInterval(10);
time_clock->start();
connect(time_clock, SIGNAL(timeout()), this, SLOT(readcamera()));
}
QT_VS_Capture::~QT_VS_Capture()
{
}
void QT_VS_Capture::readcamera()
{
Mat frame;
capture >> frame;
cvtColor(frame, frame, CV_BGR2RGB);
img = QImage((const unsigned char*)frame.data, frame.cols, frame.rows, QImage::Format_RGB888);
ui.label->setPixmap(QPixmap::fromImage(img));
ui.label->resize(ui.label->pixmap()->size());
ui.label->show();
}
8.运行如下