The steps for this recipe are as follows:
- First, load an image and make its copy:
import cv2, numpy as npimage = cv2.imread('../data/Lena.png')image_to_show = np.copy(image)
- Now, define some variables to store the mouse state:
mouse_pressed = Falses_x = s_y = e_x = e_y = -1
- Let's implement a handler for mouse events. This should be a function that takes four arguments, as follows:
def mouse_callback(event, x, y, flags, param): global image_to_show, s_x, s_y, e_x, e_y, mouse_pressed if event == cv2.EVENT_LBUTTONDOWN: mouse_pressed = True s_x, s_y = x, y image_to_show = np.copy(image) elif event == cv2.EVENT_MOUSEMOVE: if mouse_pressed: image_to_show = np.copy(image) cv2.rectangle(image_to_show, (s_x, s_y), (x, y), (0, ...