ubuntu16.04系统下QT+opencv+realsense435i

2023-05-16

ubuntu16.04系统下QT+opencv+realsense435i

    • pro文件的内容如下所示。
    • mainwindow.h文件的内容是:
    • main.cpp文件的内容如下
    • mainwindow的内容如下
    • 设计的界面是这样的
    • 最后的效果图

本文在ubuntu16.04系统下,将realsen435i的深度图像和彩色图像显示在QT界面上,并完成了截图和截取一段视频的功能,并将截取的图片或者视频以当前的时间点来命名保存到工程所在的文件夹。我的工程文件名叫做opencvrealsense。
本文基于opencv和realsense驱动包安装完成的基础上,这部分网上有很多教程,可以自行查询,如有问题也可以给我留言啦,很乐意一起交流进步。

pro文件的内容如下所示。

重点就是QT配置opencv和realsense,网上有很多教程说是在配置时需要把路径改成自己安装opencv和realsense的路径,我也没改直接这样写的竟然运行成功了,大家可以试试直接复制粘贴能不能行。

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

DEFINES += QT_DEPRECATED_WARNINGS

TARGET = untitled
TEMPLATE = app


SOURCES += \
    main.cpp \
    mainwindow.cpp

HEADERS += \
    mainwindow.h

FORMS += \
    mainwindow.ui

#system
INCLUDEPATH += /usr/local/include \
               /usr/include \
               /usr/lib/x86_64-linux-gnu

LIBS += -L/usr/local/lib
LIBS += -L/usr/lib

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

//配置opencv
INCLUDEPATH += /usr/local/include \
               /usr/local/include/opencv \
               /usr/local/include/opencv2

LIBS += -L /usr/local/lib/ -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_imgcodecs
LIBS += -L /usr/local/lib/libopencv_*.so
LIBS += -lopencv_core \
-lopencv_imgproc \
-lopencv_highgui \
-lopencv_ml \
-lopencv_video \
-lopencv_features2d \
-lopencv_calib3d \
-lopencv_objdetect \
-lopencv_flann

//配置realsense
INCLUDEPATH +=/usr/local/include/librealsense2
LIBS += /usr/local/lib/librealsense2.so

mainwindow.h文件的内容是:

(可能有一些头文件也用不到,但是加上也没有影响,我就一直加着呢)

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtWidgets/QMainWindow>
#include <QTimer>
#include <QFileDialog>
#include <QMessageBox>
#include <QDateTime>
#include "opencv2/opencv.hpp"//添加Opencv相关头文件
#include <librealsense2/rs.hpp>//realsense相关头文件
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>

using namespace std;
using namespace cv;
using namespace rs2;

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent =Q_NULLPTR);
    ~MainWindow();

private slots:
    void on_pushButton_clicked();
    void on_pushButton_2_clicked();
    void update_window();
    void update_window2();
    void on_pushButton_3_clicked();
    void on_pushButton_4_clicked();
    void on_pushButton_5_clicked();

private:
    Ui::MainWindow *ui;
    QTimer *timer,*timerr;
    QImage depth_image,color_image;
    IplImage *imgIpl;
};
#endif

main.cpp文件的内容如下

这部分我就加了一句话,把界面名字改了。

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.setWindowTitle("zyl的界面");
    w.show();
    return a.exec();
}

mainwindow的内容如下

这部分就是代码的关键部分,可能我写的比较乱看不懂的话可以给我留言呀!

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "librealsense2/rs.hpp"
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>
#include <cxcore.h>

using namespace cv;
using namespace std;
using namespace rs2;

Mat depth_img, color_img;
colorizer color_map;
pipeline pipe;
VideoWriter writer;
int cw,ch;
int c;


MainWindow::MainWindow(QWidget *parent):
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    timer = new QTimer(this);
    timerr = new QTimer(this);
}



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

void MainWindow::on_pushButton_clicked()//打开相机
{
            connect(timer, SIGNAL(timeout()), this, SLOT(update_window()));
            timer->start(20);
            pipe.start();
            ui->label_3->setText("camera is open");
}



void MainWindow::on_pushButton_2_clicked()//关闭相机
{
     pipe.stop();
     timer->stop(); //停止取帧
     ui->label->clear();
     ui->label_2->clear();
     ui->label_3->setText("camera is closed");
}

