V4L2+QT视频优化策略

问题描述

之前写了这样一篇文章: V4L2+QT+USB摄像头实时显示视频(Arm,Linux,window均适用)
程序运行在Linux系统中,视频不掉帧,显示很稳定;但是编译之后运行在Arm9开发板时,能感到明显的延迟

解决问题过程中发现这样一个论坛: Qt用QLabel来显示摄像头,CPU占用率过高的问题,定时器变不准

总结一下内容:

  1. QLabel控件不适合做频繁刷新的图像显示,没有效率;
  2. 可以开多线程来解决显示问题
  3. 可用QPixmap+QPainter替代QLabel控件

不妨尝试用多线程+QPainter来显示视频帧

编程

QPainter显示图像

首先是QPainter显示图像,先学习一下 QPainter Class,核心代码

    /* 显示的每帧图像 */
    QImage img;

    /* 重写父类下的protected方法*/
protected:
    void paintEvent(QPaintEvent *);
    
......

void videoshow::paintEvent(QPaintEvent *){
    try{
        QPainter painter(this);

        if(!img.isNull()){
            painter.drawImage(QPointF(0,0),img);
        }

    }catch(...){}
}

这里选择不缩放显示图像,参考以下函数

在这里插入图片描述

QThread线程开启

官方文档: QThread Class

QT使用线程主要有两种方法,其中之一就是继承QThread,然后重写run()的方法

class V4l2Thread : public QThread{
public:
    explicit V4l2Thread(QWidget *parent = 0);
    ~V4l2Thread();
    
	/* QThread 虚函数 run */
	void run();
}

void V4l2Thread::run(){
	/* V4l2的编程就可以写在`run()`函数中 */
}

线程的启动start(),线程的销毁destroyed()

/* 实例化V4l2Thread */
t = new V4l2Thread();

......

t.start();

......

t->destroyed();
  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
V4L2 (Video for Linux 2) is a kernel API that provides an interface for capturing and manipulating video devices on Linux. OpenCV is a widely used computer vision library that provides various functionalities for image and video processing. Qt is a cross-platform application framework that allows you to develop graphical user interfaces. If you want to work with V4L2, OpenCV, and Qt together, you can use OpenCV to capture video frames from a V4L2 device and then use Qt to display the captured frames in a graphical user interface. Here are the steps you can follow: 1. Install the necessary libraries: - Install V4L2 library: `sudo apt-get install libv4l-dev` - Install OpenCV library: You can either download it from the official website or install it using package manager (e.g., `pip install opencv-python`) 2. Include the required headers in your C++ code: ```cpp #include <linux/videodev2.h> // V4L2 headers #include <opencv2/opencv.hpp> // OpenCV headers #include <QtWidgets/QApplication> // Qt headers ``` 3. Use V4L2 to capture video frames: ```cpp int fd; fd = open("/dev/video0", O_RDWR); // Open the V4L2 device // Set up video capture parameters struct v4l2_format fmt; // ... // Request buffers from the V4L2 device struct v4l2_requestbuffers reqbuf; // ... // Queue the buffers for capturing struct v4l2_buffer buf; // ... // Start capturing frames enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE; ioctl(fd, VIDIOC_STREAMON, &type); // Capture frames for (int i = 0; i < numFrames; ++i) { // Dequeue a buffer // ... // Process the captured frame using OpenCV cv::Mat frame; // ... // Display the frame using Qt QImage image(frame.data, frame.cols, frame.rows, QImage::Format_RGB888); // ... } // Cleanup and close the V4L2 device // ... ``` 4. Use Qt to display the frames: ```cpp QApplication app(argc, argv); QWidget window; QLabel label(&window); label.setFixedSize(frame.cols, frame.rows); label.setPixmap(QPixmap::fromImage(image)); label.show(); return app.exec(); ``` Remember to handle error checking, memory management, and other necessary operations according to your application's needs.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值