Basic Painter in OpenCV

Nov 7, 2008 by     18 Comments    Posted under: OpenCV, Tutorials

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

Demo Source

18 Comments + Add Comment

  • Thanks! Nice post.

  • Спасибо за пост! Добавил блог в RSS-ридер, теперь читать буду регулярно..

  • Thanks for your comment HemaSwemo, I traduce your comment in english: “Thanks for the post! Added blog RSS-reader, will now read regularly ..”

  • [...] do it, we go to use all we learn in before tutorials, we go to use a simple basic painter and the basic pattern recognition and classification with openCV [...]

  • Nice work!!

  • nice work, it is great.

  • Thanks a ton! really nicely put..

  • thank u man go ahead

  • Hi thanks for your code it is very helpful for understanding mouse interactions.

    I have one quick fix for a memory leak in the code follows:

    The two line that use cvCloneImage need to be substitute with cvCopyImage as follows.

    –screenBuffer = cvCloneImage(imagen);
    ++cvCopyImage(imagen,screenBuffer);

  • Hello,

    My question is how can i load a bmp image and modify, with that track bars, the R G and B values? i wanna modify the RGB values but on a static image, and see the effects in the window

    Thanks

  • Hi, you can load image with this function

    IplImage* cvLoadImage( const char* filename, int iscolor CV_DEFAULT(1));

    and modify tutorial code change line 103 to load image and modify or delete if you want the 119 line

    Regards.

  • Im using visual Studio 2010.
    Please help

    Getting this error
    1>—— Build started: Project: painter, Configuration: Debug Win32 ——
    1> painter.cpp
    1>c:\opencv2.2\mytest\painter\painter\painter\painter.cpp(6): warning C4627: ‘#include “cv.h”‘: skipped when looking for precompiled header use
    1> Add directive to ‘StdAfx.h’ or rebuild precompiled header
    1>c:\opencv2.2\mytest\painter\painter\painter\painter.cpp(7): warning C4627: ‘#include “highgui.h”‘: skipped when looking for precompiled header use
    1> Add directive to ‘StdAfx.h’ or rebuild precompiled header
    1>c:\opencv2.2\mytest\painter\painter\painter\painter.cpp(9): warning C4627: ‘#include ‘: skipped when looking for precompiled header use
    1> Add directive to ‘StdAfx.h’ or rebuild precompiled header
    1>c:\opencv2.2\mytest\painter\painter\painter\painter.cpp(161): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add ‘#include “StdAfx.h”‘ to your source?

  • The code is not performed to VStudio, but take me some time to create CMake project generator to you can build for VStudio.

  • Do we have option for mouse scroll on cvSetMouseCallback(…). If no, can you please provide an alternative to this in Opencv 1.0

  • There are no option for scroll with opencv mouse callback, you can use your own ui, MFC, .NET, X11, QT, … or use a combination of key + mouse click, for example CTRL+MouseLeft.

    Regards.

  • nice :)

  • Can you please explain cvSetMouseCallback function? How is the function defined. Great work, by the way!! :P

  • Review the official doc: http://docs.opencv.org/modules/highgui/doc/user_interface.html?highlight=cvsetmousecallback#void cvSetMouseCallback(const char* window_name, CvMouseCallback on_mouse, void* param)

Got anything to say? Go ahead and leave a comment!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Category

Polls

How Is My Site?

View Results

Loading ... Loading ...

Twitter: damiles3D

  • Could not connect to Twitter