In this basic tutorial we go to learn some basic instructions to work in OpenCV, mouse event, trackbar controler, create image, save image and draw basic circle.

First step is create two windows, one is our canvas image, and other to color selector as we see in last tutorials.

//Create window
cvNamedWindow( "Demo", 0 );
cvNamedWindow( "ColorSelector",0);

Second step is creat the trackbars to control the color, then we need 3 trackbars, one by color.

//Create track Bar
cvCreateTrackbar("Red","ColorSelector", &red,255,&changeColor);
cvCreateTrackbar("Green","ColorSelector", &green,255,&changeColor);
cvCreateTrackbar("Blue","ColorSelector", &blue,255,&changeColor);

The trackbar function has these parameter and definition:

int cvCreateTrackbar( const char* trackbar_name, const char* window_name,
int* value, int count, CvTrackbarCallback on_change );

trackbar_name Name of created trackbar.
window_name Name of the window which will e used as a parent for created trackbar.
value Pointer to the integer variable, which value will reflect the position of the slider. Upon the creation the slider position is defined by this variable.
count Maximal position of the slider. Minimal position is always 0.
on_change Pointer to the function to be called every time the slider changes the position. This function should be prototyped as void Foo(int);Can be NULL if callback is not required.

In our tutorial we have a changeColor function asociated to set a color preview:

void changeColor(int pos){
cvSet(imgColor, CV_RGB(red,green,blue),NULL);
}

The cvSet function set all matrix values to a scalar value. The cvSet function and params are:

void cvSet( CvArr* arr, CvScalar value, const CvArr* mask=NULL );

arr The destination array.
value Fill value.
mask Operation mask, 8-bit single channel array; specifies elements of destination array to be changed.

The function cvSet copies scalar value to every selected element of the destination array:

arr(I)=value if mask(I)!=0

If array arr is of IplImage type, then is ROI used, but COI must not be set.

To get mouse events we use cvSetMouseCallback where first parameter is window name we want give events, and the second parameter the pointer to callback function, we can pass third parameter as callback optional parameters.

cvSetMouseCallback("Demo",&on_mouse, 0 );

The mouse event callback function must defined a:

void on_mouse( int event, int x, int y, int flags, void* param )

Where

event is a constant that give this values and define the mouse event:
#define CV_EVENT_MOUSEMOVE      0
#define CV_EVENT_LBUTTONDOWN    1
#define CV_EVENT_RBUTTONDOWN    2
#define CV_EVENT_MBUTTONDOWN    3
#define CV_EVENT_LBUTTONUP      4
#define CV_EVENT_RBUTTONUP      5
#define CV_EVENT_MBUTTONUP      6
#define CV_EVENT_LBUTTONDBLCLK  7
#define CV_EVENT_RBUTTONDBLCLK  8
#define CV_EVENT_MBUTTONDBLCLK  9

x, y: integer mouse position in our image

flag is another constant value:
#define CV_EVENT_FLAG_LBUTTON   1
#define CV_EVENT_FLAG_RBUTTON   2
#define CV_EVENT_FLAG_MBUTTON   4
#define CV_EVENT_FLAG_CTRLKEY   8
#define CV_EVENT_FLAG_SHIFTKEY  16
#define CV_EVENT_FLAG_ALTKEY    32