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.
1. Adding Text to Image
Listing 1 below shows you how to add text to image. The result is shown in Fig.1
Listing 1: add_text.c
#include <stdio.h>
#include "cv.h"
#include "highgui.h"
int main(int argc, char** argv)
{
IplImage *img = cvLoadImage("2.jpg", CV_LOAD_IMAGE_COLOR);
/* initialize font and add text */
CvFont font;
cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 1.0, 1.0, 0, 1, CV_AA);
cvPutText(img, "Hello World!", cvPoint(10, 130), &font, cvScalar(255, 255, 255, 0));
/* display the image */
cvNamedWindow("image", CV_WINDOW_AUTOSIZE);
cvShowImage("image", img);
cvWaitKey(0);
cvDestroyWindow("image");
cvReleaseImage( &img );
return 0;
}
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.
2. Drawing Shapes
Listing 2 below shows you how to draw some shapes. The result is shown in Figure 2.
Listing 2: shapes.c
#include <stdio.h>
#include "cv.h"
#include "highgui.h"
int main(int argc, char** argv)
{
/* create an image */
IplImage *img = cvCreateImage(cvSize(200, 100), IPL_DEPTH_8U, 3);
/* draw a green line */
cvLine(img, /* the dest image */
cvPoint(10, 10), /* start point */
cvPoint(150, 80), /* end point */
cvScalar(0, 255, 0, 0), /* the color; green */
1, 8, 0); /* thickness, line type, shift */
/* draw a blue box */
cvRectangle(img, /* the dest image */
cvPoint(20, 15), /* top left point */
cvPoint(100, 70), /* bottom right point */
cvScalar(255, 0, 0, 0), /* the color; blue */
1, 8, 0); /* thickness, line type, shift */
/* draw a red circle */
cvCircle(img, /* the dest image */
cvPoint(110, 60), 35, /* center point and radius */
cvScalar(0, 0, 255, 0), /* the color; red */
1, 8, 0); /* thickness, line type, shift */
/* display the image */
cvNamedWindow("img", CV_WINDOW_AUTOSIZE);
cvShowImage("img", img);
cvWaitKey(0);
cvDestroyWindow("img");
cvReleaseImage(&img);
return 0;
}
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
3. Handling Keyboard Input
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
#include <stdio.h>
#include "cv.h"
#include "highgui.h"
int main(int argc, char** argv)
{
IplImage *img0, *img1;
int key;
/* load an image */
img0 = cvLoadImage("2.jpg", CV_LOAD_IMAGE_COLOR);
/* create a copy */
img1 = cvCloneImage(img0);
/* display original image */
cvNamedWindow("image", CV_WINDOW_AUTOSIZE);
cvShowImage("image", img0);
while(1) {
/* wait for keyboard input */
key = cvWaitKey(0);
/* 'q' pressed, quit the program */
if (key == 'q') break;
switch(key) {
/* '1' pressed, display the original image */
case '1':
cvShowImage("image", img0);
break;
/* '2' pressed, flip the image horizontally */
case '2':
cvFlip(img1, NULL, 1);
cvShowImage("image", img1);
break;
/* '3' pressed, flip the image vertically */
case '3':
cvFlip(img1, NULL, 0);
cvShowImage("image", img1);
break;
}
}
/* free memory */
cvDestroyWindow("image");
cvReleaseImage(&img0);
cvReleaseImage(&img1);
return 0;
}
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'.
4. Handling Mouse Events
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:
- Write a mouse handler function
- Register the function to a specified window
Listing 4 below shows you a simple example.
Listing 4: mouse.c
#include <stdio.h>
#include "cv.h"
#include "highgui.h"
IplImage *img0, *img1;
void mouseHandler(int event, int x, int y, int flags, void *param)
{
switch(event) {
/* left button down */
case CV_EVENT_LBUTTONDOWN:
fprintf(stdout, "Left button down (%d, %d).\n", x, y);
break;
/* right button down */
case CV_EVENT_RBUTTONDOWN:
fprintf(stdout, "Right button down (%d, %d).\n", x, y);
break;
/* mouse move */
case CV_EVENT_MOUSEMOVE:
/* draw a rectangle */
img1 = cvCloneImage(img0);
cvRectangle(img1,
cvPoint(x - 15, y - 15),
cvPoint(x + 15, y + 15),
cvScalar(0, 0, 255, 0), 2, 8, 0);
cvShowImage("image", img1);
break;
}
}
int main(int argc, char** argv)
{
/* load an image */
img0 = cvLoadImage("2.jpg", CV_LOAD_IMAGE_COLOR);
/* create new window and register mouse handler */
cvNamedWindow("image", CV_WINDOW_AUTOSIZE);
cvSetMouseCallback( "image", mouseHandler, NULL );
/* display the image */
cvShowImage("image", img0);
cvWaitKey(0);
cvDestroyWindow("image");
cvReleaseImage(&img0);
cvReleaseImage(&img1);
return 0;
}
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.
Related Articles
The Downloads
Enrique Araiza on Feb 10, 2009: