RK3588搭建QT开发环境并利用QT和opencv获取摄像头图像显示

目录

1 我的RK3588开发板

2安装opencv

3 安装QT

4 摄像头节点

5 安装向日葵

6 QT代码

6.1 qt_opencv_cmaera_demo.pro

6.2 mainwindow.ui

6.3 mainwindow.h

6.4 mainwindow.cpp

6.5 main.cpp

参考文献:


1 我的RK3588开发板

我把它放客厅了,连接电视。然后我在书房远程登录做开发,

2安装opencv

这里不下载源码编译了,直接用apt-get安装opencv

sudo apt-get update
sudo apt-get install libopencv-dev

然后find / -iname *opencv*就可以看到安装的opencv了

3 安装QT

 同样QT也不编译源码,直接用apt-get安装。

sudo apt-get install qt5-default
sudo apt-get install qt5-doc qt5-doc-html qtbase5-doc-html qtbase5-examples
sudo apt-get install qtcreator
sudo apt-get install qt5*

然后qmake -query可以看到安装的QT。 

4 摄像头节点

可以通过v4l2-ctl --list-devices来获取摄像头的节点。

5 安装向日葵

由于我需要编写界面程序,所以仅仅SSH远程登录不太够,我需要直接远程桌面,因此安装向日葵

去向日葵官网下载Linux arm64麒麟版本;

然后安装 

sudo apt-get install libappindicator3-1;
sudo dpkg -i SunloginClient_11.0.1.44968_kylin_arm.deb

6 QT代码

6.1 qt_opencv_cmaera_demo.pro

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

INCLUDEPATH += /usr/include/opencv4

LIBS += -L/path/to/your/opencv/libs \
        -lopencv_core \
        -lopencv_imgproc \
        -lopencv_highgui \
        -lopencv_ml \
        -lopencv_video \
        -lopencv_videoio \
        -lopencv_features2d \
        -lopencv_calib3d \
        -lopencv_objdetect \
        -lopencv_flann


# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#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

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

6.2 mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>952</width>
    <height>644</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>40</x>
      <y>10</y>
      <width>891</width>
      <height>411</height>
     </rect>
    </property>
    <property name="text">
     <string>TextLabel</string>
    </property>
   </widget>
   <widget class="QPushButton" name="openCameraButton">
    <property name="geometry">
     <rect>
      <x>190</x>
      <y>530</y>
      <width>131</width>
      <height>27</height>
     </rect>
    </property>
    <property name="text">
     <string>open camera</string>
    </property>
   </widget>
   <widget class="QPushButton" name="closeCameraButton">
    <property name="geometry">
     <rect>
      <x>590</x>
      <y>530</y>
      <width>131</width>
      <height>27</height>
     </rect>
    </property>
    <property name="text">
     <string>close camera</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>952</width>
     <height>24</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

6.3 mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QWidget>
#include <QImage>
#include <QDebug>
#include <QTimer>
#include <QPixmap>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc_c.h>
#include <opencv2/imgproc/types_c.h>


QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

public slots:
    void on_opencamera_clicked();//打开摄像头
    void readFrame();//读取一帧
    void on_closecamera_clicked();//关闭摄像头



private:
    Ui::MainWindow *ui;
    QTimer *timer;//计时器
    QImage image;//用QImage形式记录的图片
    cv::VideoCapture cap;//视频捕捉
    cv::Mat frame;//用mat形式记录的图片
    void init();
};
#endif // MAINWINDOW_H

6.4 mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <opencv2/opencv.hpp>
#include<QTimer>

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


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

void MainWindow::init()
{
    timer = new QTimer(this);
    connect(timer,&QTimer::timeout,this,&MainWindow::readFrame);//超时就捕捉一张图片
    connect(ui->openCameraButton, &QPushButton::clicked, this, &MainWindow::on_opencamera_clicked);
    connect(ui->closeCameraButton, &QPushButton::clicked, this, &MainWindow::on_closecamera_clicked);
}


void MainWindow::on_opencamera_clicked()//打开摄像头
{

    // 打开摄像头,参数为摄像头的设备节点
    cap.open("/dev/video1");
    if (!cap.isOpened()) {
        std::cout << "无法打开摄像头" << std::endl;
    }
    timer->start(25);        // 开始计时
}

void MainWindow::readFrame(){//读取帧并显示

    // 从摄像头捕获一帧图像
    cap >> frame;
    // 检查图像是否为空
    if (frame.empty()) {
        std::cout << "无法捕获图像" << std::endl;
        return;
    }

    // 将OpenCV的Mat图像转换为Qt的QImage
    QImage img = QImage((const unsigned char*)(frame.data), frame.cols, frame.rows, QImage::Format_RGB888).rgbSwapped();

    // 在QLabel中显示图像
    ui->label->setPixmap(QPixmap::fromImage(img));
}

void MainWindow::on_closecamera_clicked()//关闭摄像头
{
    cap.release();
}



6.5 main.cpp

#include "mainwindow.h"

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

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

#if 0
    // 创建一个VideoCapture对象
    cv::VideoCapture cap;

    // 打开摄像头,参数为摄像头的设备节点
    cap.open("/dev/video1");
    if (!cap.isOpened()) {
        std::cout << "无法打开摄像头" << std::endl;
        return -1;
    }
    // 创建一个Mat对象来存储摄像头的图像
    cv::Mat frame;
    while(1)
    {
        // 从摄像头捕获一帧图像
        cap >> frame;
        // 检查图像是否为空
        if (frame.empty()) {
            std::cout << "无法捕获图像" << std::endl;
            return -1;
        }
        // 显示图像
        cv::imshow("摄像头图像", frame);
        // 等待用户按键
        cv::waitKey(1);
    }

    // 释放摄像头
    cap.release();
#endif


    MainWindow w;
    w.show();
    return a.exec();
}

参考文献:

ArmSoM-W3之RK3588安装Qt+opencv+采集摄像头画面_rk3588 debian qt-CSDN博客

windows平台下Qt Creator的下载与安装流程_qt creator 下载-CSDN博客

RK3588安装向日葵-CSDN博客

2. Designer — [野火]嵌入式Qt应用开发实战指南——基于LubanCat-i.MX6ULL-MP157开发板 文档

1. Hello Qt — [野火]嵌入式Qt应用开发实战指南—基于LubanCat-RK开发板 文档

QT+opencv4实时获取摄像头的图像展示并附带截图功能_qt opencv 视频截图-CSDN博客

rk3588_demo: 在rk3588平台上,验证mpp、npu、驱动,流媒体等相关的功能。mpp包含,rockit,mpp,gstreamer,v4l2等; rockit vpss/vdec/venc/gdc/vgs/tde mpp:enc dec rga:npu 使用vi(rockit,v2l4)实时vo输出增加rtmp实时推流 - Gitee.com 

  • 11
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈 洪 伟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值