Introduction to OpenCV
The easiest way to learn OpenCV is to buy & read the book “Learning OpenCV” from O’Reilly. Otherwise you can follow these instructions depending on your C/C++ compiler.
This webpage explains how to setup OpenCV v2.1.0. There is a newer version of OpenCV available (v2.2.0), but it seems to still have many bugs and is more difficult to setup compared to v2.1.0, so this tutorial explains v2.1.0 only.
Install OpenCV
For Visual Studio 2008:
- Download & install the prebuilt library “OpenCV-2.1.0-win32-vs2008.exe” from“http://sourceforge.net/projects/opencvlibrary/files/”
- Make sure you install OpenCV to a folder without spaces, such as “C:\OpenCV2.1”.
- Make sure you ask the installer to put the OpenCV bin folder on your system path.
- OpenCV should be ready for you to use in your projects straight away. (DLL files such as “cv210.dll” are in the “bin” folder, LIB files such as “cv210.lib” are in the “lib” folder, and INCLUDE files such as “cv.h” are in the “include\opencv” folder.
For Linux, Unix, Mac and other Windows compilers:
- Download & extract the source code from “http://sourceforge.net/projects/opencvlibrary/files/” in Zip or Tar format.
- Make sure you install OpenCV to a folder without spaces, such as “C:\OpenCV2.1”.
- Build OpenCV using CMake. Follow the Install Guide for your compiler at “http://opencv.willowgarage.com/wiki/InstallGuide”
Try out the demo programs
To get a feel for what is possible with OpenCV, try running the many sample programs that come with OpenCV in the “samples\c” folder (or the “samples\python” folder for the new Python interface, the “samples\swig_python” folder for the old Python interface, or the “samples\octave” folder to use GNU Octave with a similar style to Matlab).
- It is best to run the sample programs from a command line, because some of them print important information to the console, or require you to pass in arguments such as image filenames on the command-line.
- Also be aware that some of the OpenCV sample programs assume you have a webcam plugged in before running the program.
Compile your first OpenCV program
To learn how to configure an OpenCV C/C++ project, create a new project for your compiler and copy & paste the following source code into your project:
#include <stdio.h> // For printf()
#include <cv.h> // Main OpenCV library.
#include <highgui.h> // OpenCV functions for files and graphical windows.
int main(int argc, char* argv[])
{
// Open the file "lena.jpg".
IplImage* img = cvLoadImage("lena.jpg", CV_LOAD_IMAGE_UNCHANGED);
if (!img) {
printf("Error: Could not open the image file! \n");
exit(1);
}
// Blur the image.
cvSmooth(img, img, CV_BLUR);
// Save the blurred image to a file.
cvSaveImage("blurred.jpg", img);
// Show the blurred image on the screen.
cvNamedWindow("Blurred", CV_WINDOW_AUTOSIZE);
cvShowImage("Blurred", img);
// Wait for the user to press something on the graphical window.
// Note: cvWaitKey() is needed for time to draw on the screen.
cvWaitKey(0);
// Free the resources.
cvDestroyWindow("Blurred");
cvReleaseImage( &img );
return 0;
}
- To compile any OpenCV project such as this, you must first tell your compiler where to find the header include files (located in the “include/opencv” folder of OpenCV). For Visual Studio 2008, click on “Tools -> Options -> Projects and Solutions -> VC++ Directories”, then click on “Show Directories for:” and select “Include Files”. Add “C:\OpenCV2.1\include\opencv” (or wherever the file “cv.h” is) and hit OK. For other compilers, read the instructions at “http://opencv.willowgarage.com/wiki/InstallGuide#Configure”
- You must also tell your compiler where to find the lib files (located in the “lib” folder of OpenCV). For Visual Studio 2008, click on the same “Tools -> Options -> Projects and Solutions -> VC++ Directories”, then click on “Show Directories for:” and select “Library Files”. Add “C:\OpenCV2.1\lib” (or wherever the file “cv210.lib” is) and hit OK. For other compilers, read the instructions at “http://opencv.willowgarage.com/wiki/InstallGuide#Configure”
- Add the OpenCV lib files you will use. Lib files for release builds: cv210.lib, cxcore210.lib, cvaux210.lib and highgui210.lib. Lib files for debug builds: cv210d.lib, cxcore210d.lib, cvaux210d.lib and highgui210d.lib. For Visual Studio 2008, click on the Project “Properties -> Linker -> Input -> Additional Dependencies” and add “cv210d.lib cxcore210d.lib cvaux210d.lib highgui210d.lib” when in Debug configuration, then click on “Configuration” and select “Release”, then in Additional Dependencies, add “cv210.lib cxcore210.lib cvaux210.lib highgui210.lib”.
- Build the project. (For Visual Studio 2008, click “Build -> Build Solution”). If it gives error messages that refer to “.cpp”, “.c” or “.h” files then you haven’t setup the include files properly (step 1). But if it gives error messages that refer to “linker” or “.o”, “.obj”, “.lib” or “.a” files then you haven’t setup the library files properly (steps 2 and 3).
- Copy the image “samples\c\lena.jpg” to the same folder as the generated program (on Windows this is the EXE file in the Debug or Release folder).
- Run the program. You should see a window created with a blurry photo until you type something on your computer while the window is focused, and also there should be a new image “blurred.jpg” stored in the same folder as the program. If you instantly get a popup error saying it can’t find “cxcore210.dll” or “cxcore.dll” or anything like that, it means you don’t have the OpenCV runtime on your path (DLL files on Windows), so you should either copy the OpenCV DLL files into the same folder as your EXE or add OpenCV’s bin folder to your PATH system variable.
Modify the demo programs
Now you can copy and paste code from the sample programs in “samples\c” to do tasks that you are interested in. For example:
- “houghlines” for detecting lines in an image,
- “contours” for detecting curves in an image,
- “find_obj” for detecting objects in another image using SURF,
- “facedetect” for detecting faces through your webcam,
- “peopledetect” for detecting people in images,
- “lkdemo” for optical flow motion through your webcam,
- “letter_recog” for optical character recognition (OCR),
- “stereo_calib” to calibrate a stereo camera system.
Try making your own programs based on the docs
You should now be finally ready to combine and customize the sample code and some of the 500+ other functions using the reference documentation. Note that most OpenCV samples are using the traditional C interface, but OpenCV 2.0+ includes a new C++ interface that may be easier to use and learn. The full reference documentation is available for C, C++ and Python interfaces in the file “docs\opencv.pdf” in your OpenCV folder.
- The latest reference documentation for the C interface is available at “http://opencv.willowgarage.com/documentation/c/index.html”
- The latest reference documentation for the C++ interface is available at “http://opencv.willowgarage.com/documentation/cpp/index.html”
- There is an introduction to the C++ interface at “http://opencv.willowgarage.com/documentation/cpp/introduction.html” and some extra info at “http://opencv.willowgarage.com/documentation/cpp/c++_cheatsheet.html”
- A handy C++ Cheatsheet document is available at “http://opencv.willowgarage.com/wiki/Welcome?action=AttachFile&do=view&target=opencv_cheatsheet.pdf”
- The latest reference documentation for the Python interface (under construction) is available at “http://opencv.willowgarage.com/documentation/c/index.html”
Read tutorials about how to solve various problems
There are various FAQs and tutorials you can read to discover more things about OpenCV:
- The OpenCV Beginner’s FAQ shows a few things like how to access pixels directly, at “http://opencv.willowgarage.com/wiki/faq”
- There are also some tutorials on topics (such as Blob Detection, 3D Pose Estimation and Face Recognition) in the Full OpenCV Wiki at “http://opencv.willowgarage.com/wiki/FullOpenCVWiki”.
- The best way to learn how to use OpenCV is to read the O’Reilly book “Learning OpenCV: Computer Vision with the OpenCV Library”, that explains many important concepts such as Image Processing, Histogram Matching, Object Tracking, 3D Pose Estimation, Stereo Camera Calibration, Machine Learning, etc.
- Many tasks in OpenCV require state-of-the-art techniques to solve the problems. Therefore, a good place to find latest techniques is by reading papers from computer vision conferences such as CVPR and others at “http://www.cvpapers.com/”. There are also many free computer vision books online.
Join discussions with other OpenCV users
There is an active Yahoo usergroup where you can post OpenCV questions, at “http://tech.groups.yahoo.com/group/OpenCV/”
- Just remember that people will get angry if you ask questions that have been asked already, or if you just say “give me free source-code to do …”, rather than saying you have tried something and are having problems. So please search this usergroup or Google for similar questions before posting something, because there will nearly always be similar posts with your desired information ready without waiting for replies. Especially if you are asking about compiler errors!
Related Posts
- Speed testing a website
- Tbot: The Self-Balancing Transformer Robot
- Scripting form post in C# & .net
- Color Conversions
- Introduction to Face Detection and Face Recognition
- To RAID or not to RAID
- Best Concise Linux System Administrator’s Guide (SAG)
- Buying vs renting
- Script to check available diskspace in C#/.net
- RAID 1 vs RAID 5