树莓派4B安装QT与配置OpenCV(最新QT与OpenCV4)

我的环境是官方最新系统即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 ..
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文件改成下面的内容:

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 
#include 
#include 
#include 

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;
}

你可能感兴趣的:(树莓派4B安装QT与配置OpenCV(最新QT与OpenCV4))