Hello World using OpenCV and Qt

Hello everyone!

Qt is one of the popular cross-platform application and UI frameworks allowing developers to incorporate both GUI and Non-GUI based features within their applications. The Qt SDK also helps in developing cross-platform OpenCV applications. The Qt Creator IDE is a tool that can be used to write, debug and execute programs. On the other hand, OpenCV as you might be aware is a well known image processing library. The combination of Qt and OpenCV is something that has attracted the attention of developers worldwide.

Through this post, we will learn how to write a simple Hello World application using OpenCV and Qt.

Pre-requisites: Windows 7 (64 bit), Qt Creator based on Qt 5.2.0, OpenCV

Step 1: Configure Qt for using OpenCV

In case you have not configured Qt for using OpenCV, I would recommend reading this presentation for setting up your Qt environment. Once you have configured your environment, you can proceed to the next step.

Step 2: Create a new Qt Project

Launch the Qt Creator IDE and create a new Qt console Application with the name OpenCV_HelloWorld. Choose the compiler (MinGW or Visual Studio) of your choice and proceed. Now, write the following code in your main.cpp file.

main.cpp

#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"

using namespace cv;

int main(int argc, char** argv){

    namedWindow("output",1);
    Mat output = Mat::zeros(120,350,CV_8UC3);
    putText(output,"Hello world :)", cvPoint(15,70), FONT_HERSHEY_PLAIN,3,cvScalar(0,255,0),4);
    imshow("Output",output);
    waitKey(0);
    return 0;

}

Step 3: Add path to OpenCV libraries

In order for Qt to make use of OpenCV, we need to include the path to the libraries in the Qt project (.pro) file as follows.

QT       += core

QT       -= gui

TARGET = OpenCV_HelloWorld
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app

SOURCES += main.cpp
INCLUDEPATH += F://softwares//opencv//opencv//sources//release//install//include

Finally, run qmake and build the project. Make sure no errors occur. If everything runs fine then you should see the following output!

qt_opencv_helloworld

So that’s it for this Qt tip. A lot more still to come! ๐Ÿ™‚

Packt announces new book for Android and OpenCV!

If you are working on Android, involved in designing image processing applications then most probably you would come across the use of OpenCV, a popular computer vision and real-time image processing library.

Keeping this is mind, print company Packt Publishing recently announced a new book titled Android Application Programming with OpenCV for those who wish to get an insight on using image processing in Android. The book helps you set up OpenCV and Android from scratch depending upon your work environment and includes easy to learn code samples for capturing, displaying real-time videos and still images.

On the whole, the book is a practical, hands on guide covering the fundamental tasks of computer vision with step-by-step instructions for writing both an application and reusable library classes. To know more, grab a copy of the book from over here. Happy reading! ๐Ÿ™‚

Getting started with OpenCV for Android

Hello everyone!

Today I would like to talk about a popular computer vision library called OpenCV (Open Source Computer Vision library) and how one should go about installing it for Android to start developing applications. The OpenCV library includes many algorithms that help to track camera movements, identify static and moving objects and also extract 3D models of objects. In addition it also helps in face recognition and focuses mainly on creating real time applications. At present it has C, C++, Python and Java interfaces and supports Windows, Linux, Android and the Mac OS platforms.

To start using OpenCV, first you need to download the latest version of OpenCV for the desired platform from here. In this post I would be focussing on installing OpenCV for Android.

Step 1: Once you have downloaded the .zip file, extract it in a folder. I extracted mine to C:\android-opencv\OpenCV-2.4.2-android-sdk. Once you have extracted the file you should get apk, samples, doc and the sdk folders as shown below.

OpenCV folder contents

Step 2: If you have the Android SDK, NDK and the ADT plugin installed and configured in Eclipse you can skip the following steps and proceed to step 6.

Step 3: In case you don’t have the Android SDK setup, you can do so by installing the Android SDK from over here. You can follow a detailed tutorial for this step. Make sure that you tick the Android NDK development tools while you are installing the ADT plugin for Eclipse.

Step 4: You need to install the Android NDK as well if you are planning to reuse your C++ code. You can download the current version from over here.

Step 5: Once all the installation is complete, open up Eclipse. Go to Window–>Preferences–>Android. Add the SDK and NDK locations properly and make sure you have no errors.

Step 6: In Eclipse, go to File–>Import–>Existing Android projects into workspace. Give the path to your samples folder inside the OpenCV installation directory. Import all the projects selected into your workspace.

Step 7: At this point, you may see that there are errors present. To remove these errors, again go to File–>Import–>Existing Android projects into workspace, but this time give the path to your sdk\java folder instead. Now right click on any project, go to Properties–>Android. Add the reference to your sdk\java folder. Click on Apply and then Ok and you will see that the errors have disappeared for the selected project. Repeat this for other projects also.

Add reference to sdk\java library

Connect your phone via USB and make sure it is connected to the Internet (just for now). Finally, now run any sample as an Android application. For the first time, OpenCV will ask you to install the the OpenCV Manager from the Google Play store. Install it, restart your application and now your OpenCV ready! ๐Ÿ™‚

Here are some screenshots I took while running the preloaded samples provided by OpenCV:

OpenCV: Android Camera

OpenCV: Canny Edge Detector

OpenCV: Gray scale

OpenCV: RGBA along with other options

Do read about: Android release notes, user guide and tutorials.