树莓派上实现qt+opencv获取mipi-csi摄像头视频以及qt使用wiringPi库控制LED的亮灭

https://blog.csdn.net/xiaolintyd/article/details/52974809首先在系统里面安装opencvsudo apt-get install libopencv-dev安装qtsudo apt-get updatesudo apt-get install qt5-defaultsudo apt-get install qtcrea...
摘要由CSDN通过智能技术生成

系统配置:

https://blog.csdn.net/xiaolintyd/article/details/52974809

首先在系统里面安装opencv

sudo apt-get install libopencv-dev

安装qt

sudo apt-get update

sudo apt-get install qt5-default

sudo apt-get install qtcreator

 

源代码:

代码参考:https://www.cnblogs.com/lifan3a/articles/7305652.html

先在在ui界面上放置一个“Vertical Layout”控件,调整到合适大小

 

pro文件需要添加引入opencv和gpio相关的动态库:

LIBS += -lwiringPi
LIBS += -lopencv_highgui
LIBS += -lopencv_core
LIBS += -lopencv_video
LIBS += -lopencv_videoio
LIBS += -lopencv_imgproc

mainwindow.h中加入相应的头文件和类的成员声明:

#if 1
#include <QPaintEvent>
#include <QTimer>
#include <QPainter>
#include <QPixmap>
#include <QLabel>
#include <QImage>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#endif

//类的声明:
class M
  • 0
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
如果你想在Qt中读取地方摄像头视频,可以使用OpenCV的`VideoCapture`类进行操作。 具体步骤如下: 1. 在Qt中添加OpenCV,可以在.pro文件中添加以下内容: ``` INCLUDEPATH += /usr/local/include/opencv4 LIBS += -L/usr/local/lib -lopencv_core -lopencv_highgui -lopencv_videoio ``` 其中,`/usr/local/include/opencv4`为OpenCV的头文件路径,`/usr/local/lib`为OpenCV文件路径。 2. 在Qt中创建一个窗口用于显示视频。 3. 使用`VideoCapture`类打开地方摄像头,可以通过参数指定摄像头的设备编号或者视频文件的路径。 4. 循环读取视频帧并在窗口中显示,直到读取失败或者用户退出程序。 完整代码示例如下: ``` #include <opencv2/opencv.hpp> #include <QApplication> #include <QMainWindow> #include <QImage> #include <QPixmap> #include <QLabel> int main(int argc, char *argv[]) { QApplication app(argc, argv); QMainWindow window; QLabel label; window.setCentralWidget(&label); window.show(); cv::VideoCapture cap(0); // 打开地方摄像头 if (!cap.isOpened()) // 打开失败 { std::cerr << "Failed to open camera." << std::endl; return -1; } while (true) // 循环读取视频帧 { cv::Mat frame; cap >> frame; // 读取视频帧 if (frame.empty()) break; // 读取失败,退出循环 cv::cvtColor(frame, frame, cv::COLOR_BGR2RGB); // OpenCV图像格式为BGR,需要转换为RGB QImage qimg(frame.data, frame.cols, frame.rows, QImage::Format_RGB888); // 创建QImage对象 QPixmap qpixmap = QPixmap::fromImage(qimg); // 转换为QPixmap对象 label.setPixmap(qpixmap); // 显示在窗口中 QApplication::processEvents(); // 处理Qt事件,保证窗口正常显示 if (cv::waitKey(30) == 'q') break; // 按下q键退出循环 } return app.exec(); } ``` 其中,`QMainWindow`和`QLabel`用于创建窗口和显示视频帧,`QImage`和`QPixmap`用于将OpenCV的图像格式转换为Qt的图像格式,`QApplication::processEvents()`用于保证窗口正常显示。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值