QT 使用opencv 打开usb相机

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

  1. 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
  1. 相机类的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();//关闭相机
相关推荐
敲上瘾16 小时前
Qt 主窗口QMainWindow:菜单栏、工具栏、状态栏与浮动窗口等
开发语言·c++·qt·ui·qt6.3
小此方1 天前
QT从入门到精通(一)Qt新手保姆级教程:从官网安装到项目实战,四步带你彻底搞懂Qt框架
开发语言·qt
ShineWinsu1 天前
对于Qt:TCP的解析
linux·c++·qt·网络协议·面试·tcp·客户端
bkspiderx1 天前
在Visual Studio中安装与配置Qt插件(Qt Visual Studio Tools)
ide·qt·visual studio·qt插件
无能者狂怒1 天前
机器视觉引导第二篇:眼在手上(Eye-in-Hand)单相机,为什么标定必须“方向取反”?
人工智能·数码相机·计算机视觉
ShineWinsu3 天前
对于Qt:信号量(QSemaphore)的解析
c++·qt·面试·笔试·客户端·线程安全·信号量
ShineWinsu4 天前
对于Qt:条件变量(QWaitCondition)的解析
c++·qt·面试·线程·条件变量·客户端·锁
郝学胜-神的一滴4 天前
Effective STL 条款12:对容器线程安全性的现实期望
c语言·开发语言·c++·qt·网络协议·rpc·numpy
敲上瘾4 天前
Qt 常用控件:按钮、显示输入、容器与布局等
开发语言·c++·qt·qt6.3
ShineWinsu5 天前
对于Qt:事件分发器以及事件过滤器的解析
c++·qt·面试·gui·客户端·事件过滤器·事件分发器