系統配置:
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 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的調試功能可能就會出現問題。因爲調試的功能都存在單步運行的功能。
如果我們先編譯好(點擊左下角榔頭的圖標),再執行固件。就不會有單步運行的需求。