Ubuntu14下OpenCV3和QT5的安装及联合使用

来自https://www.hsli.top

本文参考了众多安装教程综合而成,力求做出一个最有效最快捷的安装方案:

  • Ubuntu 14.04
  • OpenCV 3.0
  • QT 5

安装一些基本的支持包

1
2
3
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 install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

下载OpenCV

可以从OpenCV官网下载opencv安装包[ OpenCV官网]

安装OpenCV

解压到自定义目录,进入然后创建build目录并进入

1
2
mkdir build
cd build

配置opencv

1
cmake -D CMAKE_BUILD_TYPE=Release -D MAKE_INSTALL_PREFIX=/usr/local ..

这里安装在/usr/local目录

进行编译

1
make

漫长的等待之后安装opencv库

1
sudo make install

至此opencv3就安装完了,到/usr/local/include下可以看到opencv的头文件,到/usr/local/lib下可以看到opencv的库文件

安装QT

1
sudo apt-get install qt-sdk

这里一句命令搞定了qt的所有安装,简便快捷

QT与OpenCV的联合使用

由于先要测试一下qt与opencv的联合,因此首先打开qt-creater创建一个console application

创建console application

在此我测试了一遍加载haar分类器进行人脸识别的代码

人脸识别代码

注意头文件一定要include全,用qt写代码有代码提示和高亮,相当舒服

然后修改工程文件,双击打开工程名.pro在最后加上以下代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
INCLUDEPATH += /usr/local/include \
/usr/local/include/opencv \
/usr/local/include/opencv2

LIBS += /usr/local/lib/libopencv_calib3d.so \
/usr/local/lib/libopencv_core.so \
/usr/local/lib/libopencv_features2d.so \
/usr/local/lib/libopencv_flann.so \
/usr/local/lib/libopencv_highgui.so \
/usr/local/lib/libopencv_imgcodecs.so \
/usr/local/lib/libopencv_imgproc.so \
/usr/local/lib/libopencv_ml.so \
/usr/local/lib/libopencv_objdetect.so \
/usr/local/lib/libopencv_photo.so \
/usr/local/lib/libopencv_shape.so \
/usr/local/lib/libopencv_stitching.so \
/usr/local/lib/libopencv_superres.so \
/usr/local/lib/libopencv_videoio.so \
/usr/local/lib/libopencv_video.so \
/usr/local/lib/libopencv_videostab.so

实际上加载的库只需要本程序必须的库就可以,但是为了方便期间,我把所有的库都写在一个txt里,以后每次修改就粘贴进去

列出所有库的方法:

1
ls /usr/local/lib >> list.txt

需要权限
然后用gedit把路径/usr/local/lib等替换进去

编译工程,运行,大搞成功

如果文章有用,请随意打赏