1.在.pro文件中 添加opencv的头文件与lib 库
INCLUDEPATH += D:\opencv\build\install\include
LIBS += D:\opencv\build\install\x64\mingw\lib\libopencv_*.a
LIBS += D:\opencv\build\install\x64\mingw\bin\libopencv_*.dll
- usb相机类的头文件usbCamera.h
cpp
#ifndef USBCAMERA_H
#define USBCAMERA_H
#include <QObject>
#include <QDebug>
#include <QImage>
#include <QString>
#include <QThread>
#include <QTime>
#include <QApplication>
#include "opencv2/opencv.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/core.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "iostream"
// 相机类:提供相机的基本操作
class usbCamera : public QObject
{
Q_OBJECT
cv::VideoCapture capture; //视频流捕获器
static int cameracount; //存储相机数目
public:
int g_read_fps=0; //统计读取帧率
int g_disply_fps=0; //统计显示帧率
usbCamera();
~usbCamera();
bool openFlag=false;
bool open(int _index);
void close();
cv::Mat read();
cv::Mat matnow;
QImage Mat2QImage(cv::Mat const& src);
cv::Mat QImage2Mat(QImage const& src);
static int getCameraCount(){//获取可用相机数目
if(cameracount>0)return cameracount;
cv::VideoCapture _capture;
while(_capture.open(cameracount,cv::CAP_DSHOW)){
cameracount++;
_capture.release();
}
return cameracount;
}
signals:
void updateImage(QImage const&);
public slots:
void Operate(int); // 当Camera类被MovetoThread时,该槽函数相当于QThread::run()
};
#endif // USBCAMERA_H
- 相机类的cpp文件 usbCamera.cpp
cpp
#include "usbcamera.h"
int usbCamera::cameracount=0;
usbCamera::usbCamera()
{
}
usbCamera::~usbCamera()
{
if(!capture.isOpened())
{
capture.release();
}
}
bool usbCamera::open(int _index)
{
qDebug()<<"open index="<<_index;
// int desiredWidth = 1920; // 你想要的宽度
// int desiredHeight = 1080; // 你想要的高度
int desiredWidth = 1080; // 你想要的宽度
int desiredHeight = 720; // 你想要的高度 不能超过最大的分辨率
if(capture.open(_index, cv::CAP_DSHOW))
{
openFlag=true;
capture.set(cv::CAP_PROP_FPS, 30);
capture.set(cv::CAP_PROP_FRAME_WIDTH, desiredWidth);
capture.set(cv::CAP_PROP_FRAME_HEIGHT, desiredHeight);
capture.set(cv::CAP_PROP_FOURCC, cv::VideoWriter::fourcc('M','J','P','G'));
return true;
} else {
openFlag=false;
return false;
}
}
void usbCamera::close()
{
capture.release();
}
cv::Mat usbCamera::read()
{
cv::Mat mat;
capture.read(mat);
return mat;
}
void usbCamera::Operate(int _index)
{
if(open(_index))
{
qDebug()<<"Camera open success!";
} else {
qDebug()<<"Camera open failed";
return;
}
while(1)
{
//qApp->processEvents();
cv::Mat matin=read();
QImage image= Mat2QImage(matin);
emit updateImage(image);
}
}
QImage usbCamera::Mat2QImage(cv::Mat const& mat)
{
cv::Mat temp;
cv::cvtColor(mat, temp,CV_BGR2RGB);
QImage image((const uchar *) temp.data, temp.cols, temp.rows, temp.step, QImage::Format_RGB888);
image.bits();
return image;
}
cv::Mat usbCamera::QImage2Mat(QImage const& image)
{
cv::Mat tmp(image.height(),image.width(),CV_8UC3,(uchar*)image.bits(),image.bytesPerLine());
cv::Mat mat;
cv::cvtColor(tmp, mat,CV_BGR2RGB);
return mat;
}
4.对象申明
头文件:
#include "usbcamera.h"
usbCamera* cameraTraj;//轨迹抓取
void updateTrajImage(QImage);//traj 更新
5.初始化相机
cameraTraj = new usbCamera();
threadCameraTraj =new QThread();
cameraTraj->moveToThread(threadCameraTraj);//将camera对象放在子线程
threadCameraTraj->start();
connect(this, SIGNAL(cameraOperate(int)), cameraTraj, SLOT(Operate(int)));//打开相机
connect(cameraTraj, SIGNAL(updateImage(QImage)), this, SLOT(updateTrajImage(QImage)));//图片更新
emit cameraOperate(0);//开启第一个相机
//更新图片
void MainForm::updateTrajImage(QImage image)
{
cameraTraj->g_read_fps++;//读取帧率
if(!image.isNull()&&startTask==false)
{
ui->TrajLabel->setPixmap(QPixmap::fromImage(image.scaled(ui->TrajLabel->size(), Qt::KeepAspectRatio)));//显示图片
ui->TrajLabel->setScaledContents(true);//根据QLabel
的当前大小来调整图片的大小
}
}
5.结束销毁
threadCameraTraj->terminate();//结束线程
cameraTraj->close();//关闭相机