Reputation: 43
I am trying to draw with mouse move in an opencv window. But when I draw, nothing draws on the window. When I try to close the window from the cross in the topleft(ubuntu), it opens a new window which it should be as I haven't pressed escape, and in this new window, I am able to see my drawing. I can't understand the problem. I think something is wrong with window refreshing with mouse call back. Here is the code...
bool drawing = false; //true if mouse is pressed
Mat img;
void draw(int event, int x, int y, int flags, void* param){
if (event == CV_EVENT_LBUTTONDOWN){
drawing = true;
} else if (event == CV_EVENT_MOUSEMOVE){
if (drawing) circle(img,Point(x,y),4,Scalar(255,255,255),-1);
} else if (event == CV_EVENT_LBUTTONUP){
drawing = false;
circle(img,Point(x,y),2,Scalar(255,255,255),-1);
}
}
int main(){
// Create black empty images
img = Mat::zeros(window_width, window_height, CV_8UC3);
namedWindow( window_name, CV_WINDOW_AUTOSIZE ); // Create a window for display.
setMouseCallback(window_name, draw, &img); //set the callback function for any mouse event
while(true){
imshow(window_name, img);
if (waitKey(0) == 27) break;
}
destroyAllWindows();
return 0;
}
Any help,please?
Upvotes: 1
Views: 1424
Reputation: 20130
your code works for me.
But you used cv::waitKey(0)
which means that the program waits there until you press a keyboard key. So try pressing a key after drawing, or use cv::waitKey(30)
instead.
If this doesnt help you, please add some std::cout
in your callback function to verify it is really called.
btw, Mat::zeros(window_width, window_height, CV_8UC3);
probably should be Mat::zeros(window_height, window_width, CV_8UC3);
instead if you variable names shall be intuitive
Upvotes: 2