Tuesday, June 3, 2014

Building applications with OpenCV inside Ubuntu with Code::Blocks and CMake

For building applications with OpenCV we need to set our Ubuntu as per earlier chapters where we discussed about how to set Ubuntu for PandaBoard and How to install OpenCV in Ubuntu. After these steps we can check in terminal whether successful installation of OpenCV took place or not.
pkg-config --cflags OpenCV
pkg-config --libs OpenCV

Fig 1: Ensuring OpenCV Installation
1    OpenCV with CMake
CMake is a cross-platform free software program for managing the build process of software using a compiler-independent method. It is designed to support directory hierarchies and applications that depend on multiple libraries, and for use in conjunction with native build environments such as make, Apple's Xcode, and Microsoft Visual Studio. It also has minimal dependencies, requiring only a C++ compiler on its own build system.
CMake can handle in-place and out-of-place builds, enabling several builds from the same source tree, and cross-compilation. The ability to build a directory tree outside the source tree is a key feature, ensuring that if a build directory is removed, the source file remains unaffected.
Another feature of CMake is the ability to generate a cache to be used with a graphical editor, which, when CMake is run, can locate executables, files and libraries. This information goes into the cache, which can then be tailored before generating the native build files.
Complicated directory hierarchies and applications that rely on several libraries are well supported by CMake. For instance, CMake is able to accommodate a project that has multiple toolkits, or libraries that each have multiple directories. In addition, CMake can work with projects that require executables to be created before generating code to be compiled for the final application. Its open-source, extensible design allows CMake to be adapted as necessary for specific projects.
CMake can generate makefiles for many platforms and IDEs including UNIX, Windows, Mac OS X, OS/2, MSVC, Cygwin, MinGW and Xcode.
The build process with CMake takes place in two stages. First, standard build files are created from configuration files. Then the platform's native build tools are used for the actual building.
Each build project contains a CMakeLists.txt file in every directory that controls the build process. The CMakeLists.txt file has one or more commands in the form COMMAND (args...), with COMMAND representing the name of each command and args the list of arguments, each separated by white space. While there are many built-in rules for compiling the software libraries (static and dynamic) and executables, there are also provisions for custom build rules. Some build dependencies can be determined automatically. Advanced users can also create and incorporate additional makefile generators to support their specific compiler and OS needs.

Example CMake file

cmake_minimum_required (VERSION 2.6)

PROJECT(WebcamFaceRec)

# Requires OpenCV v2.4.1 or later
FIND_PACKAGE( OpenCV REQUIRED )
IF (${OpenCV_VERSION} VERSION_LESS 2.4.1)
    MESSAGE(FATAL_ERROR "OpenCV version is not compatible : ${OpenCV_VERSION}. FaceRec requires atleast OpenCV v2.4.1")
ENDIF()

SET(SRC
    main.cpp
    detectObject.cpp
    preprocessFace.cpp
    recognition.cpp
    ImageUtils_0.7.cpp
)

ADD_EXECUTABLE( ${PROJECT_NAME} ${SRC} )
TARGET_LINK_LIBRARIES( ${PROJECT_NAME}  ${OpenCV_LIBS} )

Now go to the folder containing CMakeLists.txt file in terminal and write command for configuring cmake. The command is
gunjan@ubuntu:~/Desktop/Projects/Chapter8_FaceRecognition$ ./cmake .

Fig 2: CMake
gunjan@ubuntu:~/Desktop/Projects/Chapter8_FaceRecognition$ ./make



Fig 3: Make

After make of the project is successful you can execute the generated application with its name
For example here it is
 gunjan@ubuntu:~/Desktop/Projects/Chapter8_FaceRecognition$ ./WebcamFaceRec


Fig 4: Execution of Program

2    OpenCV with Code::Blocks

Code::Blocks is a free and open source, cross-platform IDE which supports multiple compilers including GCC and Visual C++. It is developed in C++ using wxWidgets as the GUI toolkit. Using a plugin architecture, its capabilities and features are defined by the provided plugins. Currently, Code::Blocks is oriented towards C, C++, and Fortran. It can also be used for creating ARM, AVR, D, DirectX, GLUT, GTK+,MATLAB, OpenGL, Qt and wx programs and applications, although in some cases installing third-party SDKs or frameworks is necessary.

Code::Blocks is being developed for Windows, Linux, and Mac OS X and has been ported to FreeBSD, OpenBSD,and Solaris.

2.1    Features

Compilers
Code::Blocks supports multiple compilers, including MinGW / GCC, Digital Mars, Microsoft Visual C++, Borland C++, LLVM Clang, Watcom, LCC and the Intel C++ compiler. Although the IDE was designed for the C++ language, there is some support for compilers of other languages, including GNU Fortran, Digital Mars D and GNU GDC. A plug-in system is included to support other programming languages.

