def make_mouse_callback(imgs, ref_pt):
# initialize the list of reference points and boolean indicating
# whether cropping is being performed or not
cropping = [False]
clone = imgs[0]
def _click_and_crop(event, x, y, flags, param):
# grab references to the global variables
# global ref_pt, cropping
# if the left mouse button was clicked, record the starting
# (x, y) coordinates and indicate that cropping is being
# performed
if event == cv2.EVENT_LBUTTONDOWN:
ref_pt[0] = (x, y)
cropping[0] = True
# check to see if the left mouse button was released
elif event == cv2.EVENT_LBUTTONUP:
# record the ending (x, y) coordinates and indicate that
# the cropping operation is finished
ref_pt[1] = (x, y)
cropping[0] = False
# draw a rectangle around the region of interest
imgs[1] = image = clone.copy()
cv2.rectangle(image, ref_pt[0], ref_pt[1], (0, 255, 0), 2)
cv2.imshow("image", image)
elif event == cv2.EVENT_MOUSEMOVE and cropping[0]:
img2 = clone.copy()
cv2.rectangle(img2, ref_pt[0], (x, y), (0, 255, 0), 2)
imgs[1] = image = img2
cv2.imshow("image", image)
return _click_and_crop
评论列表
文章目录