您的位置:首页 > 编程语言 > Qt开发

Setting up OpenCV with Qt Creator in Ubuntu 13.10

2015-01-24 15:23 465 查看


Setting up OpenCV with Qt Creator in Ubuntu 13.10

HOMEABOUTRESEARCHRESUMENOTESBLOGSUBSCRIBE


1. Introduction

OpenCV
 (Open Source Computer Vision)
is a library of programming functions mainly aimed at real-time computer vision.

Qt
 is a cross-platform application
framework that is widely used for developing application software with a graphical user interface (GUI).

It's very helpful and powerful to combine OpenCV library and Qt Creator together, especially in Linux environment, since the Visual Studio of Microsoft can only run on Windows.


2. Installation

#Reference

The installation procedures detailed below have been tested and verified using:
Ubuntu 13.10 64-bit
OpenCV 2.4.8
Qt 5.2.1 for Linux 64-bit


2.1 Installing OpenCV

1.Open Terminal: Press 
Control-Alt-T
 to
open a Terminal instance.

2.Download and install CMake and other required dependencies (Note: 
qt-opencv-multithreaded
 requires
V4L. Also, Ubuntu 13.10 comes supplied with pkg-config):

$ sudo apt-get update
$ sudo apt-get install build-essential cmake libv4l-dev pkg-config


Optional packages:
libgtk2.0-dev The default GUI backend for highgui on OSes other than Windows and MacOSX.

libtiff4-dev For reading and writing TIFF images.

libjasper-dev Library for the coding and manipulation of images (e.g. JPEG-2000 format).

libavformat-dev Library containing demuxers and muxers for audio/video container formats.

libswscale-dev Library containing video image scaling and colorspace/pixelformat conversion routines.

libavcodec-dev Library containing all the FFmpeg audio/video encoders and decoders.

libjpeg-dev For reading and writing JPEG images.

libpng-dev For reading and writing PNG images.

3.Download OpenCV for Unix (choose a folder where you wish to download the file to: e.g. home/Downloads):
$ cd Downloads
$ wget https://github.com/Itseez/opencv/archive/2.4.8.tar.gz

4.Unpack archive and navigate to the newly created folder containing the OpenCV source:
$ tar -xvf opencv-2.4.8.tar.gz
$ cd opencv-2.4.8/

5.Create a new folder and navigate to it (the build files generated by CMake will go into this folder):
$ mkdir release
$ cd release

6.Use CMake to generate the makefile (add any other required flags with the -D option):

Note: Remember to specify the source folder as the last argument while invoking cmake.

$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..

7.Compile and install OpenCV:
$ make
$ sudo make install
$ sudo ldconfig

8.OpenCV runtime libraries (.so) can now be linked with simply: 
pkg-config
opencv --libs


2.2 Installing Qt

Note: Steps 4 and 5 explain how to configure Qt Creator for use with the OpenCV libraries.

You can also directly install Qt Creator with the 
online
installer
 from here. It's much easier and straightforward than the following 
step
1 and 2
.

1.Download the latest version of Qt 5 for Linux 32/64-bit from the Qt website:

http://qt-project.org/downloads

2.Follow the installation instructions on the download page. Installation is typically started with the following:
$ chmod u+x path_to_downloaded_setup_file
$ ./path_to_downloaded_setup_file

3.In Qt Creator, add the following to the .PRO file after creating a project:
LIBS += `pkg-config opencv --libs`


OR (for Mac OS/X)
QT_CONFIG -= no-pkg-config
CONFIG += link_pkgconfig
PKGCONFIG += opencv

4.Include the OpenCV header files in your C/C++ source file(s). For example:
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>

5.Your Qt-based OpenCV project can now be built and run using Qt Creator!


2.3 Compiling & running the application

1.Download the latest stable release of qt-opencv-multithreaded and unpack the downloaded archive: Downloads

OR

Get the "cutting-edge" version from the SVN repository (requires a SVN client): See instructions here.

2.Open 
qt-opencv-multithreaded.pro
 in
Qt Creator. You may also be asked to setup the project build targets - modify if required.

3.The application can be now be simply modified, compiled and run within Qt Creator!

IMPORTANT: If you receive compilation/linking errors such as /usr/bin/ld: cannot find -lGL or GL/gl.h: No such file or directory when using Qt 5, do the following:

$ sudo apt-get install libgl1-mesa-dev


2.4 Test in a new Qt project.

#Reference

1.Establish a new 
Qt Widget Project
,
e.g. 
OpenCV_test
.

2.Paste the following code then click 
build
.

Note the 
#include
 and 
using
namespace
 part.

#include "mainwindow.h"
#include <QApplication>

#include <iostream>
#include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;

int main( int argc, char** argv )
{
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}

Mat image;
image = imread(argv[1]); // Read the file

if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}

// Create a window for display.
namedWindow( "Display window", CV_WINDOW_AUTOSIZE );

// Show our image inside it.
imshow( "Display window", image );

waitKey(0); // Wait for a keystroke in the window
return 0;
}


3.Find the executable file, e.g. 
OpenCV_test
,
in the build folder, e.g.
build_OpenCV_test
.
Put an image, e.g. 
lena.png
, in this
folder, then open the terminal and type in:
$ cd ./path-to/build_OpenCV_test
$ ./OpenCV_test lena.png


Congratulations! Now you see the image!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: