Learning OpenCV

Gary Bradski, Adrian Kaehler

Mentioned 34

Describes the features and functions of OpenCV along with information on how to build applications using computer vision.

More on Amazon.com

Mentioned in questions and answers.

I am currently working on a system for robust hand detection.

The first step is to take a photo of the hand (in HSV color space) with the hand placed in a small rectangle to determine the skin color. I then apply a thresholding filter to set all non-skin pixels to black and all skin pixels white.

So far it works quite well, but I wanted to ask if there is a better way to solve this? For example, I found a few papers mentioning concrete color spaces for caucasian people, but none with a comparison for asian/african/caucasian color-tones.

By the way, I'm working with OpenCV via Python bindings.

Have you taken a look at the camshift paper by Gary Bradski? You can download it from here

I used the the skin detection algorithm a year ago for detecting skin regions for hand tracking and it is robust. It depends on how you use it.

The first problem with using color for tracking is that it is not robust to lighting variations or like you mentioned, when people have different skin tones. However this can be solved easily as mentioned in the paper by:

  1. Convert image to HSV color space.
  2. Throw away the V channel and consider the H and S channel and hence discount for lighting variations.
  3. Threshold pixels with low saturation due to their instability.
  4. Bin the selected skin region into a 2D histogram. (OpenCV"s calcHist function) This histogram now acts as a model for skin.
  5. Compute the "backprojection" (i.e. use the histogram to compute the "probability" that each pixel in your image has the color of skin tone) using calcBackProject. Skin regions will have high values.
  6. You can then either use meanShift to look for the mode of the 2D "probability" map generated by backproject or to detect blobs of high "probability".

Throwing away the V channel in HSV and only considering H and S channels is really enough (surprisingly) to detect different skin tones and under different lighting variations. A plus side is that its computation is fast.

These steps and the corresponding code can be found in the original OpenCV book.

As a side note, I've also used Gaussian Mixture Models (GMM) before. If you are only considering color then I would say using histograms or GMM makes not much difference. In fact the histogram would perform better (if your GMM is not constructed to account for lighting variations etc.). GMM is good if your sample vectors are more sophisticated (i.e. you consider other features) but speed-wise histogram is much faster because computing the probability map using histogram is essentially a table lookup whereas GMM requires performing a matrix computation (for vector with dimension > 1 in the formula for multi-dimension gaussian distribution) which can be time consuming for real time applications.

So in conclusion, if you are only trying to detect skin regions using color, then go with the histogram method. You can adapt it to consider local gradient as well (i.e. histogram of gradients but possibly not going to the full extent of Dalal and Trigg's human detection algo.) so that it can differentiate between skin and regions with similar color (e.g. cardboard or wooden furniture) using the local texture information. But that would require more effort.

For sample source code on how to use histogram for skin detection, you can take a look at OpenCV"s page here. But do note that it is mentioned on that webpage that they only use the hue channel and that using both hue and saturation would give better result.

For a more sophisticated approach, you can take a look at the work on "Detecting naked people" by Margaret Fleck and David Forsyth. This was one of the earlier work on detecting skin regions that considers both color and texture. The details can be found here.

A great resource for source code related to computer vision and image processing, which happens to include code for visual tracking can be found here. And not, its not OpenCV.

Hope this helps.

I am learning OpenCV using Learning OpenCV book.

One problem I am facing while compiling the code is that I have to write a long command to compile and get the executable.

This is the command I am using

g++ `pkg-config –cflags opencv` file_name.cpp -o output_file `pkg-config –libs opencv`

I am no Make expert but I think I can eliminate writing that long command using make. Before that I should explain my work flow. I have created a directory called opencv in my home directory (~/opencv/). I am reading the book section by section and coding the examples or exercises into new cpp source code files in that directory. So I don't know the names of the files before hand.

Now what I want make to do is,

Suppose I have coded a new file named facedetect.cpp in my opencv directory, and if I call make like this

make facedetect

then I want make to execute the following command for me

g++ `pkg-config --cflags opencv` facedetect.cpp -o facedetect `pkg-config --libs opencv`

so that whenever I make a new file named abc.cpp, I will execute make abc so that I can run

$ ./abc

at my command line to test my abc.cpp

Please give that make file so that I can save the frustration of typing that long command each time.

PS: I have Googled for help on this and found this on using CMake but I could not understand what that does. Kindly also explain how can I use CMake for the same task.

You can create a file called Makefile in you working directory like this:

CFLAGS = `pkg-config --cflags opencv`
LIBS = `pkg-config --libs opencv`

% : %.cpp
        g++ $(CFLAGS) $(LIBS) -o $@ $<

then you can use this file for all your single-file programms. Just call make with the basename of the file you want to compile. For facedetect.cpp that would be

make facedetect

Here some more details:

The general format of a makefile is like this:

target : dependecy1 dependenc2 ...
    command that generates the target

So for your example you could write:

facedetect : facedetect.cpp
    g++  $(CFLAGS) $(LIBS) -o facedetect facedetect.cpp

For each new example you can now create a new target. But you can also make it more general:

% : %.cpp
    g++  $(CFLAGS) $(LIBS) -o $@ $<

Here % matches any nonempty substring. The automatic variables $@ and $< substitute the names of the target file and the source file. For more information you can consult the make documentation.

i want to do a small project on object recognition, any any tools or literature suggestions on this topic ?


You will probably need to work with Image Processing techniques in your project. A very good introductory book to this area is the Digital Image Processing by Gonzalez and Woods. It covers topics such as image segmentation, which is a technique used to separate the objects to be recognized from the rest of the image

After you have identified the objects in the input image, the next step consists in finding a way to measure how similar they are to one another. Probably, the best way to do that is to use image descriptors. Usually, for object recognition, the best class of descriptors are the ones based on shape. The article "Review of shape representation and description techniques" by Zhang D. and Lu G. provides a great review about shape descriptors.

Finally, you have to classify those objects. [Machine Learning] by Mitchell is a classical book that discusses techniques such as k-NN that you can use in your project.


OpenCV or Matlab. I particularly use OpenCV and I really like it for the following reasons:

  • Very good documentation and a great number of good books and tutorials about it.
  • A number of segmentation algorithms implementations, such as the Otsu method and the Watershed.
  • Provides basic GUI and Media IO.

High, I need to do some image manipulations on CT volume images. Mainly segmentations. Which open-source library supports 3D algorithms - Filtering, edge detection, deformable objects and so ? Language is not an issue at the moment.


I'm going to set up some devices to perform real-time 3D motion tracking. My first idea to do so is to use a pair of cameras to take stereo image and calculate the depth map to get the 3D data I needed.

Are there any good open source libraries (C/C++) available and is fast enough for real-time(~12-24fps)? I've found 3D Reconstruction using Stereo Vision and EStereo in SourceForge, have anyone tried them?

Or any algorithm suggestion that I can implement?

Opencv has a whole section on this, see chapter 12 of Learning Opencv

I have a basic understanding in image processing and now studying in-depth the "Digital Image Processing" book by Gonzales, but have an urgent task and will appreciate help from somebody experienced in this area.

When image given and object of interest approximated form is known (e.g. circle, triangle), what is the best algorithm / method to find this object on image?

The object can be slightly deformed, so brute force approach will not help.

I strongly recommend you to use OpenCV, it's a great computer vision library that greatly help with anything related to computer vision. Their website isn't really attractive, nor helpful, but the API is really powerful.

A book that helped me a lot since there isn't a load of documentation on the web is Learning OpenCV. The documentation that comes with the API is good, but not great for learning how to use it.

Related to your problem, you could use a Canny Edge detector to find the border of your item and then analyse it, or you could proceed with and Hough transform to search for lines and or circles.

I tried to read Digital Image Processing by Gonzalez/Woods but I found it difficult to understand/grasp. I have taken a Graduate Course in Computer Vision, which is more practically oriented and I am doing lot of cool stuff with OpenCV, however I still feel I am swimming in higher abstractions, and do NOT understand the basics beneath.

I am planning to read a book on Computer Vision/Image Processing during the Winter Break to solidify my understanding of the content and would appreciate some must-read suggestions

I have done assignments like - camera calibration, image transforms, stitching images into panoramas, haar classification.

Have a look at this book. It's quite heavy (and expensive!), but it covers a lot of topics, and each chapter is authored by a different person that is competent in the corresponding field. If cost is a huge issue, I've seen reprints from Taiwan that appear to be legitimate for a fraction of the original price (they are soft cover, though, and the print quality is obviously not as good).

Mind you, I've got both The Handbook and Gonzalez & Woods, and I've found Gonzalez to be easier to digest during the initial stages. Rather than just reading, it is definitely recommended to attempt to reproduce all the examples that they give, and make an honest attempt at the exercises at the end of each chapter. The Handbook is great for coverage but lacks exercises.

Finally, your choice of must read really depends which specific direction you are expecting to be working in. The basic knowledge (spatial and frequency domain filtering, for example) has been around since the dawn of the field (early 60s) and is usually covered fairly well by most texts. If you want to learn about more recent applications, you have be a bit more specific (or go for The Handbook as it attempts to cover it all).

Gonzales and woods (or Wintz in my day) is a very good introduction.

There is a more readable but less concise introduction - Image-Processing-Analysis-Machine-Vision

And since you are working with opencv - you can do worse than read the opencv book

I have a project where I need to identify the license plate of a car using OpenCV.

I want to load an image of a number or a letter and let OpenCV identify it and print it to the console.

Is there a function do this? If not, how can I do it?

Note: I am working on gray level

Please help, I have to make it a week from now

thank you for your fast answer

I am working with Microsoft Visual C++ 2008 Express Edition

and about library the following libraries are whwat i used:

"C:\Program Files\OpenCV\lib"
"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\_graphics\include"
"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\_graphics\src"

if you are looking to learn more about OpenCV generally, you a good place to start is with this book: Learning OpenCV by Bradksi et al.

I have been finding a book for OpenCV, but Learning OpenCV is all what I can find. Unfortunately, the book is written for OpenCV 1.1 and is quite out of date. So I searched in Stackoverflow and found this thread. After reading the thread, I understand I can still learn OpenCV from the book, but I am afraid the code differences between OpenCV 1.1 and 2.2 will make learning OpenCV frustrating, especially for a complete novice like me. Currently I am finding for a book or tutorial website that is specifically written for OpenCV 2.2, so I can follow the code in the book and get started more easily.

By the way, I would like to know that which language integrates best with OpenCV? I am wondering is using Emgu CV (OpenCV for .net) with C#.net a good choice? Thanks!

OpenCV 2 Computer Vision Application Programming Cookbook was published in June 2011. It covers the newer C++ APIs, so it may be what you're looking for.

Learning OpenCV book is recently updated to OpenCV version 2.4. Here is the link. Also there is a good reference manual for OpenCV version 2.4.2 here[pdf].

How do services like card.io work behind the scenes? Do they use an OCR library like Tesseract or is it more complex?

Also, in this video, it looks like the app is waiting for you to hold your card in a specific range from the camera inside the green boundaries on the camera, and when you do it takes the photo automatically. If the image recognition is happening on the server, how did they do that?

How can I implement my own mobile 2D object scanner? Where do I start?

Josh from card.io here. I can't tell you the details of how card.io works (hopefully others will speculate here), but I can answer some of your other questions.

card.io does not use an OCR engine; we looked at Tesseract and others and found that they did not work well on many credit cards.

card.io's image recognition happens entirely on the phone. Early versions required some server assistance, but even those did a good chunk of the work on the phone.

To get started, I recommend sitting down and reading Learning OpenCV; it is a good general introduction to computer vision. Then play around and ask more detailed questions. On the machine learning side, Theano and Eigen are very helpful libraries.

I want to develop a system that can track and follow a road. Initially, I'd like to handle well-defined roads only and maybe later incorporate tracking for roads that aren't so well defined.

The problem I'm facing is that I don't know where to get started. I am new to image processing and I was hoping I could get some pointers on where to start off and what books to read on the subject. I am an 'experienced' programmer (I can program in C and Python fairly well, and can handle C++ and Objective-C), so the code itself isn't that big of an issue - its just "where do I start? what do I read?" thats confusing me. I am also open on learning another language if it helps me in anyway.

I'll appreciate any pointers/suggestions regarding this.

For a hands-on: Learning OpenCV: Computer Vision with the OpenCV Library by Gary Bradski and Adrian Kaehler. This book will give you a nice introduction to a lot of CV topics, references to go further and code examples using OpenCV, probably the most used computer vision library nowadays (recently, NVidia announced porting part of the code to their GPUs). OpenCV presents C, C++ and Python APIs.

Gonzalez and Woods' Digital Image Processing is a nice companion of image processing techniques.

Update I forgot the hot new Szelisk's book. There is a free (and good) draft available!

I am using opencv with C, and I am trying to get the extrinsic parameters (Rotation and translation) between 2 cameras.

I'm told that a checkerboard pattern can be used to calibrate, but I can't find any good samples on this. How do I go about doing this?


The suggestions given are for calibrating a single camera with a checkerboard. How would you find the rotation and translation between 2 cameras given the checkerboard images from both views?

I was using code from http://www.starlino.com/opencv_qt_stereovision.html. It has some useful information and code of the author is pretty easy to understand and analyze, it covers both - chessboard calibrate and getting depth image from stereo cameras. I think it's based on this OpenCV book

I am working on backgroun subtraction under sudden light change. Is there any example code or way to do this efficiently in OpenCV or IPP? I am reading video frames, so the running time should be very fast. Thanks in advance.

This book contains all needed for you information.

I am really new to digital image processing and is fixed with the below mentioned problem:- I need to write a C program which will load a ppm image file and do line detection with convolution kernels. Any kind of help will be appreciated.

  1. First off, you need open the file with fopen(..)
  2. Second you need to design an array or data structure store the image.
  3. Then you need to design a mask for each type of angle
  4. Add a border depending on the size of the mask, and convolve the mask with the image
  5. save unto a file

Might find this link helpful http://dasl.mem.drexel.edu/alumni/bGreen/www.pages.drexel.edu/_weg22/edge.html

To Implement the masks in the links you're refering to, take this code and change GX and GY to a) and b)

/* 3x3 GX Sobel mask. Ref: www.cee.hw.ac.uk/hipr/html/sobel.html */

GX[0][0] = -1; GX[0][1] = 2; GX[0][2] = -1;

GX[1][0] = -1; GX[1][1] = 2; GX[1][2] = -1;

GX[2][0] = -1; GX[2][1] = 2; GX[2][2] = -1;

/* 3x3 GY Sobel mask. Ref: www.cee.hw.ac.uk/hipr/html/sobel.html */

GY[0][0] = -1; GY[0][1] = -1; GY[0][2] = -1;

GY[1][0] = 2; GY[1][1] = 2; GY[1][2] = 2;

GY[2][0] = -1; GY[2][1] = -1; GY[2][2] = -1;

Same for c) and d)

