各模块的基本实现——1. 在Qt界面上显示树莓派摄像头捕获到的图像


文章目录

  • 各模块的基本实现——1. 在Qt界面上显示树莓派摄像头捕获到的图像
  • 一些废话
  • 一、思路
  • 二、代码
  • 三、现象
  • 总结



一些废话

我——一个很久之前做过嵌入式程序开发现在逐渐变为MATLAB玩家的小菜鸡。
前一段时间发现竟然连全局变量怎么声明都快忘记了,瞬间陷入焦躁不安忧虑难过中…
遂赶紧开始码代码并刷一波C++语法书安慰一下自己。
一个小功能实现之后,感觉回了一点血,记忆仿佛又回来了ヾ(◍°∇°◍)ノ゙~

这篇文章用一个小demo实现摄像头模块的驱动,即在Qt界面上显示树莓派摄像头捕获到的图像,具备一些基本的人机交互功能。
因为在第一章中我们已经完成了交叉编译环境的配置,后面的程序开发均是在PC端的Ubuntu18.04环境下,并将可执行文件放在树莓派上执行。

一、思路

demo的功能和实现的基本思路见下图。

树莓派图像识别摄像头设置_树莓派图像识别摄像头设置

二、代码

Show You My Code~主要是3个文件mainwindow.h,main.cpp和mainwindow.cpp
mainwindow.ui中的操作比较简单,不再赘述。

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QLabel>
#include <QObject>
#include <QToolBar>
#include <QAction>
#include <QTimer>

#include <opencv2/opencv.hpp>
#include <iostream>


QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

private:
    QToolBar *cameraTool;
    QAction *actStartCamera;
    QAction *actStopCamera;
    QAction *actImageCapture;

    cv::VideoCapture g_cap;
    QTimer *timer;
    cv::Mat frame;
    QImage frameCap;

    bool frameCaptureFlag = false;

    void initTimer();
    void initCamera();
    void createToolBars();
    void createActions();

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void slot_actStartCamera_triggered();
    void slot_actStopCamera_triggered();
    void slot_actCapture_triggered();

    void slot_timer_timeout();

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

main.cpp

#include "mainwindow.h"

#include <QApplication>

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"


using namespace std;
using namespace cv;


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    createActions();    //初始化Actions
    createToolBars();    //初始化工具条

    initTimer();    //初始化定时器
    initCamera();    //初始化摄像头
}




MainWindow::~MainWindow()
{
    delete ui;
}




/**************************************
    函数名称:MainWindow::createActions
    函数功能:初始化Actions
    入口参数:无
    出口参数:无
**************************************/
void MainWindow::createActions()
{
    //actStartCamera = new QAction(QIcon("startcamera.jpg"), tr("开启摄像头"), this);
    actStartCamera = new QAction(tr("开启摄像头"), this);
    actStartCamera->setEnabled(true);
    connect(actStartCamera, SIGNAL(triggered()), this, SLOT(slot_actStartCamera_triggered()));

    actStopCamera = new QAction(tr("关闭摄像头"), this);
    actStopCamera->setEnabled(false);
    connect(actStopCamera, SIGNAL(triggered()), this, SLOT(slot_actStopCamera_triggered()));

    actImageCapture = new QAction(tr("抓取图片"), this);
    actImageCapture->setEnabled(false);
    connect(actImageCapture, SIGNAL(triggered()), this, SLOT(slot_actCapture_triggered()));
}




/**************************************
    函数名称:MainWindow::createToolBars
    函数功能:初始化工具条
    入口参数:无
    出口参数:无
**************************************/
void MainWindow::createToolBars()
{
    cameraTool = addToolBar("camera");    //新建工具条
    cameraTool->addAction(actStartCamera);
    cameraTool->addAction(actStopCamera);
    cameraTool->addAction(actImageCapture);
}



/**************************************
    函数名称:MainWindow::initCamera
    函数功能:初始化摄像头设备
    入口参数:无
    出口参数:无
**************************************/
void MainWindow::initCamera()
{
    int deviceID = 0;
    int apiID = cv::CAP_ANY;
    g_cap.open(deviceID, apiID);

    if (!g_cap.isOpened())
    {
        cerr << "ERROR! Unable to open camera.\n";
    }

    cout << "Camera opened." << endl;
}




/**************************************
    函数名称:MainWindow::initTimer
    函数功能:初始化定时器
    入口参数:无
    出口参数:无
**************************************/
void MainWindow::initTimer()
{
    timer = new QTimer(this);
    timer->stop();
    connect(timer, SIGNAL(timeout()), this, SLOT(slot_timer_timeout()));
}




/**************************************
    函数名称:MainWindow::slot_actStartCamera_triggered
    函数功能:“开启摄像头”按钮触发的槽函数.
    入口参数:无
    出口参数:无
**************************************/
void MainWindow::slot_actStartCamera_triggered()
{
    if(timer->isActive() == false)
    {
        timer->start(5);    //开启定时器
        cout << "Timer opened." << endl;

        actStartCamera->setEnabled(false);
        actStopCamera->setEnabled(true);
        actImageCapture->setEnabled(true);
    }
}




/**************************************
    函数名称:MainWindow::slot_actStopCamera_triggered
    函数功能:“关闭摄像头”按钮触发的槽函数.
    入口参数:无
    出口参数:无
**************************************/
void MainWindow::slot_actStopCamera_triggered()
{
    if(timer->isActive() == true)
    {
        timer->stop();    //关闭定时器

        actStartCamera->setEnabled(true);
        actStopCamera->setEnabled(false);
        actImageCapture->setEnabled(false);
    }
}




/**************************************
    函数名称:MainWindow::slot_timer_timeout
    函数功能:定时器溢出触发的槽函数.
    入口参数:无
    出口参数:无
**************************************/
void MainWindow::slot_timer_timeout()
{
    g_cap.read(frame);
    //cout << "Timer slot executed." << endl;

    if(frame.empty())
    {
        cerr << "ERROR! blank frame grabbed!" << endl;
    }

    cvtColor(frame, frame, COLOR_BGR2RGB);
    QImage qframe = QImage((const unsigned char*)(frame.data), frame.cols, frame.rows, frame.step, QImage::Format_RGB888);

    ui->viewFinder->setPixmap(QPixmap::fromImage(qframe));
    ui->viewFinder->resize(qframe.size());
    ui->viewFinder->show();

    if(frameCaptureFlag == true)
    {
        frameCaptureFlag = false;

        frameCap = qframe;
        ui->frameCaptured->setPixmap(QPixmap::fromImage(frameCap));
        ui->frameCaptured->resize(frameCap.size());
        ui->frameCaptured->show();
    }
}




/**************************************
    函数名称:MainWindow::on_cameraStateChanged
    函数功能:“抓取图像”按钮触发的槽函数.
    入口参数:无
    出口参数:无
**************************************/
void MainWindow::slot_actCapture_triggered()
{
    frameCaptureFlag = true;
}

三、现象

现象如下图(不要在意我拍了个什么鬼东西,毕竟是测试,有图就行 ( ̄▽ ̄) )。

树莓派图像识别摄像头设置_qt_02

总结

✿✿ヽ(°▽°)ノ✿