Installing OpenCV 2.4.9 + QT5 in Ubuntu
Hi,
Today is a small how to install OpenCV + QT5 in linux Ubuntu. For those who doesn’t know what is OpenCV, you can check here. Briefly OpenCV is a library for real time image processing created by Intel. It’s free for use under BSD license.
Installing QT
QT is a multiplataform application framework developed by Digia. Some plataforms supported are: Android, Embedde Linux, iOS, Windows, Windows CE, etc.
To install 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
|
Compiling OpenCV
First of all we need to install some required packages
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
|
Now, we get OpenCV here and we save it in a directory that we’ll call opencv5.
1
2
3
4
5
6
7
8
|
cd
opencv5
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
..
make
sudo make install
|
By default this library will be installed in /usr/local/lib. If you want to see some cmake options, read CMakeLists.txt.
In order to be able to link the lib, we need to create a file opencv.conf in /etc/ld.so.conf.d/ with the following:
1
|
/usr/local/lib
|
and then execute
1
|
sudo
ldconfig
|
Testing OpenCV
If you want to test if everything is OK, you can go to the directory ./build/bin and execute one of several tests like:
1
2
|
cd
./bin
cd ./open_test_core
|
QT5 + OpenCV together
First start QTCreator, in command line type
1
|
/opt/Qt/Tools/QtCreator/bin/qtcreator
|
Create a new console project
Select Desktop as platform and follow the wizard until the end.
Now we need to tell to QT where we installed the libs. To do that open the .pro file and add the path.
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`
|
Let’s test it and check if everything is working properly. In our OpenCV “hello world”, we’ll just show an image, of course, lena. You need to copie it in your project in Debug folder if you are in debug or Release if you are in release mode.
Below is our “hello world” code.
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;
}
|
Compile and run and if everything is perfect, we should have this image