系统配置:
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
#include
#include
#include
#include
#include
#include
#include
#include
#endif
//类的声明:
class MainWindow : public QMainWindow{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void updateImage();
#if 1
void on_pushButton_clicked();
void on_pushButton_2_clicked();
#endif
private:
#if 1
QTimer theTimer;
Mat srcImage;
VideoCapture videoCap;
QLabel *imageLabel;
#endif
Ui::MainWindow *ui;
protected:
void paintEvent(QPaintEvent *e);
};
mainwindow.cpp中的代码:
MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent), ui(new Ui::MainWindow){
ui->setupUi(this);
//capture video from camera
#if 1
connect(&theTimer, &QTimer::timeout, this, &MainWindow::updateImage);
//if(videoCap.open(0)){//local mipi-csi camera
if(videoCap.open("rtsp://192.168.99.1:554/11/")){//ip camera
srcImage = Mat::zeros(videoCap.get(CV_CAP_PROP_FRAME_HEIGHT),
videoCap.get(CV_CAP_PROP_FRAME_WIDTH), CV_8UC3);
theTimer.start(33);
}
//set the Label to display video
imageLabel = new QLabel(this);
ui->verticalLayout->addWidget(imageLabel);
#endif
#if 1
wiringPiSetup();
pinMode(25, OUTPUT);
#endif}
#if 1
void MainWindow::paintEvent(QPaintEvent *e)
{//here are two ways to display video
#if 1//way 1
QImage image1 = QImage((uchar*)(srcImage.data), srcImage.cols, srcImage.rows, QImage::Format_RGB888);
imageLabel->setPixmap(QPixmap::fromImage(image1));
imageLabel->resize(image1.size());
imageLabel->show();
#else// way 2
QPainter painter(this);
QImage image1 = QImage((uchar*)(srcImage.data), srcImage.cols, srcImage.rows, QImage::Format_RGB888);
painter.drawImage(QPoint(20,20), image1);
#endif
//e=NULL;
}
#endif
#if 1
void MainWindow::updateImage(){
videoCap>>srcImage;
if(srcImage.data){
cvtColor(srcImage, srcImage, CV_BGR2RGB);//qt:rgb opencv:bgr
this->update();//send flash messages
}
}#endif
MainWindow::~MainWindow(){
delete ui;
}
#if 1
void MainWindow::on_pushButton_clicked(){
digitalWrite(25, 0);
}
void MainWindow::on_pushButton_2_clicked(){
digitalWrite(25, 1);
}
#endif
以下是运行时资源占用情况:
以下是自己的调试过程记录:
如果编译的时候出现错误:
error: //usr/lib/arm-linux-gnueabihf/libopencv_videoio.so.3.2: error adding symbols: DSO missing from command line
就是缺少连接文件libopencv_videoio.so.3.2,只需要在pro文件中添加LIBS+= -lopencv_videoio既可。
点击qt的那个ide中点击的debug按钮(也就是左下角有个小虫子的绿色按钮)后application output出现如下错误:
15:24:22: Starting /home/pi/sharedir/qt-pro/build-qt_pro0-Desktop-Debug/qt_pro0...
libEGL warning: DRI2: failed to authenticate
qt5ct: using qt5ct plugin
(qt_pro0:7369): GLib-GObject-WARNING **: 15:24:26.507: cannot register existing type 'GtkWidget'
(qt_pro0:7369): GLib-GObject-CRITICAL **: 15:24:26.507: g_type_add_interface_static: assertion 'G_TYPE_IS_INSTANTIATABLE (instance_type)' failed
(qt_pro0:7369): GLib-GObject-WARNING **: 15:24:26.507: cannot register existing type 'GtkBuildable'
(qt_pro0:7369): GLib-GObject-CRITICAL **: 15:24:26.507: g_type_interface_add_prerequisite: assertion 'G_TYPE_IS_INTERFACE (interface_type)' failed
(qt_pro0:7369): GLib-CRITICAL **: 15:24:26.507: g_once_init_leave: assertion 'result != 0' failed
(qt_pro0:7369): GLib-GObject-CRITICAL **: 15:24:26.507: g_type_add_interface_static: assertion 'G_TYPE_IS_INSTANTIATABLE (instance_type)' failed
(qt_pro0:7369): GLib-GObject-CRITICAL **: 15:24:26.507: g_type_register_static: assertion 'parent_type > 0' failed
按照如下方式:https://blog.csdn.net/u012939880/article/details/83245731
安装了libcurl4-openssl-dev 这个程序后仍然出现上述的问题,在运行的时候。
经过各种尝试,修改到最开始添加opencv之前的形式正常。在qt的工程目录下的.pro文件的将包含opencv相关的库注释掉,既可正常。其中只要包含了opencv_highgui和opencv_videoio任意一个库,就会报出上面的问题。
LIBS += -lwiringPi
#LIBS += -lopencv_highgui
LIBS += -lopencv_core
LIBS += -lopencv_video
#LIBS += -lopencv_videoio
LIBS += -lopencv_imgproc
正常后的application output显示如下:
10:49:28: Debugging starts
libEGL warning: DRI2: failed to authenticate
qt5ct: using qt5ct plugin
qt5ct: D-Bus global menu: no
10:49:47: Debugging has finished
张另外一张卡上安装了,安装上了qt和opencv后,也会出现同样的现象,应该是树莓派的环境问题。
树莓派4B同样的问题。
说是解决方案,倒不如说是规避。出现这种错误的时候,说明debug用不了。但是同样在与工程目录同级的一个目录build-qt_pro0-Desktop-Debug生成了可执行的文件qt_pro0。直接在终端执行:sudo ./qt_pro0。能够正常执行,就不会报出上面的错误(其中qt_pro0是我的qt的工程名)
个人理解:上面的错误信息是在qt的那个ide中点击的debug按钮(也就是左下角有个小虫子的绿色按钮)出现的。因为opencv的两个库opencv_videoio 和opencv_highgui都是和视频界面相关的,这样的话如果使用qt IDE的调试功能可能就会出现问题。因为调试的功能都存在单步运行的功能。
如果我们先编译好(点击左下角榔头的图标),再执行固件。就不会有单步运行的需求。