This article is Part 2 of my OpenCV Example series. In this article, I will explain some basic things to do with OpenCV like: adding text to image, drawing shapes, handling keyboard and mouse events.
Listing 1 below shows you how to add text to image. The result is shown in Fig.1
Listing 1: add_text.c
The code above simply loads an image, setup font and add text, then displays the image. Consult OpenCV Reference for details of cvInitFont and cvPutText parameters. The result is shown in Fig.1 below.

Listing 2 below shows you how to draw some shapes. The result is shown in Figure 2.
Listing 2: shapes.c
The code above draws a line, a rectangle and a circle. It is also possible to draw more complex shapes like ellipses and pollygons. Consult the OpenCV Reference for the functions. The result image is shown below

Listing 3 shows you a simple example of keyboard input handling. While there is no button or such in OpenCV GUI,you can employ input from keyboard to control your application.
Listing 3: keybd.c
The code above loads an image and waits for user input. If the user press '1', it displays the original image. If the user press '2', it flips the image horizontally. If the user press '3', it flips the image vertically. And it quits when the user press 'q'.
Employing mouse for you OpenCV application is very useful, since you can select object, make selection, slicing image and such things.
To do this, you need to perform 2 things:
Listing 4 below shows you a simple example.
Listing 4: mouse.c
The code above loads an image and waits for mouse events. It displays a message when left and right button is pressed. It also draws a red rectangle that follows the mouse pointer when the mouse move over the image.
Learning OpenCV: Computer Vision with the OpenCV Library
By: Gary Bradski, Adrian Kaehler
This book is the "de facto" OpenCV User's Manual. It provides a practical, pragmatic, accessible book on computer vision, with algorithmic explanation and concrete example code snippets. Written by the creators of OpenCV, no doubt you should obtain a copy.
Machine Vision: Theory, Algorithms, Practicalities (Signal Processing and its Applications)
By: E. R. Davies
This book provides a solid and concrete foundation to computer vision from engineering point of view. Use Learning OpenCV from Gary Bradsky to learn how to use OpenCV, and use this book to understand how OpenCV works behind the screen.
C Programming Language (2nd Edition)
By: Brian W. Kernighan
For you who new to C programming, this is a must-have book for you. Written by the C creator himself, this book is concise and powerful, just like C itself.
Duncan Boehle on May 26, 2009:
Nash on May 27, 2009:
img1 = cvCloneImage(img0);cvCloneImage will read img0 and overwrite it to img1 repeatedly. This is true since img1 is a global variable. img[i] = cvCloneImage(img0); Huda on Jul 13, 2009:
Nash on Jul 14, 2009:
dewesh on Oct 8, 2009:
Stefan on Oct 12, 2009:
Nash on Oct 14, 2009:
larun on Jan 7, 2010:
Yang Mulia on Jan 8, 2010:
Maui on May 23, 2010:
hajar on Nov 23, 2010:
Rida Shamasneh on Nov 27, 2010:
| ICQ | 489571630 |
| Skype | dede_bl4ckheart |
| Yahoo | dede_bl4ckheart |
| nashruddin.amin |
Enrique Araiza on Feb 10, 2009: