Ubuntu16.04+Qt5+Opencv+Cmake一步成功配置(详细)

一、Ubuntu换源(该片使用Ubuntu16.04版本)

解释:由于Ubuntu系统自带软件源为美国地区,所以系统下载文件时非常慢,通过换成国内源可提快下载速度。

1.备份原来的源

将之前源备份在 /etc/apt/sources_init.list 文件内,防止之后使用。

sudo cp /etc/apt/sources.list /etc/apt/sources_init.list

2.更换源

使用gedit打开文档,将下边的源代码复制进去,然后点击保存关闭。

sudo gedit /etc/apt/sources.list

注意:以下源选择一个即可!

阿里源
deb http://mirrors.aliyun.com/ubuntu/ xenial main
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main
deb http://mirrors.aliyun.com/ubuntu/ xenial universe
deb-src http://mirrors.aliyun.com/ubuntu/ xenial universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main
deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security universe
清华源
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
#deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiversedeb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiversedeb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiversedeb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
网易源
deb http://mirrors.163.com/ubuntu/ wily main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ wily-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ wily-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ wily-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ wily-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ wily main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ wily-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ wily-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ wily-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ wily-backports main restricted universe multiverse

3.更新

更新源
sudo apt-get update
复损坏的软件包,尝试卸载出错的包
sudo apt-get -f install
更新软件
 sudo apt-get upgrade

4.懒人换源法

其他Ubuntu版本可以用懒人换源法: Ubuntu所以脚本快速换成国内源

二、下载Qt

1.通过Qt官网下载

Qt官网下载地址
Ubuntu16.04+Qt5+Opencv+Cmake一步成功配置(详细)_第1张图片
选择自己需要的版本


下载.run文件

2.安装Qt

cd到qt-opensource-linux-x64-5.9.9.run所在的目录下,在命令端口中输入安装命令:(注意看自己下载的版本号)

sudo ./qt-opensource-linux-x64-5.9.9.run

之后会出现一个安装界面,按照提示安装就行,如果要登录账号,自己注册一个即可。

3.路径配置

在命令端口中输入命令:

sudo vim /usr/lib/x86_64-linux-gnu/qt-default/qtchooser/default.conf  

打开default.conf文件。

第一行改为自己安装路径(这是我的安装路径/home/Qt5.9.9)下的bin目录的路径
第二行改为Qt5.9.9目录的路径

如下:

/home/Qt5.9.9/5.9/gcc_64/bin
/home/Qt5.9.9/

保存退出

4.安装 libgl1-mesa-dev(libGL库)

在命令端口中输入命令:

sudo apt-get install libgl1-mesa-dev

至此Qt就安装完成了,可以打开软件编程了。

三、下载OpenCV

1、官网下载

opencv下载: 下载地址:http://opencv.org/releases.html
opencv-contrib下载: https://github.com/opencv/opencv_contrib

2.解压下载的.zip包(xxx对于自己的opencv版本号)

对应自己的压缩包文件

unzip opencv-xxx.zip
unzip opencv-contrib-xxx.zip

注意:opencv和opencv-contrib版本要一样!

创建build文件

mkdir build
cd build

注意:记住路径!

四、配置环境

命令行内:(一条一条写入)
(1)安装编译环境

sudo apt-get update
sudo apt-get install build-essential pkg-config cmake cmake-gui

(2) 安装 pip

sudo apt-get install python-pip
sudo apt-get install python3-pip

(3) 安装 numpy

pip install numpy scipy matplotlib scikit-image scikit-learn
pip3 install numpy scipy matplotlib scikit-image scikit-learn

(4) 安装依赖库

sudo apt-get install checkinstall yasm gfortran git ffmpeg libxvidcore4 libgtk-3-dev
sudo apt-get install libjpeg8-dev libtiff5-dev libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install libdc1394-22-dev libxine2-dev libv4l-dev libqt4-dev libgtk2.0-dev libtbb-devsudo apt-get install libatlas-base-dev libfaac-dev libmp3lame-dev libtheora-dev libvorbis-dev
sudo apt-get install libxvidcore-dev libopencore-amrnb-dev libopencore-amrwb-dev x264 v4l-utils

(5)可选库

sudo apt-get install libprotobuf-dev protobuf-compiler libgoogle-glog-dev
sudo apt-get install libgflags-dev libgphoto2-dev libeigen3-dev libhdf5-dev doxygen

(6) 打开 cmake-gui 配置

cmake-gui

(7)Cmake界面

第一行是opencv路径
第二行是build路径(新的编译路径)

之后按照图操作
Ubuntu16.04+Qt5+Opencv+Cmake一步成功配置(详细)_第2张图片
Ubuntu16.04+Qt5+Opencv+Cmake一步成功配置(详细)_第3张图片
修改项

CMAKE_BUILD_TYPE: RELEASE
WITH_OPENGL: 选上
WITH_CUDA: 不选

包含opencv-contrib库

OPENCV_EXTRA_MODULES_PATH 填写 opencv_contrib 解压目录下,modules 文件夹的路径:

OPENCV_EXTRA_MODULES_PATH: opencv_contrib-xxx/modules

重新 ConfigureGenerate

最后命令行输入开始编译: make -j8
(-j8意思是使用8线程编译)

(7)安装 opencv(注意单条输入 命令行

make install
sudo sh -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig

完成!!!

五、测试

test.pro文件中

QT       += core

QT       -= gui

TARGET = test
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app

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

LIBS += /usr/local/lib/libopencv_highgui.so \
            /usr/local/lib/libopencv_core.so    \
            /usr/local/lib/libopencv_imgproc.so   \
           /usr/local/lib/libopencv_imgcodecs.so

SOURCES += main.cpp

main .cpp文件中

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

using namespace cv;

int main(int argc, char *argv[])
{
    namedWindow( "src", WINDOW_AUTOSIZE );
    Mat src = imread( "/home/test.jpg" );//自己图片路径

    imshow( "src", src );

    return a.exec();
}

如果能显示图片,那么恭喜你完成了!!!

你可能感兴趣的:(笔记)