Other than creating from scratch you could also use the open source openCV http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/canny_detector/canny_detector.html There are online documentation and textbooks on how to use it too http://www.amazon.com/dp/0596516134

I am searching in the Computer Vision topic, I don't have any previous experience.I found a good library OpenCv with python interfacing .I have started reading it's documentation, but I have faced many new concepts and names, that I am not familiar with, such as Compute the Laplacian , GoodFeaturesToTrack, CreateMat and many others

So my questions is what I have to study before starting with this library, to understand the functions of the library ?

I would recommend a book by OpenCV author Gary Bradski - Learning OpenCV: Computer Vision with the OpenCV Library.

It is not only a refence how to use OpenCV, but also a comprehensive book on many computer vision topics, with many images that illustrate the concepts. It guides you through OpenCV basics, then trough image processing using filters, convolutions, histograms, contours, segmentation, tracking, camera calibration and 3D vision.

It is relatively easy to read (not too much math, just enough), I liked it really much.

I would recommend you the book of gonzalez and woods - Digital image processing for the image processing basics.

there is a book on opencv but is for version 1.0. It is a decent book anyway.

Anyway openCV is a good library image processing but you would still need the basics of programming (don't forget this).

I am sorry about the Title for this question, but really I don't know how to name it. There is something that I have seen sometimes, and I ask myself how to do that kind of software. It's about the PitchTrax that MLB uses in its TV-games. I think that is did it using cameras or something like that... I would like to read (or to learn) how to do little applications using this kind of technology, but I don't know anything about that, really I don't know where "to start" studying for this... Do you know something about this??

I am sorry about my English.

I don't know pitch trax specifically (try google or their site)

But the general field of object tracking and image processing then openCV is probabyl a good place to start (as a programmer)

There is a good book at http://www.amazon.com/Learning-OpenCV-Computer-Vision-Library/dp/0596516134/ref=sr_1_1?ie=UTF8&s=books&qid=1262807256&sr=8-1

I am working on developing an iOS video app that needs to do stuff like apply filters, adjust brightness/contrast/saturation add overlays etc. As I am new to image processing I am not able to judge which resources (i.e. APIs, open source libraries) I can use. So any guidance from those who have experience in this field will be of great help.

Use OpenCV.

1. Get OpenCV

Check out OpenCV homepage to download OpenCV source.

2. Check out this SOF for more details on OpenCV on iOS

iPhone and OpenCV

3. Get and read some good books on OpenCV

The best book on OpenCV is "Learning OpenCV" written by Gary Bradsky, main founder of OpenCV.

Second one is "OpenCV cookbook".

These books contains lots of examples on OpenCV along with description

4. Check out OpenCV documentation.

OpenCV documentation contains details of complete functions. It also includes a lot of tutorials, which are really good for all.

5. Also try running OpenCV samples. It contains a lot of good programs

All the best.

I have two cameras setup as shown in following picture:

enter image description here

What I have with this setup:

  • Top view of this green object with (x1,y1) as its 2D image coordinates.
  • Side view of this green object with (x2,y2) as its 2D image coordinates.

How can I trace/identify real world 3D coordinates (x,y,z) of this green object via these two cameras (i.e combination of top view image and side view image)?

I know how to calibrate single camera using JavaCV/OpenCV but I don't know how perform stereo calibration other stuff using JavaCV/OpenCV.

What is the step-by-step procedure for doing such stereo calibration via two cameras?

What would be the output (e.g., intrinsic matrix, distortion etc.) of such a stereo calibration process and how can I use that output in order to compute the real world 3D coordinates of this green object?

Hi the following blog will answer all your questions... http://blog.martinperis.com/2011/01/opencv-stereo-camera-calibration.html

No image was uploaded.. Just check or else you could find theoritical concepts from the book http://www.amazon.com/Learning-OpenCV-Computer-Vision-Library/dp/0596516134

or for the actual code implementing with cameras one can refer to http://www.youtube.com/watch?v=jVdKK_5l9XU&feature=plcp

I'm having some images, of euro money bills. The bills are completely within the image and are mostly flat (e.g. little deformation) and perspective skew is small (e.g. image quite taken from above the bill).

Now I'm no expert in image recognition. I'd like to achieve the following:

  • Find the boundingbox for the money bill (so I can "cut out" the bill from the noise in the rest of the image
  • Figure out the orientation.

I think of these two steps as pre-processing, but maybe one can do the following steps without the above two. So with that I want to read:

  • The bills serial-number.
  • The bills face value.

I assume this should be quite possible to do with OpenCV. I'm just not sure how to approach it right. Would I pick a FaceDetector like approach or houghs or a contour detector on an edge detector?

I'd be thankful for any further hints for reading material as well.

There is a good book on openCV

Using a Hough transform to find the rectangular bill shape (and angle) and then find rectangles/circles within it should be quick and easy

For more complex searching, something like a Haar classifier - if you needed to find odd corners of bills in an image?

I am looking for a way to separate the part of an image that contains a person from the background in an image. It does not have to be very accurate, rough borders will do too. It can be an algorithm, a software lib (pref. open source), or even a reference to a relevant AI or image processing material. Having to train the solver is acceptable. However, the final application have to be reasonably lightweight, as it would have to run on a smartphone.

Start with OpenCV. It's an open-source computer-vision library that already contains some algorithms for that. Try what it's got and see if it's good enough for your needs.

The “Learning OpenCV” book will give good introduction into computer vision and machine learning.

I have started learning OpenCV. I am working on linux. From their documentation page I was able to compile this http://docs.opencv.org/doc/tutorials/introduction/linux_gcc_cmake/linux_gcc_cmake.html#linux-gcc-usage

However after that I got lost in trying to declare a new mat and it's constructors. SO I decided to go with this book http://www.amazon.com/Learning-OpenCV-Computer-Vision-Library/dp/0596516134 However I am not able to compile the very first program from this book. The program is here :

#include "highgui.h"
int main(int argc, char** argv)
    IplImage* img = cvLoadImage (argv[1]);
    cvNamedWindow("Example1", CV_WINODW_AUTOSIZE);

I saved this in a file named load.c

Then I created a CMakeLists.txt file and put this in it :

project( load )
find_package( OpenCV REQUIRED )
add_executable( load load )
target_link_libraries( load ${OpenCV_LIBS} )

when running "cmake ." from terminal it is succesful. But when I am running "make" it gives me this error :

Scanning dependencies of target load
[100%] Building C object CMakeFiles/load.dir/load.o
/home/ishan/load/load.c: In function ‘main’:
/home/ishan/load/load.c:4:2: error: too few arguments to function ‘cvLoadImage’
/usr/local/include/opencv2/highgui/highgui_c.h:212:18: note: declared here
/home/ishan/load/load.c:5:28: error: ‘CV_WINODW_AUTOSIZE’ undeclared (first use in this  function)
/home/ishan/load/load.c:5:28: note: each undeclared identifier is reported only once for each function it appears in
make[2]: *** [CMakeFiles/load.dir/load.o] Error 1
make[1]: *** [CMakeFiles/load.dir/all] Error 2
make: *** [all] Error 2

I think it is because this example in the book is for OpenCV 1.x while I am currently running 2.4.3, however I believe there must be a way to run this program and the subsequent program that are in the book. I think the problem lies with linking the header files properly. I would like to first read from the book and using reference from documentation and then switch to documentation fully. But for now I wish to learn from the book as learning from the book is far easier to me than documentation. Plus I bought this book for approx 3000 INR and got it just today, I don't want to see it go to waste. I want to learn from it.

Please help me out.

CV_WINODW_AUTOSIZE is mispelled. The correct constant is CV_WINDOW_AUTOSIZE

cvLoadImage (argv[1]); should be cvLoadImage (argv[1], 1); (for loading a color image) because the C standard does not support default arguments.

By the way, if you're using OpenCV 2.0+, I recommend learning the C++ API. It's a lot less convoluted than the C API and performance is comparable.

I'm just getting started with OpenCV. I came across this question, and I'm trying to find where that code came from. Any ideas?

If you don't have reference image of the background then you could try to make an average of first n frames and use such resultant image as a reference. This number, n, could be 20 or similar and it should be enough if the scene isn't too complex. I recommend you to read a chapter about background subtraction in official OpenCV book (http://www.amazon.com/Learning-OpenCV-Computer-Vision-Library/dp/0596516134) as there are some of other similar techniques presented.

I want to segregate the frames that have high correlation. Suppose in a video the first 6 seconds, the scenes do not change much except that there is a very little movement of objects. How can I separate out those frames by finding the correlation between all of them. ....

For this I visited this link but it did not help much ....

Please help me in this problem !

You can use image subtraction. Cross-correlation is also acceptable here.

Google search phrase: background subtraction algorithm.

Also this book contains needed info for you.

My goal is to be able to have a person with a mobile phone snap a picture of a local landmark (building or otherwise (ex. gazebo, statue, etc)) on our college campus and be able to identify the landmark and tell them what it is.

For instance, they are walking around and they see a large building with a metal dome. They don't know what it is, but it looks interesting, so they snap a picture and the app tells them that it's the basketball center (and other relevant info).

My limited knowledge in this particular field led me to think of using neural networks and training the program to recognize particular places. If this is the case, please also give me resources for this option, as the extent of my knowledge of NN is that they can be used to recognize things if they are trained. :)

I know of the OpenCV library, but as I am not a C developer, I'd like to know if I need to go down that road before I start. I primarily work in Java, but I'm not opposed to getting my hands dirty.


This is in response to your original question. The best resource would be the O'Reilly book Learning OpenCV

You can read the thing on Google books for free and it uses C along with OpenCV. You can use python or Java to suit your work.

The OpenCV library includes haar training and sample programs on training it for face/text recognition. After that you'll basically have to figure things out. Another useful resource I just stumbled upon is Intel's reference manual for OpenCV. So, good luck!

Now I control robot to run tic-tac-toe game
but i couldn't know how to use opencv library to recognize game board and inner square
I wonder how to use algoritm to get corner of game board and 9X9 Game square

This is what i do get the corner

Binary Image:


#include <stdio.h>
#include <fcntl.h>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>

int main(void){ 
IplImage *img=NULL;
IplImage *gray=NULL;
CvMemStorage* contour_storage;
CvSeq* contours=NULL;
CvPoint2D32f *corners;

img = cvLoadImage("tictactoe.bmp",CV_LOAD_IMAGE_COLOR);
gray = cvCreateImage( cvGetSize(img), IPL_DEPTH_8U, 1 );

cvSmooth( img, img, CV_GAUSSIAN, 3, 0, 0, 0);
cvCvtColor(img, gray, CV_RGB2GRAY);
cvAdaptiveThreshold(gray, gray, 128,CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY_INV, 3, 5);
cvNamedWindow( "Threshold", CV_WINDOW_AUTOSIZE);
cvShowImage("Threshold", gray);

contour_storage = cvCreateMemStorage(0);
corners = (CvPoint2D32f *)malloc(sizeof(CvPoint2D32f)*9);
cvFindContours(gray, contour_storage, &contours, sizeof (CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
cvDrawContours(img, contours, CV_RGB(255,0,0), CV_RGB(0,255,0), 2, 4, 8, cvPoint(0,0));

cvNamedWindow("Calibration", CV_WINDOW_AUTOSIZE);
cvShowImage( "Calibration", img);


return 0;

i wonder how to process to image processing to perform game

This Progmram is limited to Using C Code and OpenCv 1.x to run embedded board using arm

Please let me know how to develop this game using opencv

I only used the c++ interface up to now, so I don't know about the exact names of the functions you'll have to call. Nevertheless, here's a description of a rough algorithm I would use to achieve what you want, so you have a few keywords you can look up:

  • As the camera image is clearly distorted, recognizable by the curved board lines, you should start by calibrating your camera. If you always use the same camera, or at least the same model with the same focal length, you'll only have to do this once. A quick tutorial on this can be found here. This is a more detailed tutorial, with more mathematics, unfortunately he uses the c++ functions.

  • The next step would be detecting the cells of your board. I think this can be achieved more easily by using a Hough Line Transform (also c++) and then calculating the line intersections and thus you can define your cells.

  • If you want your software to automatically take it's turn, you can use a motion detector to determine if there's currently no motion and then act. Background subtraction is an easy way to do this. I didn't find a suitable explanation on this, just do a google search.

  • After that you'll have to find circles and crosses. Circles are the easier part, as there's also a Hough Circle Transform. Crosses are more tricky. I would estimate the length of the crosses lines by taking the diagonals of your cells into account and the again use a Hough Line Detector.

Everything I mentioned here gets explained in detail (but not too theoretically) in this book, which I found very helpful when learning OpenCV. They even use the C-Interface.

HI guys,

Currently I am developing an image processing library for android, I need a book/pdf doc which contains all algorithms implemented in open Cv.

Thank you!!

There is an O'Reilly book by two of the major authors of OpenCV: Learning OpenCV: Computer Vision with the OpenCV Library by Bradski and Kaehler.

You should note that it is based on OpenCV 1.0, not the more recent 2.2.

Nonetheless, to understand algorithms it will likely be useful. For most of the higher-level algorithms like corner detection, for example, the book contains a mathematical descriptions of the implementation. Also, the authors do a decent job of providing references to academic journal articles, so even if their own description of the implementation is lacking, you will be able to use the references as a starting point.

I am Looking for book or tutorials for implementing face detection in iphone sdk? OpenCV is one but it takes more time to detect faces.

I want to learn algorithms used for face detection. Once i got a logic then it can be easily converted into programs.

Plz send me appropriate tutorials or ebook links ???

I can't find the best way to detect red color in different illumination or background.

I found that there's YCbCr color space which is good for red or blue color detection (actually I need to detect blue color too). The problem is that I can't figure out which threshold to use in different lightning. For example in sunny weather this threshold equals 210 (from 255), when in cloudly weather this threshold equals 130.

I use OpenCV library to implement this.

Thanks for any help or advice.

Yes, HSV is usually used for such purpose. In HSV you can tell that whatever is brightness etc, red is what is needed. I also recommend to look into two places. One is simple tutorial http://aishack.in/tutorials/tracking-colored-objects-in-opencv/ and another is to take a book Learning OpenCV and use examples of histograms from there. They do exactly what you need. Using HSV and Histograms makes your solution solid.

i have been given a project where i will need to write code such that it parts of images. For example the project will require me to extract a river part from the scenery or so. I have no experience in this context. Please tell me where do i start studying form. Which are good books? Which technologies will i need to learn. What are the tools that are helpful?

openCV is probably the most complete free image processing library. There is also a book which describes both the library and some image processing techniques.

This is a reasonably complex problem, not exactly graduate research but challenging!

See this question for a list of other books.

I want to create a simple program that could calculate transformation of camera based on recorded video. The idea is that I could put some picture on top of the video and transformation of the picture will be matching the image in the video.

I was looking at optical flow, but then can not figure out how to calculate transformations (translate, scale, rotation) based on the results.

What would be the best way of doing it?

It's called pose estimation - there is a tutorial on the POSIT algorithm

It's covered in the openCV book if you can get hold of a copy

The pixel value of a colored image represents the total of the Red , green , blue component effect . I want to extract the exact value for each component using opencv, Please suggest !

It's all in the OpenCV FAQ Wiki:

Suppose, we have 8-bit 3-channel image I (IplImage* img):

I(x,y)blue ~ ((uchar*)(img->imageData + img->widthStep*y))[x*3]
I(x,y)green ~ ((uchar*)(img->imageData + img->widthStep*y))[x*3+1]
I(x,y)red ~ ((uchar*)(img->imageData + img->widthStep*y))[x*3+2]

You might also want to get a copy of O'Reilly's Learning OpenCV and read it if you're planning to do any serious work with OpenCV - it will save a lot of time on very basic questions such as the above.

Within my project I'm using the camera function of the iPhone. I'd like to take a picture of a card (Rectangle shape). After I take this picture I'd like to scale and fit this card to a new image.

Can anyone point me in the right direction for making these functions? (are there any libraries or so?)

Help is greatly appreciated

Read about OpenCV. It's easy to port it on iPhone. But it isn't so easy to use it. Grab a book about OpenCV for weekend reading and on monday you'll easy solve your task.

I'm rather novice in C++ and I must realize this schoolar project :

Assume an image in a document containing both texts and images.

There should be a program written in C++ whose the goal is to load the document and extract separately texts and images in order to output it in some target destinations like UI or file.

Furthermore, if image contains any texts like legends, program should be able to extract it separately too.

Is there an existing c++ library that respond to those requirements ?

No doubt in that, use OpenCV.

But remember, you have a long way to go.

1. First of all, you should be good in C++ and object oriented programming.

Well, if you are not good, try to learn it first. Check out following link for some best resources : What are good online resources or tutorials to learn C++

2. Then get OpenCV and install

Check out OpenCV homepage to get info about downloading and installing OpenCV.

3. Now get and read some good books on OpenCV

The best book on OpenCV is "Learning OpenCV" written by Gary Bradsky, main founder of OpenCV.

Second one is "OpenCV cookbook".

These books contains lots of examples on OpenCV along with description

4. Check out OpenCV documentation.

OpenCV documentation contains details of complete functions. It also includes a lot of tutorials, which are really good for all.

5. Also try running OpenCV samples. It contains a lot of good programs

And always, Google is your best friend. Ask everything there first. Come here only when you are lost in your path.

Acquire all the above things. Then you will be really good in OpenCV and i am sure you will enjoy its power. Once you are done with these, you will get enough idea on realizing your project.( Otherwise, you will post new questions every day asking codes to realize your project, which will be useless for you. )

For your understanding, your project include advanced things like Optical Character Recognition. It is a big topic. So build yourself from basics. And it will take time.

All the best.