Powered by Blogger.

Friday, November 7, 2008

Tag: , , , , , ,

Basic Painter in OpenCV

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

About David Millán Escrivá

David completed his studies in Universidad Politecnica de Valencia in IT with a Master's degree in artificial intelligence, computer graphics, and pattern recognition, focusing on pattern recognition and Computer Vision. David has more than 15 years of experience in IT, with more than ten years of experience in Computer Vision, computer graphics, and pattern recognition, working on different projects and start-ups, applying his knowledge of Computer Vision, optical character recognition, and augmented reality. Co-Author of two OpenCV books and reviewer of few more.

25 comments:

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

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

    ReplyDelete
  3. [...] 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 [...]

    ReplyDelete
  4. Thanks a ton! really nicely put..

    ReplyDelete
  5. thank u man go ahead

    ReplyDelete
  6. 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);

    ReplyDelete
  7. 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

    ReplyDelete
  8. 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.

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

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

    ReplyDelete
  11. 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.

    ReplyDelete
  12. Thanks for making such a killer blog. I arrive on here all the time and am floored with the fresh information here! You are great!
    Harry // pigeon racing

    ReplyDelete
  13. Thank you so much for your share, Damiles. Owing to you, I have found mouse events list of open cv.
    In deed, My problem is not just mouse list, I also have problem with finding a comprehensive material which clearly indicates what open cv functions, members, constants, features etc. Can you help me or direct meany source? Thank you again so much!!!

    ReplyDelete
  14. Нашел в блоге много полезно. Автор молодец!

    ReplyDelete
  15. Nice post, but I can't download source code. Can you upload again ? Thanks you so much.

    ReplyDelete
  16. could you please re-upload the demo source, because the link is broken. thank you

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

    ReplyDelete
  18. 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)

    ReplyDelete
  19. Hi,very useful article..but i cant able to see the demo source code,It is telling "The requested document was not found on this server".....
    Could you please retain it.

    ReplyDelete
  20. Hello,
    The topic and work seems very interesting! I would like to study your code, but unfortunately the demo source link is broken. Could be so nice and repair this? I will be very grateful. Thank you in advance.

    ReplyDelete
  21. Hello dear, your project is really nice. I could not download demo project. May be file not found. Can you upload it again or send it to my email dejan.gope23@gmail.com.? Thanks in advanced.

    ReplyDelete