void MainWindow::update_window()
{
        frameset data = pipe.wait_for_frames(); // Wait for next set of frames from the camera
        frame depth = data.get_depth_frame().apply_filter(color_map);

        const int w = depth.as<video_frame>().get_width();// 查询帧大小(宽度和高度)
        const int h = depth.as<video_frame>().get_height();


        // 从着色的深度数据中创建OpenCV大小(w,h)的OpenCV矩阵
        depth_img = Mat(Size(w, h), CV_8UC3, (void*)depth.get_data(), Mat::AUTO_STEP);
        depth_image = QImage((const unsigned char*)(depth_img.data), depth_img.cols, depth_img.rows, QImage::Format_RGB888);
        ui->label_2->setPixmap(QPixmap::fromImage(depth_image));
        ui->label_2->setFixedSize(480, 260);
        ui->label_2->setScaledContents(true);



        frame color = data.get_color_frame();//获取RGB图
        const int cw = color.as<video_frame>().get_width();// 查询帧大小(宽度和高度)
        const int ch = color.as<video_frame>().get_height();

        color_img = Mat(Size(cw, ch), CV_8UC3, (void*)color.get_data(), Mat::AUTO_STEP);//帧转化为Mat
        //帧转化为QImage
        color_image = QImage((const unsigned char*)(color_img.data), color_img.cols, color_img.rows, QImage::Format_RGB888);

        ui->label->setPixmap(QPixmap::fromImage(color_image));
        ui->label->setFixedSize(480, 260);
        ui->label->setScaledContents(true);
}



void MainWindow::on_pushButton_3_clicked()//截图
{
    //帧转化为QImage
    color_image = QImage((const unsigned char*)(color_img.data), color_img.cols, color_img.rows, QImage::Format_RGB888);
    //在label_4上将截图显示3秒,然后保存
    ui-hlabel_4->setPixmap(QPixmap::fromImage(color_image));
    ui->label_4->setFixedSize(480, 260);
    ui->label_4->setScaledContents(true);

    waitKey(3000);
    const QPixmap *img = ui->label_4->pixmap();
    QDateTime time = QDateTime::currentDateTime();
    QString dateTime = time.toString("MM-dd-hh-mm");
    QString str= QString("Data%1").arg(dateTime);
    img->save(str,"jpg",100);

    ui->label_4->clear();
}


void MainWindow::on_pushButton_4_clicked()//开始录制视频
{
    //videoname为视频的名字,以当前时间命名
    QDateTime time = QDateTime::currentDateTime();
    QString dateTime = time.toString("MM-dd-hh-mm");
    QString str= QString("Data%1.avi").arg(dateTime);
    string videoname;
    videoname = str.toStdString();

    writer=VideoWriter(videoname, CV_FOURCC('X', 'V', 'I', 'D'), 30, Size(1280,720),1);
    connect(timerr, SIGNAL(timeout()), this, SLOT(update_window2()));

    timerr->start(10);
    ui->label_5->setText("正在录制视频");


}


void MainWindow::on_pushButton_5_clicked()//结束录制视频
{
    timerr->stop(); //停止取帧
    ui->label_5->setText("视频录制结束");
    waitKey(2000);
    ui->label_5->clear();
}

void MainWindow::update_window2()
{
    Mat color;
    cvtColor(color_img,color,CV_BGR2RGB);
    writer.write(color);

     waitKey(0);
}

设计的界面是这样的

比较粗糙,对应的组件如下表所示

彩色图像显示窗口label
深度图像显示窗口label_2
信号窗口label_3
截图窗口(下图中隐藏了)label_4
信号窗口label_5
openpushButton
closepushButton_2
截图pushButton_3
视频录制开始pushButton_4
视频录制结束pushButton_5

在这里插入图片描述

最后的效果图

