在Qt下没有专门的视频采集与播放工具,这里使用了OpenCV所带的库函数捕获摄像头的视频图像。
硬件环境:
讯为RK3588开发板、OV5695(MIPI接口)摄像头
软件版本:
OS:ubuntu20.04镜像固件
QT:5.12.8(Qt Creator:4.11.0)
OpenCV:3.4.14
RK3588开发板烧录系统镜像
RK3588开发板出厂自带的系统是安卓,需要下载开发板官方的Ubuntu系统镜像重新烧录,系统镜像烧录过程参考开发板手册,不在赘述。
安装QT
在RK3588开发板上我们直接使用apt在线安装方式安装Qt:
- 修改系统的镜像源
编辑/etc/apt/sources.list文件,使用清华源。
root@iTOP-RK3588:/home/topeet/Downloads $ cat /etc/apt/sources.list
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-security main restricted universe multiverse
root@iTOP-RK3588:/home/topeet/Downloads $
root@iTOP-RK3588:/home/topeet/Downloads $sudo apt-get update
root@iTOP-RK3588:/home/topeet/Downloads $sudo apt-get upgrade
- 安装Qt
root@iTOP-RK3588:/home/topeet/Downloads $sudo apt-get install build-essential
root@iTOP-RK3588:/home/topeet/Downloads $sudo apt-get install qtbase5-dev qtchooser qt5-qmake qtbase5-dev-tools
root@iTOP-RK3588:/home/topeet/Downloads $sudo apt-get install qtcreator
root@iTOP-RK3588:/home/topeet/Downloads $sudo apt-get install qt5*
安装完成后搜索qt会显示出Qt的相关程序。
安装OpenCV
安装opencv参考了官方文档:
https://docs.opencv.org/3.4.14/d2/de6/tutorial_py_setup_in_ubuntu.html
- 安装依赖
root@iTOP-RK3588:/home/topeet/Downloads $sudo apt-get install cmake
root@iTOP-RK3588:/home/topeet/Downloads $sudo apt-get install gcc g++
root@iTOP-RK3588:/home/topeet/Downloads $sudo apt-get install python3-dev python3-numpy
root@iTOP-RK3588:/home/topeet/Downloads $sudo apt-get install build-essential
root@iTOP-RK3588:/home/topeet/Downloads $sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev libv4l-dev libxvidcore-dev libx264-dev libatlas-base-dev gfortran libgtk2.0-dev libjpeg-dev libpng-dev
注:ubuntu20.04在线安装python3-dev python3-numpy可能会报错;参考https://blog.csdn.net/louObaichu/article/details/122188243解决。
- 下载OpenCV
到https://codeload.github.com/opencv/opencv/zip/refs/tags/3.4.14下载OpenCV。
解压opencv-3.4.14.zip压缩包会生成opencv-3.4.14文件夹。 - 编译安装OpenCV
进入opencv-3.4.14文件夹,在该目录下新建一个build文件夹;
cd build
cmake ../
make #执行make开始编译,这个时间比较长,耐心等待。
sudo make install
- 配置动态链接库
编辑/etc/ld.so.conf,文末加入“/usr/local/lib”,执行sudo /sbin/ldconfig –v生效。
- 验证OpenCV
OpenCV默认都安装到了/usr/local/目录下,通过ll检查文件是否存在;
python3环境下验证OpenCV:
QT展示
- 进入qt创建一个新Qt项目,创建完后打开.pro文件,加入opencv库的路径。
INCLUDEPATH +=/usr/local/include/ \
/usr/local/include/opencv/ \
/usr/local/include/opencv2
LIBS +=/usr/local/lib/lib*
- 编辑main.cpp文件,输入以下代码获取摄像头的画面,其中11代表的是摄像头的节点号,可以通过v4l2-ctl --list-devices来获取摄像头的节点。
#include "mainwindow.h"
#include <QApplication>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <QDebug>
using namespace std;
using namespace cv;
int main()
{
VideoCapture captrue;
Mat img;
captrue.open(11);
if(captrue.isOpened()){
qDebug() <<"camera is opened";
}
while (1) {
captrue.read(img);
imshow("Video test",img);
waitKey(1);
}
waitKey(0);
return 0;
}
- 效果展示
编辑完main.cpp文件后,单击运行即可查看显示效果: