Colombo 05, Sri Lanka
24-07-2010
Colombo, Sri Lanka
16-06-2010
Colombo, Sri Lanka
21-05-2010
Dehiwala, Sri Lanka
21-05-2010
Digital Image Processing with OpenCV



by
Lahiru Lakmal Priyadarshana



Image Processing With OpenCV – Chapter 02


We hope you enjoyed our previous article about Image Processing with OpenCV and we hope it inspired you to learn more about Image Processing and to try actual codes running.

In this article we will let you know how to install OpenCV on your computer and setup it up on your favourite IDE so you can say ‘hello’ to the world of image processing.

In the setting up section we will cover two major IDEs in detail; Visual Studio C++ Express and Code::Blocks. But if you don’t like to use any of those IDEs and still want use your favourite IDE then no problem, this Wiki will provide you lot of details for setting up OpenCV on many other IDEs. Further, this Wiki will help you to find lot of additional information about using OpenCV. So if you get stuck in any of the following steps that we are going to discuss in few seconds, we suggest you to refer this Wiki in the first place.



Downloading OpenCV?

Download OpenCV from the official project website. You can either download the lastest published version for Linux or Windows. Or you can try the SVN version of OpenCV.



Installing OpenCV


If you have downloaded the binaries for Linux or Windows, you can just use them to install OpenCV on your computer. For those who want to try the latest version from SVN should build and install OpenCV using a build tool like CMake or autotools.

Please refer following links to find more information about installing OpenCV on your computer.
 Installing OpenCV on Linux, Windows or MacOS 
Installing OpenCV on Linux (This is correct, but superseded by the above)



Setting up OpenCV on Visual C++ 2008 Express


Step 1: Linking DLLs


This is our very first step in setting up OpenCV on Visual Studio. First we must link all the required OpenCV DLLs with our project. To permanently include necessary DLL files, add "C:\Program Files\OpenCV\bin" (or the correspondent path according to your OpenCV installation) to PATH environment variable. To modify “System Variables”, right click on the “My Computer” icon and select “Properties”, then press the “System Variables” button in the “Advanced” tab.

Or as an alternative you can just copy necessary DLL files into the project directory with the source files.



Step 2: Customizing Global Options


Open Visual C++ 2008 Express Edition. In the menu bar, select: Tools -> Options

In the listing (left sidebar), choose: Projects and Solutions->VC++

Directories

First, select “Library files” from the “Show Directories for” List Box

Click the “New Line” icon (Ctrl+Insert), and locate the folder where you have installed OpenCV

(Example "C:\Program Files\OpenCV")

In the Library files list, locate and add the path to lib folder of OpenCV

(Example "C:\Program Files\OpenCV\lib")

These paths may differ based on where you installed OpenCV. “C:\Program Files\OpenCV” is the default installation path.




Now choose “Include files” in the list box, and locate and add the following directories:


C:\Program Files\OpenCV\cv\include

C:\Program Files\OpenCV\cxcore\include

C:\Program Files\OpenCV\otherlibs\highgui

C:\Program Files\OpenCV\cvaux\include

C:\Program Files\OpenCV\otherlibs\cvcam\include









Next, choose “source files” in the list box, and locate and add the following directories:

C:\Program Files\OpenCV\cv\src

C:\Program Files\OpenCV\cxcore\src

C:\Program Files\OpenCV\cvaux\src

C:\Program Files\OpenCV\otherlibs\highgui

C:\Program Files\OpenCV\otherlibs\cvcam\src\windows

Now click OK in the Options dialog.


Great!, You have successfully configured the global settings.




Step 3: Creating a New Project


Select from main menu: File -> New -> Projects… Choose "Win32 console application"

Type the project name (here we have used ‘Hello_diGIT’ but you can type any name you prefer) and choose the location


Click OK










In the Application Wizard, Just click Finish

After the above steps done Visual Studio will create the project folder (by default it has the same name as the project), <project name>.vcproj file, Solution <project name>.sln and, Three Source files: <project name>.cpp, stdafx.cpp and stdafx.h.


'StdAfx’ files are precompiled header files, which can be very useful if you want to reduce the compilation time.


For example, here we have created a new Project named ‘Hello_diGIT’. So we have a file named ‘Hello_diGIT.cpp’.




Open the ‘Hello_diGIT.cpp’ file, and include the OpenCV-related #include directives:


#include <cv.h>

#include <cxcore.h>

#include <highgui.h>


Note that these should be included after ‘stdafx.h’ or you may get build errors.

Now try to Build the Solution by pressing the F7 Key. There should be linker errors.



Step 4: Adding dependency projects into workspace


Choose from menu: Project ->Properties (Alt+F7)

Choose Configuration Properties->Linker -> Input category and add following to the “Additional Dependencies” field

cxcore.lib cv.lib highgui.lib











If the build process complains about a missing 'windows.h' header file, then you'll need to install the latest version of the Microsoft Windows SDK.

That’s it!

Now Build and Run the application using F5 key. Yeah, still nothing happens but you should be able to build the project with no errors at this step.

Now, for those who don’t like to use Visual Studio and those who are on Linux, let’s see how to setup OpenCV on Code::Blocks IDE.






Setting up OpenCV on Code::Blocks


Step 1: Configuring Code::Blocks


From the main menu select: Settings -> Compiler and debugger

In the window select, Global compiler settings and go to the tab, Search directories -> Compiler

Click Add and add all the following directories one by one

C:\Program Files\OpenCV\cv\include

C:\Program Files\OpenCV\cxcore\include

C:\Program Files\OpenCV\otherlibs\highgui

C:\Program Files\OpenCV\cvaux\include


              





Now go to the Linker tab (next to the Compiler tab) and add the following directory


C:\Program Files\OpenCV\lib






















Now in the Global compiler settings window go to Linker settings tab and add the following libraries to the Link libraries.


C:\Program Files\OpenCV\lib\cv.lib

C:\Program Files\OpenCV\lib\cvaux.lib

C:\Program Files\OpenCV\lib\cxcore.lib

C:\Program Files\OpenCV\lib\highgui.lib















Step 2: Creating a New Project


From the main menu select: File -> New -> Project

Select: Console application and press Go












 



You will get a startup wizard window. Click Next

In the next window select C++ and click Next

Enter Project name and location and click Next

Click Finish
















You just created a new project on Code::Blocks. Open the ‘main.cpp’ and add the following #include statements


#include <cv.h>

#include <cxcore.h>

#include <highgui.h>

Try Build -> Buld and Run, and you should get no errors. Now you are ready to write the hello world program with OpenCV on Code::Blocks.







Saying ‘Hello World’


We hope that you could be able to setup OpenCV on your favorite IDE. Now it’s time to write the long waited ‘HelloWorld’ program in OpenCV.

#include <cv.h>

#include <cxcore.h>

#include <highgui.h>

int main()

{

    IplImage* img = cvLoadImage("sample.png");

    cvNamedWindow("Image", CV_WINDOW_AUTOSIZE);

    cvShowImage("Image", img);

    cvWaitKey();

    cvDestroyWindow("Image");

    cvReleaseImage(&img);

    return 0;

}




Build and run the Project. You will get a new window with the image loaded that you specified in,

IplImage* img = cvLoadImage("sample.png");

Press any key to exit the application.

Congratulations! You just created the first application with OpenCV! In the next article let’s look into more details of the ‘HelloWorld’ program and some other cool stuff in OpenCV.

Before finishing the article, always try to learn things by your own. Use internet to find lots and lots of articles, references and tutorials. Remember, use the references whenever you get stuck, or please feel free to contact us.








Happy Hacking!

-Lahiru.


No votes yet

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.