Ubuntu14.04 内 安装 OpenCV 2.4.9 + QT5

时间:2022-04-25 08:45:47


英文原文地址:http://marcelojo.org/marcelojoeng/2014/09/installing-opencv-2-4-9-qt5-in-ubuntu.html

如果不熟悉OpenCV和QT的朋友,请参考下列链接:

http://opencv.org/

http://qt-project.org/


安装 QT:


1
2
3
4
5
mkdir  qt5
cd  qt5
wget  http : //download.qt-project.org/official_releases/online_installers/qt-opensource-linux-x64-1.6.0-4-online.run
chmod   + x   . / qt - opensource - linux - x64 - 1.6.0 - 4 - online . run
sudo   . / qt - opensource - linux - x64 - 1.6.0 - 4 - online . run

注:ubuntu14.04自带Qt5 及 QtCreator的安装:sudo apt-get install qtcreator


编译OpenCV

注:ubuntu14.04自带OpenCV的安装,可通过命令查询:apt-cache search opencv

首先,需要安装一些需要的包:

1
2
3
sudo  apt - get  install  build - essential
sudo  apt - get  install   libgtk2 . 0 - dev
sudo  apt - get  install  libavcodec - dev  libavformat - dev  libswscale - dev  libv4l - dev

其次,下载OpenCV,保存到你选择的目录,如 opencv。 

1
2
3
4
5
6
7
8
cd  opencv
unzip   . / opencv - 2.4.9.zip
cd  opencv - 2.4.9
mkdir  build
cd  build
cmake   - D   WITH_V4L = ON   - D   INSTALL_C_EXAMPLES = ON   - D   INSTALL_PYTHON_EXAMPLES = ON   - D   BUILD_EXAMPLES = ON   - D   WITH_GTK_2_X = ON   . .
//懒人办法,直接用cmake-gui 效果一样。不会用的请留言。
make
sudo  make  install

默认情况下,库文件会安装在 /usr/local/lib。如果想看cmake的选项,参看文件 CMakeLists.txt。

为了能够链接到 lib,需要在目录/etc/ld.so.conf.d/ 下创建一个文件 opencv.conf:

<pre name="code" class="cpp">touch opencv.conf
gvim opencv.conf //使用任何你熟悉的文本编辑器

 
 

在文件内写入并保存:
1
/ usr / local / lib

然后在此目录下执行:

1
sudo  ldconfig

 

测试 OpenCV

如果想测试的话,请进入 ./build/bin 并执行测试程序如:

1
2
cd   . / bin
cd   . / open_test_photo

 

QT5 + OpenCV 

首先打开 QTCreator,

1
/ opt / Qt / Tools / QtCreator / bin / qtcreator

 

创建 Qt控制台应用项目:

 Ubuntu14.04 内 安装 OpenCV 2.4.9 + QT5


 

选择Desktop平台,直到向导结束。

 Ubuntu14.04 内 安装 OpenCV 2.4.9 + QT5


 

现在需要告诉Qt 库文件安装在了哪里。打开 .pro 文件并添加以下路径:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
INCLUDEPATH   +=   .
INCLUDEPATH   +=   / usr / local / include
INCLUDEPATH   +=   / usr / local / include / opencv
INCLUDEPATH   +=   / usr / local / include / opencv2
INCLUDEPATH   +=   / usr / local / include / opencv2 / core
INCLUDEPATH   +=   / usr / local / include / opencv2 / highgui
INCLUDEPATH   +=   / usr / local / include / opencv2 / imgproc
INCLUDEPATH   +=   / usr / local / include / opencv2 / flann
INCLUDEPATH   +=   / usr / local / include / opencv2 / photo
INCLUDEPATH   +=   / usr / local / include / opencv2 / video
INCLUDEPATH   +=   / usr / local / include / opencv2 / features2d
INCLUDEPATH   +=   / usr / local / include / opencv2 / objdetect
INCLUDEPATH   +=   / usr / local / include / opencv2 / calib3d
INCLUDEPATH   +=   / usr / local / include / opencv2 / ml
INCLUDEPATH   +=   / usr / local / include / opencv2 / contrib
LIBS   +=   ` pkg - config  opencv   -- cflags   -- libs `

测试程序:

在我们的例程“hello world”里,显示一幅图像:当然是lena.jpg(注:OpenCV经典例图)。你需要把要显示的图像(lena.jpg)放在程序相应的debug或release目录里。否则程序会找不到图像而无法显示。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <QCoreApplication>
#include <opencv2/highgui.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
using   namespace   cv ;
using   namespace   std ;
int   main ( int   argc ,   char   * argv [ ] )
{
     Mat  image ;
     image   =   imread ( "lena.jpg" ) ;     // Read the file
     if ( ! image . data )
     {
         cout   <<   "nenhuma imagem!" ;
     }
     else
     {
         cout   <<   "imagem carregada!" ;
     }
     cv :: namedWindow ( "Teste imagem" ) ;
     cv :: imshow ( "Teste imagem" ,   image ) ;
     cv :: waitKey ( 0 ) ;
     return   0 ;
}

编译并运行,便会看到下列图像:

 

Ubuntu14.04 内 安装 OpenCV 2.4.9 + QT5