Setting up OpenCV with Qt Creator in Ubuntu 13.10

本文提供了一个详细的指南,教你如何在Ubuntu 13.10环境下使用Qt Creator集成OpenCV库,包括安装步骤、配置Qt Creator以支持OpenCV库以及创建并运行一个简单的基于Qt的OpenCV应用。

Setting up OpenCV with Qt Creator in Ubuntu 13.10

HOME ABOUT RESEARCH RESUME NOTES BLOG SUBSCRIBE

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!

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值