视觉里程计06 Qt界面显示摄像头

界面主体

783913-20171022233334568-314770852.png

显示图像通过定时器定时调用信号槽里的更新函数实现

编写信号槽函数前需要先编译,这样才能更新界面的.h文件

具体实现

qtcameratest01.h修改如下:

#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_qtcameratest01.h"

#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/videoio.hpp> // for camera
using namespace cv;

class qtcameratest01 : public QMainWindow
{
    Q_OBJECT

public:
    qtcameratest01(QWidget *parent = Q_NULLPTR);
private:
    Ui::qtcameratest01Class ui;
    QTimer *timer;
    Mat frame;
    QImage image;
    VideoCapture cap1;
    private slots:
    void opencam();
    void nextFrame();
    void closeCamara();
    void camshot();
};
static QImage Mat2QImage(Mat& image);

qtcameratest01.cpp文件修改如下:

#include "qtcameratest01.h"
#include <QMessageBox>
#include <QTimer>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/videoio.hpp> // for camera
#include <opencv.hpp>
using namespace cv;


qtcameratest01::qtcameratest01(QWidget *parent)
    : QMainWindow(parent)
{
    // 初始化
    timer = new QTimer(this);
    timer->stop();
    ui.setupUi(this);
    connect(ui.OpenCamBtn, SIGNAL(clicked()), this, SLOT(opencam()));
    connect(timer, SIGNAL(timeout()), this, SLOT(nextFrame()));
    connect(ui.CloseCamBtn, SIGNAL(clicked()), this, SLOT(closeCamara()));
    connect(ui.CamshotBtn, SIGNAL(clicked()), this, SLOT(camshot()));
}
void qtcameratest01::opencam()
{
    if (cap1.isOpened())
        cap1.release();
    double rate = cap1.get(CV_CAP_PROP_FPS);
    try
    {
        cap1.open(0);
        
        cap1 >> frame;
        if (!frame.empty())
        {
            timer->setInterval(rate);
            timer->start();
        }
        
    }
    catch (const std::exception&)
    {
        QMessageBox::critical(NULL, "ERROR", "打开失败",QMessageBox::Close);
    }
}
static QImage Mat2QImage(Mat& image)
{
    QImage img;

    if (image.channels() == 3) {
        cvtColor(image, image, CV_BGR2RGB);
        img = QImage((const unsigned char *)(image.data), image.cols, image.rows,
            image.cols*image.channels(), QImage::Format_RGB888);
    }
    else if (image.channels() == 1) {
        img = QImage((const unsigned char *)(image.data), image.cols, image.rows,
            image.cols*image.channels(), QImage::Format_ARGB32);
    }
    else {
        img = QImage((const unsigned char *)(image.data), image.cols, image.rows,
            image.cols*image.channels(), QImage::Format_RGB888);
    }

    return img;
}

void qtcameratest01::nextFrame()
{
    cap1 >> frame;
    if (!frame.empty())
    {
        image = Mat2QImage(frame);
        QImage* imgScaled = new QImage;
        QImage* imgc = &image;
        *imgScaled = imgc->scaled(ui.campicreal->width(), ui.campicreal->height(), Qt::KeepAspectRatio);

        ui.campicreal->setPixmap(QPixmap::fromImage(*imgScaled));
    }

}
void qtcameratest01::closeCamara()
{
    timer->stop();//停止读取数据。
    cap1.release();//释放内存;  
}
void qtcameratest01::camshot()
{
    QImage* imgScaled = new QImage;
    QImage* imgc = &image;
    *imgScaled = imgc->scaled(ui.campicreal->width(), ui.campicreal->height(), Qt::KeepAspectRatio);

    ui.campicshot->setPixmap(QPixmap::fromImage(*imgScaled));
}

注意事项

  • 界面中用到的资源需要在ui setup时同时初始化,否则会出现内存错误
  • 界面更新方式通过定时器访问,摄像头资源需要释放
  • debug的话需要CDB,否则无法调试

转载于:https://www.cnblogs.com/RegressionWorldLine/p/7712709.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值