Code editor
The IDE features syntax highlighting and code folding through the use of the Scintilla editor component, C++ code completion and class browser and an integrated to-do list. All of the open files are organized into tabs, which can be closed and opened at the user's will with the navigation pane or the close (X) button on the tabs. The code editor supports font and font size selection, this allows the user to choose the font and font size with which he is comfortable. In addition, it can provide specific syntax highlighting color theme that can be personalized.

Debugger
The Code::Blocks debugger has full breakpoint support. It also allows the user to debug their program by having access and using the local function symbol and argument display, user-defined watches, call stack, disassembly, custom memory dump, thread switching, CPU registers, GNU GDB Interface and MS CDB (not fully supported yet).

GUI designer
As of Code::Blocks 13.12 comes with a GUI designer called wxSmith. It is a derivative port of wxWidgets based on the 2.9.4 version.To make a complete wxWidgets application, the appropriate wx SDK must be installed with set environment variables.

User migration
Some of Code::Blocks' features are targeted at users migrating from other IDEs - these include Dev-C++ and Microsoft Visual C++ project import (MSVC 7 & 10), and Dev-C++ Devpak support.

Project files and build system
Code::Blocks uses a custom build system, which stores its information in XML-based project files, but it can optionally use external makefiles, which simplifies interfacing with projects using the GNU and Qt Software's qmake build systems.

2.2    Installing Code::Blocks on Ubuntu
This is a quick guide to get Code::Blocks up and running on your Ubuntu based Linux distribution. It is also going to make sure you can develop wxWidgets applications on your box as well. Look at the bottom of this guide for a complete command line that will install all the packages in one operation.
First be sure you have the necessary software to compile and debug programs.
1. Install the compiler.
sudo apt-get install build-essential
2. Install the debugger.
sudo apt-get install gdb
You'll need to install wxWidgets to use Codeblocks. Revisions from 4051 and after use wxWidgets 2.8.4. If you want to install them, make sure you have your universe and multiverse repositories enabled and install the following packages.
3. Install wxWidgets library. (This package is all that is needed to run any application that uses wxWidgets. ie. Code::Blocks)
sudo apt-get install libwxgtk2.8-0
4. Install the wxWidgets developement packages. (This is used to develop wxWidgets applications of your own.)
sudo apt-get install libwxgtk2.8-dev
5. Install Code::Blocks.
sudo apt-get install codeblocks

2.3    Configuring CodeBlocks IDE for use with OpenCV

·         Create a new C++ console project in CodeBlocks.
·         With the new project, right click on the project in the left bar and choose "Build Options..".
·         Click on the "Search Directories" tab, then Compiler tab, and add these locations:                    /usr/local/include/opencv
/usr/local/include/opencv2
·         Still on the "Search Directories" tab, then click on the the Linker tab and add these locations:
/usr/local/lib
·         Then click on the "Linker Settings" tab and be sure to add all of the OpenCV libraries. This is a list of all the libraries that I had installed on my system:
§  /usr/local/lib/libopencv_calib3d.so
§  /usr/local/lib/libopencv_contrib.so
§  /usr/local/lib/libopencv_core.so
§  /usr/local/lib/libopencv_features2d.so
§  /usr/local/lib/libopencv_flann.so  
§  /usr/local/lib/libopencv_gpu.so
§  /usr/local/lib/libopencv_highgui.so
§  /usr/local/lib/libopencv_imgproc.so
§  /usr/local/lib/libopencv_legacy.so  
§  /usr/local/lib/libopencv_ml.so
§  /usr/local/lib/libopencv_nonfree.so
§  /usr/local/lib/libopencv_objdetect.so
§  /usr/local/lib/libopencv_photo.so
§  /usr/local/lib/libopencv_stitching.so
§  /usr/local/lib/libopencv_ts.so
§  /usr/local/lib/libopencv_video.so
§  /usr/local/lib/libopencv_videostab.so
·         Once you have all that taken care of you can now go ahead and edit the main.cpp source code file. Copy and paste this sample code to test out your setup:

#include
#include
#include
#include

int main(int argc, char** argv)
{
cv::Mat frame;
int c;
CvCapture *capture = cvCaptureFromCAM(0);
while(1)
{
frame = cvQueryFrame(capture);
cv::imshow("result",frame);

c = cv::waitKey(10);
if(c==27)
break;
}
return 0;
}

·         Done! You can now build and run the sample application to make sure it's working. You should get a new window displaying the image from your webcam. You should now be able to copy the project and use it as a template for future work.


Fig 5: Result

No comments:

Post a Comment