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:

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

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.

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:

/usr/local/lib

and then execute

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:

cd ./bin
cd ./open_test_core

 

QT5 + OpenCV together

First start QTCreator, in command line type

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

 

Create a new console project

 

new_project

 

Select Desktop as platform and follow the wizard until the end.

 

kit_selection

 

Now we need to tell to QT where we installed the libs. To do that open the .pro file and add the path.

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.

#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

 

lena_result

 

In the next posts I’ll start to dig a little more in OpenCV. I hope you guys are enjoying as much as I do. Below some references where to read more.

 

https://qt-project.org/doc/qtcreator-2.5/creator-overview.html 

http://docs.opencv.org/2.4/doc/tutorials/introduction/linux_install/linux_install.html

http://karytech.blogspot.ca/2012/05/opencv-24-on-ubuntu-1204.html  

http://linuxconfig.org/introduction-to-computer-vision-with-opencv-on-linux

 

Bye

Marcelo

Marcelo Jo

Marcelo Jo is an electronics engineer with 10+ years of experience in embedded system, postgraduate in computer networks and masters student in computer vision at Université Laval in Canada. He shares his knowledge in this blog when he is not enjoying his wonderful family – wife and 3 kids. Live couldn’t be better.

LinkedIn 

7 Responses to 'Installing OpenCV 2.4.9 + QT5 in Ubuntu'

  1. masoud says:

    Thanks for the notes!

    I copied / pasted the code as it is but the program crashes even before running the main() function…just a window named qtcreator_process_stub is opned and says PRess to close this window…

    I think it’s a seg-fault…do you have any idea?

    Thanks,

  2. Weza says:

    Great tutorial Marcelo.

  3. Fernando Madrigal says:

    Can you help me? I’ve reproduced the the example, I have Qt5 OpenCV 2.4.9 and Ubuntu whit GCC 5.3 64 bits as you. When compiling doesn’t drop me any error, but when I run it, it craches at line 20 with that message:

    The inferior stoped because it recived a signal from the operating system.

    Signal Name: SIGSEGV
    Signal Meaning: Segmentation fault.

    By the way I have the image in the rigth place.

    Thank you for your help.

    • marcelojo says:

      It’s really a generic error of segmentation fault. It can be lots of things. Do you copy paste the entire code as it is?

  4. Hadi says:

    When I compile :

    :-1: error: skipping incompatible /usr/local/lib/libopencv_core.so when searching for -lopencv_core
    :-1: error: cannot find -lopencv_core
    :-1: error: collect2: ld returned 1 exit status

    Please help me, whats wrong?

    • marcelojo says:

      Hello, I guess your opencv compilation was correct. After that did you do sudo ldconfig? You can test if opencv is ok as well as mentionned in the post.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.