Using mouse to paint an area of video with OpenCV
Hi!
I want to paint an area of my video using mouse when the video is paused, but I'm having a little problem. When I pause the video and try to paint a region, the painted area only appears after I press any key on my keyboard (which is not implemented it). I wonder what I could do to the painted area appears when I press the mouse button?
Thank you, : )
My code:
//mouse callback
void rotina_mouse(int event, int x, int y, int flags, void* param);
bool continua = false;
//function to paint
void pinta(IplImage* image, int x, int y){
cvFloodFill (image, cvPoint (x,y), cvScalar(103), cvScalarAll(2), cvScalarAll(2), 0, CV_FLOODFILL_FIXED_RANGE , 0);
}
//main program
int _tmain(int argc, _TCHAR* argv[])
{
cvNamedWindow ("saida", CV_WINDOW_AUTOSIZE);
CvCapture* g_capture = cvCreateFileCapture ("vid.avi");
IplImage* frame = cvQueryFrame(g_capture);
IplImage* temp = cvCloneImage( frame );
cvSetMouseCallback("saida",rotina_mouse,(void*) frame);
while(1){
frame = cvQueryFrame(g_capture);
cvNot(frame, frame);
cvCopyImage( frame, temp );
cvShowImage("saida", temp);
if(!frame) break;
//pause with 'p'
char e = cvWaitKey(33);
if(e==112){
while(1){
cvCopyImage( frame, temp );
cvShowImage("saida", temp);
char d = cvWaitKey(0);
if(d==112) break;
}
}
//close video with'esc'
if(e==27) break;
}
cvReleaseCapture (&g_capture);
cvDestroyWindow("saida");
return 0;
}
//mouse callback
void rotina_mouse(int event, int x, int y, int flags, void* param) {
IplImage* image = (IplImage*) param;
switch( event ) {
case CV_EVENT_MOUSEMOVE: {
if(continua==true)
pinta(image, x, y);
}
break;
case CV_EVENT_LBUTTONDOWN: {
pinta(image, x, y);
continua=true;
}
break;
case CV_EVENT_LBUTTONUP: {
continua=false;
}
break;
default:
break;
}
}
Use C++ interface and the Mat type instead of IplImage and your problem will disappear. What is going wrong is that you do not refresh your image during the mouse callback. You need to make a local copy of the image, then do an imshow in the same window of that local copy, and paste all adaptations after you leave the onmouse callback to the original image.