


Python:Īfter defining this mouse callback function, we proceed to perform the following: If you are not familiar with annotating images, please refer to this post. When actions LEFTBUTTONDOWN and LEFTBUTTONUP are triggered, we store the coordinates in the respective variables and use them to draw the rectangle. *userdata is an optional argument that can provide further inputs to the callback function (we will not require it in this example though). These are then passed to the function for processing. Whenever the user interacts with the mouse, while it is over the image being displayed: The mouse event type and event flags are recorded, along with the mouse x and y coordinates. The available list of events is documented here. As shown below, we create the logic to draw a rectangle, based on certain mouse events.
ALTERNATIVE TO OPENCV FOR MAC HOW TO
Let’s understand how to associate this function with specific mouse events. You need not specify any input arguments to the function, these will be populated automatically when the user interacts with the mouse. This works because we are going to register this function name to be associated with mouse events. In this case, we have named it drawRectangle(). You can however name the function whatever you like. Here, this particular function is associated with mouse events, so we define it as a MouseCallback function. Called only when certain user interface events are detected, this type of functions came to be known as the ‘Callback’ functions. Points to store the center of the circle and a point on the circumferenceīegin by defining a special ‘Callback’ function that will draw a rectangle on the image displayed in the named window. If c is pressed, clear the window, using the dummy image SetMouseCallback("Window", drawRectangle) highgui function called when mouse events occur Make a temporary image, which will be used to clear the image Rectangle(image, top_left_corner, bottom_right_corner, Scalar(0,255,0), 2, 8 ) When left mouse button is released, mark bottom right corner Mark the top left corner when left mouse button is pressed Void drawRectangle(int action, int x, int y, int flags, void *userdata) function which will be called on mouse input Point top_left_corner, bottom_right_corner Points to store the bounding box coordinates Using namespace to nullify use of cv::function() syntax # If c is pressed, clear the window, using the dummy image # highgui function called when mouse events occurĬv2.setMouseCallback("Window", drawRectangle)

# Make a temporary image, will be useful to clear the drawing # When left mouse button is released, mark bottom right cornerĬv2.rectangle(image, top_left_corner, bottom_right_corner, (0,255,0),2, 8) # Mark the top left corner when left mouse button is pressed Global top_left_corner, bottom_right_corner # function which will be called on mouse inputĭef drawRectangle(action, x, y, flags, *userdata): # Lists to store the bounding box coordinates
