整体文件
Camera.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += C++11
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
INCLUDEPATH += /usr/local/include \
/usr/local/include/opencv4 \
/usr/local/include/opencv/opencv2
LIBS += /usr/local/lib/libopencv_highgui.so \
/usr/local/lib/libopencv_core.so \
/usr/local/lib/libopencv_imgproc.so \
/usr/local/lib/libopencv_imgcodecs.so \
/usr/local/lib/libopencv_videoio.so
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
QT +=sql
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDebug>
#include <QTimer>
#include <QImage>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/types_c.h>
using namespace cv;
using namespace std;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QImage Mat2QImage(Mat cvImg);
private slots:
void openCamara(); // 打开摄像头
void readFarme(); // 读取当前帧信息
void closeCamara(); // 关闭摄像头。
void takingPictures(); // 拍照
private:
Ui::MainWindow *ui;
QTimer *timer;
QImage imag;
Mat cap,cap_gray,cap_tmp; //定义一个Mat变量,用于存储每一帧的图像
VideoCapture capture; //声明视频读入类
};
#endif // MAINWINDOW_H
main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QMainWindow>
#include <QDebug>
#include <QTimer>
#include <QImage>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/types_c.h>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(readFarme())); // 时间到,读取当前摄像头信息
connect(ui->open, SIGNAL(clicked()), this, SLOT(openCamara()));
connect(ui->take, SIGNAL(clicked()), this, SLOT(takingPictures()));
connect(ui->close, SIGNAL(clicked()), this, SLOT(closeCamara()));
}
//打开摄像头
void MainWindow::openCamara()
{
capture.open(0); //从摄像头读入视频如果设备只有一个摄像头就传入参数0
qDebug("open");
if (!capture.isOpened()) //先判断是否打开摄像头
{
qDebug("err");
}
timer->start(20); // 开始计时,20ms获取一帧
}
//读取摄像头信息
void MainWindow::readFarme()
{
capture>>cap; //读取当前帧
if (!cap.empty()) //判断当前帧是否捕捉成功 **这步很重要
{
imag = Mat2QImage(cap);
imag = imag.scaled(ui->camera->width(), ui->camera->height(),
Qt::IgnoreAspectRatio, Qt::SmoothTransformation);//设置图片大小和label的长宽一致
//imshow(name, cap); //若当前帧捕捉成功,显示
ui->camera->setPixmap(QPixmap::fromImage(imag)); // 将图片显示到label上
}
else
qDebug("can not ");
}
// 拍照
void MainWindow::takingPictures()
{
capture>>cap; //读取当前帧
if (!cap.empty()) //判断当前帧是否捕捉成功 **这步很重要
{
imag = Mat2QImage(cap);
imag = imag.scaled(ui->photo->width(), ui->photo->height(),
Qt::IgnoreAspectRatio, Qt::SmoothTransformation);//设置图片大小和label的长宽一致
//imshow(name, cap); //若当前帧捕捉成功,显示
ui->photo->setPixmap(QPixmap::fromImage(imag)); // 将图片显示到label上
}
else
qDebug("can not ");
}
//关闭摄像头,释放资源,必须释放***
void MainWindow::closeCamara()
{
timer->stop(); // 停止读取数据。
}
// 图片转换(网上抄的)
QImage MainWindow::Mat2QImage(Mat cvImg)
{
QImage qImg;
if(cvImg.channels()==3) //3 channels color image
{
cv::cvtColor(cvImg,cvImg,CV_BGR2RGB);
qImg =QImage((const unsigned char*)(cvImg.data),
cvImg.cols, cvImg.rows,
cvImg.cols*cvImg.channels(),
QImage::Format_RGB888);
}
else if(cvImg.channels()==1) //grayscale image
{
qImg =QImage((const unsigned char*)(cvImg.data),
cvImg.cols,cvImg.rows,
cvImg.cols*cvImg.channels(),
QImage::Format_Indexed8);
}
else
{
qImg =QImage((const unsigned char*)(cvImg.data),
cvImg.cols,cvImg.rows,
cvImg.cols*cvImg.channels(),
QImage::Format_RGB888);
}
return qImg;
}
MainWindow::~MainWindow()
{
delete ui;
}
mainwindow.ui
运行编译即可。这里我使用的是opencv4.5.4版本,opencv3.x版本缺少很多深度学习的hpp,cpp,可能会报错,以及pro文件的地址一定要正确。