You are on page 1of 10

OpenCV on Windows

From STAB Resources


Jump to: navigation, search

Contents
[hide]

1 What is OpenCV?
2 Installing OpenCV 2.0
3 Installing CodeBlocks
4 Setting up CodeBlocks with OpenCV 2.0
5 A test program

What is OpenCV?
OpenCV (Open Source Computer Vision) is a library of programming functions for real time
computer vision. As the name clearly implies, the library is free to use for everyone. Some
common application areas of OpenCV include:

Object Identification
Motion Tracking
Mobile Robotics
Gesture Recognition
Facial Recognition System
Human - Computer Interface

The library was originally written in C and later versions include code in C++ as well. OpenCV
is a cross platform library and runs on Android, Linux, Mac OS, Windows. This short tutorial
will guide you through the setup of OpenCV on Windows.
All the steps followed in this tutorial are known to work on Windows 7 (both 64 and 32 bit), with
OpenCV 2.0 and Codeblocks 10.05.

Installing OpenCV 2.0

1. Download the OpenCV 2.0 installer from sourceforge. Download the file that reads OpenCV2.0.0a-win32.exe

2. Once the installer is downloaded, double-click it and let the installer install OpenCV. I've kept
the installation directory as default, that is - "C:\OpenCV2.0"

Installing CodeBlocks
1. Download and install CodeBlocks IDE from CodeBlocks website. Note - Download the file
with the MingW setup - codeblocks-10.05mingw-setup.exe.
2. Once the installer is downloaded, you can install CodeBlocks with the help of the wizard.

Setting up CodeBlocks with OpenCV 2.0


1. Run CodeBlocks. First we'll create a new project. Go to File > New > Project.
2. In the project wizard, select 'Empty Project'. Give the project a Title and select a folder to
create your project in. Let the other default options remain and click Finish.

3. In the left side of your CodeBlocks window, under the Projects section, you'll see your new
project listed.

Next we set the Project Build Options to include the OpenCV Library. Goto 'Project > Build
Options > Search Directories'.
4. In the Compiler section, add the following path: "C:\OpenCV2.0\include". Do not keep paths
relative.

5. In the Linker section, under 'Search Directories' add this path: "C:\OpenCV2.0\lib". Once

again do not keep the paths relative.

6. Then in the 'Linker Settings' tab, add all the files present in the "C:\OpenCV2.0\lib" folder.

Click "OK" to save and exit the Build Options.

A test program
Now we are ready to write our first OpenCV program.
1. Goto File > New > File... Select 'C/C++ Source'. We are to create a C++ file.
2. Name the file you wish to create along with the path. I've named the file main.cpp. Make sure
that you have checked 'Add file to the active project' and the 'Debug' and the 'Release' options.
Click 'Finish'
3. You'll now see the file listed in the Projects Section under ProjectName > Sources.

Type the following test code into the file and save it.
#include<iostream>
#include<opencv/cv.h>
#include<opencv/highgui.h>
using namespace std;
using namespace cv;
int main()
{
Mat rgb = imread("pic.jpg",1);
Mat hsv;
cvtColor(rgb,hsv,CV_RGB2HSV);
imshow("original",rgb); //showing hsv will give same result
Mat rgb_channels[3];
Mat hsv_channels[3];
split(rgb,rgb_channels);
imshow("red",rgb_channels[0]);
imshow("green",rgb_channels[1]);
imshow("blue",rgb_channels[2]);

split(hsv,hsv_channels);
imshow("hue",rgb_channels[0]);
imshow("saturation",rgb_channels[1]);
imshow("value",rgb_channels[2]);
cvWaitKey(0);
}

4. Save the file and build your project. Goto Build > Build or press Ctrl + F9. It should say
Process terminated with status 0. 0 errors, 0 warnings.
5. This code outputs the file pic.jpg (Make sure you put an image file with the same name in the
same folder as the .cpp file) and also splits it into different channels as per the RGB and HSV
schemes. Run the program as Build > Run or press Ctrl + F10. You should see a bunch of
windows pop up.
If everything works out as mentioned, then you are completely ready to start using OpenCV! The
best part of OpenCV is its extensive documentation. Check out the OpenCV2.0 Reference
Retrieved from "http://stab-iitb.org/wiki/OpenCV_on_Windows"
Category: Image-Processing
Personal tools

Log in

Namespaces

Page
Discussion

Variants
Views

Read
View source
View history

Actions
Search
Go

Navigation

Main Page

Search

Random page
Help

Clubs

Aeromodelling Club
Krittika Club
Electronics Club
Math & Physics Club
Robotics Club
Web n Coding Club

Toolbox

What links here


Related changes
Special pages
Printable version
Permanent link

This page was last modified on 9 October 2011, at 05:30.


This page has been accessed 1,997 times.
Content is available under Public Domain.

Privacy policy
About STAB Resources
Disclaimers

You might also like