是这样:
在这里插入图片描述
嘿嘿,第一次写博客,如有问题欢迎批评指正,俺还只是个马上研一的小白

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ubuntu16.04系统下QT+opencv+realsense435i 的相关文章

  • class balanced loss pytorch 实现

    cb loss pytorch 实现 xff0c 可直接调用 参考 xff1a https github com vandit15 Class balanced loss pytorch blob master class balanced
  • 解决不平衡数据集问题

    解决不平衡数据集问题 数据不平衡通常反映数据集中类的不均匀分布 例如 xff0c 在信用卡欺诈检测数据集中 xff0c 大多数信用卡交易都不是欺诈 xff0c 只有很少的类是欺诈交易 这使得我们在欺诈类和非欺诈类之间的比例约为50 1 迄今
  • matlab报错解决---当前文件夹或MATLAB路径中未发现xxxx.m,但它位于xx\xxx\xxx\路径下

    解决 xff1a 选定为找到的文件 xff0c 右键 xff0c 找到 选择文件夹和子文件夹 选项 xff0c 添加到路径即可 xff0c 之后文件会变亮色 xff0c 不是灰色 修改之后 xff0c 发现没有 添加路径 的选项了 最后解决
  • sdf模型插入gazebo_ros_control插件

    gazebo ros control目前只支持老版的urdf模型 xff0c 官方教程 xff1a http gazebosim org tutorials tut 61 ros control sdf模型怎么办呢 xff1f 回答 xff
  • PreparedStatement 在mysql下中文乱码解决方案

    在顶目中无意中碰到PreparedStatement 在存DB时出现乱码 xff0c 困扰了好久终于解决问题 问题代码如下 pstmt 61 con prepareStatement INSERT OFFLINE pstmt setStri
  • 2013年终总结

    2013年即将过去 xff0c 回顾这一年 xff0c 有得有失 xff0c 有喜有悲 xff0c 些许记忆碎片留在脑海中 简单做个总结 xff0c 也算划上一个完美的句号 xff0c 再迎接充满挑战的2014 xff01 项目 一年过来
  • 程序员的生活,其实苦不堪言

    前一天 A 下班前把这个代码发给我 B 好的 xff01 第二天 A 都他妈中午了 xff0c 代码怎么还没发过来 xff1f B 我他妈还没下班呢 xff01 程序猿的真实写照 曾经刚参加工作 xff0c 接手一个项目的维护 xff0c
  • 不容错过的用户标签全面解读。建议收藏!

    过去几年来 xff0c 随着我国整体人口红利优势不再 xff0c 市场竞争加剧 xff0c 获客成本不断飙升 xff0c 互联网也告别增长进入存量时代 xff0c 品牌方的营销目标也从最大化追求用户数量规模转变为追求用户质量的精细化营销上
  • 【书写makefile】相关符号介绍

    本文将介绍一下几种符号 xff1a 61 43 61 61 61 makefile中 xff0c 的意思是取变量的意思 xff0c 比如 xff0c a 61 4 那么在后面的语句中 xff0c a 就代表的是取a的值 如果给a定义的是个宏
  • python人工智能技术

    人工智能 xff08 AI xff09 已成为当今世界的热门话题 xff0c 它的应用范围越来越广泛 其中 xff0c Python成为AI开发中最受欢迎的编程语言之一 Python提供了许多功能强大的库和框架 xff0c 大大简化了开发人
  • 利用X-CTU软件给P900数传配置参数

    转自 xff1a 70条消息 P900数传参数配置 落体偏东 CSDN博客 ATS104设置网络号 xff08 设置主从之间通讯连接的密码 xff09 ATS105设置单元号 xff08 给自己使用的数传进行编号 xff0c 防止主从混乱
  • px4添加自己编写的代码并编译

    1 在px4项目下的src文件夹下的modules文件夹中创建一个文件夹 xff0c 如图我创建了一个position control文件夹 xff0c 在该文件夹中添加自己写的代码程序 xff0c 同时添加一个CmakeLists txt
  • 思岚A1M8激光雷达-ubuntu18.04-slam建图参考

    Rplidar A1 A2使用及Hector SLAM建图 NouriXiiX的博客 CSDN博客 激光雷达初体验 Ubuntu 18 04 43 思岚科技 RPLIDAR A1M8 43 ROS 上手使用 银时大魔王的博客 CSDN博客
  • intel Realsense D/T系列 kalibr标定

    kalibr官方源码GitHub ethz asl kalibr The Kalibr visual inertial calibration toolbox 鼠标下拉找到install follow the install wiki pa
  • gazebo仿真遇到的FCU问题

    当使用roslaunch xxx launch命令进行gazebo仿真时出现 FCU Preflight Fail Accel 0 uncalibrated或者FCU Preflight Fail Baro Sensor 0 missing
  • 大广角USB摄像头选用指南

    起因是我要做一个二维码引导无人机降落的实验 四旋翼无人机搭载单目下视摄像头 xff0c 用于识别地面的二维码 我选择摄像头的标准基本上只有一个 xff1a 视场角越大越好 为此查阅了一些资料 xff0c 买了很多镜头和底板 xff0c 有了
  • 一:XTDrone平台上将视觉SLAM2与gazebo仿真集合

    1 XTDrone仿真平台配置 参考官方教程 xff0c 基本没大问题 仿真平台基础配置 语雀 依赖安装sudo apt install y n https www yuque com xtdrone manual cn basic con
  • 线程和进程的区别

    不少刚看到这两个词 xff08 特别是不是计算机专业的 xff09 小伙伴可能会比较疑惑 xff0c 线程和进程有什么区别 xff0c 网上有许多专业性的解答 xff0c 但是既然不少小伙伴不是计算机专业的 xff0c 那就结合例子做个大概
  • Pixhawk烧写自己开发过的1.11.0固件连接不上QGC

    最近在更改代码烧写固件后 xff0c 飞控就连接不上地面站 xff0c 以为是飞控坏了 xff0c 烧写了最新版的固件发现有可以连接到地面站了 xff0c 又烧写了同一版本的其他代码发现也可以连接qgc xff0c 应该是自己写的代码某个部
  • C标准库string.h源码(简单版)

    C标准库string h源码 xff08 简单版 xff09 strcpy strncpystrcat strncatstrcmp strncmpstrchr strrchr strchrnul strstrmemcpy memmove s

随机推荐