树莓派4B安装QT5与配置OpenCV4

参考https://blog.csdn.net/qq_41923091/article/details/100980627

https://blog.csdn.net/weixin_43331094/article/details/103215415

 

https://www.cnblogs.com/jsxyhelu/p/7839062.html

 

我的环境是官方最新系统即9月份更新的
终端输入:(自动安装最新版本qt)

sudo apt-get update
sudo apt-get install qt5-default qtcreator

配置系统路径

sudo nano /etc/bash.bashrc

在文件的最后添加如下内容

export QTDIR=/opt/Qt5.12.0/5.12.0/gcc_64
export PATH=$QTDIR/bin:$PATH
export LD_LIBRARY_PATH=$QTDIR/lib:$LD_LIBRARY_PATH5_:

然后ctrl+o保存然后ctrl+x退出

source /etc/bash.bashrc

测试配置

echo $PATH

输出如下内容
/opt/Qt5.12.0/5.12.0/gcc_64/bin:/opt/ros/melodic/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin5446722

然后QT就算安装完成
接下来是OpenCV
下载Opencv4.1.0
配置前先下载cmake

sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get update
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

QT+Opencv配置:(在opencv文件夹中打开终端)https://docs.opencv.org/4.1.0/d7/d9f/tutorial_linux_install.html

 

mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local ..

//下面内容一起复制  再回车
(以下屏蔽了python2和python3的编译,如果用py编程的可以不用继续看了。用c++的可以继续操作
 至于为什么屏蔽,是因为博主编译的时候,编译到98%左右会报pyhton的错,网上一直找不到解决方法,
 但是博主又不用python,因此就屏蔽此部分的编译。)

cmake -D CMAKE_BUILD_TYPE=RELEASE -D BUILD_opencv_python3=OFF -D BUILD_opencv_python2=OFF -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON_EXAMPLES=OFF -D BUILD_EXAMPLES=ON ..

make -j4
sudo make install
sudo make
sudo make install
sudo nano /etc/ld.so.conf.d/opencv.conf 

 

加入

/usr/local/lib

然后ctrl+o保存然后ctrl+x退出

sudo ldconfig

接着配置环境变量:

sudo nano /etc/bash.bashrc 

写入

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig 
export PKG_CONFIG_PATH 

 

然后ctrl+o保存然后ctrl+x退出

sudo ldconfig

终端输入:

sudo cp /usr/local/include/opencv4/opencv2 /usr/local/include -r

到这里为止就完成了,然后测试下:
.pro文件改成下面的内容:

sudo cp /usr/local/include/opencv4/opencv2 /usr/local/include -r
TEMPLATE = app

CONFIG += console c++11

CONFIG -= app_bundle

CONFIG -= qt

SOURCES += main.cpp

INCLUDEPATH += /usr/local/include \
                /usr/local/include/opencv
                /usr/local/include/opencv2

LIBS += /usr/local/lib/libopencv_calib3d.so.4.1 \
        /usr/local/lib/libopencv_core.so.4.1 \
        /usr/local/lib/libopencv_features2d.so.4.1 \
        /usr/local/lib/libopencv_highgui.so.4.1 \
        /usr/local/lib/libopencv_imgcodecs.so.4.1 \
        /usr/local/lib/libopencv_imgproc.so.4.1 \
        /usr/local/lib/libopencv_ml.so.4.1 \
        /usr/local/lib/libopencv_objdetect.so.4.1 \
        /usr/local/lib/libopencv_photo.so.4.1 \
        /usr/local/lib/libopencv_stitching.so.4.1 \
        /usr/local/lib/libopencv_video.so.4.1 \

/usr/local/lib/libopencv_imgcodecs.so.4.1

测试能否调用摄像头:(前提是你有摄像头且配置好了,这个网上很多教程,如果是USB接口的摄像头可能要把VideoCapture capture (0);改成VideoCapture capture (1);或者改成2试试,没有摄像头可以用一张图片试下,就是打开一张图片
下面是.cpp文件内容

 

#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>

using namespace std;
using namespace cv;

int main()
{
    VideoCapture capture (0);
    Mat src;
    while(1)
    {

        capture >> src;
        imshow("11",src);
        char(key)=(char)waitKey(1);
        if(key==27)
            break;
    }
    return 0;
}

 

上一篇:133: error: in C++98 XXX must be initialized by constructor, not by '{...}'


下一篇:Windows下Qt5程序打包发布