How to do it...

The steps for this recipe are as follows:

  1. First, load an image and make its copy:
import cv2, numpy as np

image = cv2.imread('../data/Lena.png')
image_to_show = np.copy(image)
  1. Now, define some variables to store the mouse state:
mouse_pressed = False
s_x = s_y = e_x = e_y = -1
  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, 255, 0), 1)

elif event == cv2.EVENT_LBUTTONUP:
mouse_pressed = False
e_x, e_y = x, y
  1. Let's create the window instance that will be capturing mouse events and translating them into the handler function we defined earlier:
cv2.namedWindow('image')
cv2.setMouseCallback('image', mouse_callback)
  1. Now, let's implement the remaining part of our application, which should be reacting to buttons pushes and cropping the original image:
while True:
cv2.imshow('image', image_to_show)
k = cv2.waitKey(1)

if k == ord('c'):
if s_y > e_y:
s_y, e_y = e_y, s_y
if s_x > e_x:
s_x, e_x = e_x, s_x

if e_y - s_y > 1 and e_x - s_x > 0:
image = image[s_y:e_y, s_x:e_x]
image_to_show = np.copy(image)
elif k == 27:
break

cv2.destroyAllWindows()