Ubuntu18.04下Qt5.12.9+OpenCV4.4.0环境搭建

安装Ubuntu18.04

一、安装Qt5.12.9

step1:下载安装包

Qt官网下载release文件;注意:太新的版本没有*.run文件。

本次下载的版本:qt-opensource-linux-x64-5.12.9.run

step2:安装

在下载好的qt-opensource-linux-x64-5.12.9.run目录下执行:

sudo chmod +x qt-opensource-linux-x64-5.12.9.run
sudo ./qt-opensource-linux-x64-5.12.9.run

选择安装组件时要【全选】。

step3:配置环境变量

sudo gedit /etc/bash.bashrc
#打开后,根据自已的具体版本在最后添加如下
export QTDIR=/opt/Qt5.12.9/5.12.9/gcc_64 
export PATH=$QTDIR/bin:$PATH
export LD_LIBRARY_PATH=$QTDIR/lib:$LD_LIBRARY_PATH
#保存一下
source /etc/bash.bashrc

step4:测试hello world

打开Qt,新建non-qt-project -> plain C++ App,能输出就成功了。

Ubuntu18.04下Qt5.12.9+OpenCV4.4.0环境搭建_第1张图片

二、安装OpenCV

step1:安装cmake和依赖库

sudo apt-get install cmake 

sudo apt-get install build-essential libgtk2.0-dev libgtk-3-dev libavcodec-dev libavformat-dev libjpeg-dev libswscale-dev libtiff5-dev

step2:安装python3

Ubuntu自带python编译器了

python3
#显示如下信息:
Python 3.6.9 (default, Nov  7 2019, 10:44:02) 

step3:支持python

#python3支持
sudo apt install python3-dev python3-numpy
#streamer支持
sudo apt install libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev
#可选的依赖
sudo apt install libpng-dev libopenexr-dev libtiff-dev libwebp-dev

step4:点击Source下载release源文件

本次下载的版本:opencv-4.4.0.zip

step5:安装

将zip文件解压到当前目录

unzip opencv-4.4.0.zip -d .

进入解压后的文件夹

cd opencv-4.4.0/

创建一个编译文件夹build,并进入。

mkdir build
cd build/

step6:cmake编译

cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=YES -D CMAKE_INSTALL_PREFIX=/usr/local/opencv4 ..

这里我修改了安装路径为/usr/local/opencv4,可以自己修改到自己需要的位置。

如果该命令中不加-D CMAKE_INSTALL_PREFIX=/usr/local/opencv4,则默认各部分分别安装在/usr/local/目录的include/ bin/ lib/3个文件夹下.

此时cmake命令的结果里可以查看是否支持Python:
Ubuntu18.04下Qt5.12.9+OpenCV4.4.0环境搭建_第2张图片
step7:make编译:

# runs 6 jobs in parallel
make -j6 	
sudo make install

此时OpenCV安装完成!

step8:为了后续和qt链接的配置:

#添加库路径
cd /etc/ld.so.conf.d/
sudo touch opencv.conf
sudo chmod 777 opencv.conf
sudo echo "/usr/local/opencv4/lib" > opencv.conf
sudo ldconfig

如果sudo echo "/usr/local/opencv4/lib" > opencv.conf出现权限不够,执行:

sudo chmod 777 opencv.conf

step9:配置OpenCV 的 pgk-config环境

  1. 找到opencv4.pc文件所在目录:
sudo find / -iname opencv4.pc
显示:/usr/local/opencv4/lib/pkgconfig/opencv4.pc
  1. 安装vim
sudo apt install vim
  1. /usr/local/opencv4/lib/pkgconfig/路径加入PKG_CONFIG_PATH
#打开pkgconfig.sh
sudo vim /etc/profile.d/pkgconfig.sh

#在文件中加入下面一行
export PKG_CONFIG_PATH=/usr/local/opencv4/lib/pkgconfig:$PKG_CONFIG_PATH
  1. 保存退出(vim的保存退出命令为Esc+ZZ,开启大写锁定),使用命令激活:
source /etc/profile
  1. 验证是否配置成功:
pkg-config --libs opencv4

成功会输出:
在这里插入图片描述没成功则会错误提示:

Package opencv4 was not found in the pkg-config search path.

step10:配置动态库环境

  1. 配置OpenCV动态库环境——程序执行时加载动态库*.so的路径。
#打开opencv4.conf
sudo vim /etc/ld.so.conf.d/opencv4.conf

#在该文件(可能是空文件)末尾加上:
/usr/local/opencv4/lib
  1. 保存退出(vim的保存退出命令为Esc+ZZ,开启大写锁定),使用命令激活:
sudo ldconfig

step11:python-opencv环境(可以先跳过)

找到编译好的python cv库:

sudo find / -iname cv2*.so
显示:/usr/local/opencv4/lib/python3.6/dist-packages/cv2/python-3.6/cv2.cpython-36m-x86_64-linux-gnu.so

cv2.cpython-36m-x86_64-linux-gnu.so就是编译好的python3的opencv库;

把它复制到对应python解释器的目录下:

  1. 系统自带的python解释器/path/to/dist-packages/
sudo ln -s /usr/local/opencv4/lib/python3.6/dist-packages/cv2/python-3.6/cv2.cpython-36m-x86_64-linux-gnu.so /usr/lib/python3/dist-packages/cv2.cpython-36m-x86_64-linux-gnu.so
  1. 用户安装的python解释器/path/to/site-packages

我没有安装anaconda,所以没有执行下面这一步:

ln -s /usr/local/opencv4/lib/python3.6/dist-packages/cv2/python-3.6/cv2.cpython-36m-x86_64-linux-gnu.so ~/anaconda3/lib/python3.7/site-packages/cv2.cpython-36m-x86_64-linux-gnu.so

之后就能在该python解释器中使用python-opencv库。

三、在Qt测试OpenCV

打开之前测试Qt时的helloWorld project,或者新建一个。

*.pro文件修改为:

TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += main.cpp

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

LIBS += /usr/local/opencv4/lib/libopencv_calib3d.so \
    /usr/local/opencv4/lib/libopencv_core.so \
    /usr/local/opencv4/lib/libopencv_dnn.so \
    /usr/local/opencv4/lib/libopencv_features2d.so \
    /usr/local/opencv4/lib/libopencv_flann.so \
    /usr/local/opencv4/lib/libopencv_gapi.so \
    /usr/local/opencv4/lib/libopencv_highgui.so \
    /usr/local/opencv4/lib/libopencv_imgcodecs.so \
    /usr/local/opencv4/lib/libopencv_imgproc.so \
    /usr/local/opencv4/lib/libopencv_ml.so \
    /usr/local/opencv4/lib/libopencv_objdetect.so \
    /usr/local/opencv4/lib/libopencv_photo.so \
    /usr/local/opencv4/lib/libopencv_stitching.so \
    /usr/local/opencv4/lib/libopencv_video.so \
    /usr/local/opencv4/lib/libopencv_videoio.so

*.cpp文件修改为:

#include 
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/imgproc.hpp"

using namespace std;
using namespace cv;

int main()
{
    Mat img;
    img = imread("lena.jpg",-1);
    imshow("lena", img);
    waitKey(6000);
    return 0;
}

注意:*.jpg文件要放在build-helloWorld-Desktop_Qt_5_12_9_GCC_64bit-Debug文件夹内,否则会报错。

正常显示图片,完成!

参考资料

https://blog.csdn.net/new_delete_/article/details/84797041

https://blog.csdn.net/Wangguang_/article/details/85762705

你可能感兴趣的:(机器学习环境搭建)