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.
#Reference
The installation procedures detailed below have been tested and verified using:
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.
$ cd Downloads
$ wget https://github.com/Itseez/opencv/archive/2.4.8.tar.gz
$ tar -xvf opencv-2.4.8.tar.gz
$ cd opencv-2.4.8/
$ mkdir release
$ cd release
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 ..
$ make
$ sudo make install
$ sudo ldconfig
pkg-config opencv --libs
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 followingstep 1 and 2
.
http://qt-project.org/downloads
$ chmod u+x path_to_downloaded_setup_file
$ ./path_to_downloaded_setup_file
LIBS += `pkg-config opencv --libs`
OR (for Mac OS/X)
QT_CONFIG -= no-pkg-config
CONFIG += link_pkgconfig
PKGCONFIG += opencv
#include
#include
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
#Reference
1.Establish a new Qt Widget Project
, e.g. OpenCV_test
.
2.Paste the following code then click build
.
Note the
#include
andusing namespace
part.
#include "mainwindow.h"
#include
#include
#include
#include
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;
}
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!