2022年 11月 9日

python鼠标点击事件

 一、画矩形框

  1. import cv2
  2.  
  3. global img
  4. global point1, point2
  5. def on_mouse(event, x, y, flags, param):
  6.     global img, point1, point2
  7.     img2 = img.copy()
  8.     if event == cv2.EVENT_LBUTTONDOWN:         #左键点击
  9.         point1 = (x,y)
  10.         cv2.circle(img2, point1, 10, (0,255,0), 5)
  11.         cv2.imshow('image', img2)
  12.     elif event == cv2.EVENT_MOUSEMOVE and (flags & cv2.EVENT_FLAG_LBUTTON):               #按住左键拖曳
  13.         cv2.rectangle(img2, point1, (x,y), (255,0,0), 5)
  14.         cv2.imshow('image', img2)
  15.     elif event == cv2.EVENT_LBUTTONUP:         #左键释放
  16.         point2 = (x,y)
  17.         cv2.rectangle(img2, point1, point2, (0,0,255), 5
  18.         cv2.imshow('image', img2)
  19.         min_x = min(point1[0],point2[0])     
  20.         min_y = min(point1[1],point2[1])
  21.         width = abs(point1[0] - point2[0])
  22.         height = abs(point1[1] -point2[1])
  23.         cut_img = img[min_y:min_y+height, min_x:min_x+width]
  24.         cv2.imwrite('lena3.jpg', cut_img)
  25.  
  26. def main():
  27.     global img
  28.     img = cv2.imread('lena.jpg')
  29.     cv2.namedWindow('image')
  30.     cv2.setMouseCallback('image', on_mouse)
  31.     cv2.imshow('image', img)
  32.     cv2.waitKey(0)
  33.  
  34. if __name__ == '__main__':
  35.     main()

二、画线

  1. import os
  2. import cv2
  3. import math
  4. global img
  5. global point1, point2
  6. def calculate_distance(pt1, pt2):
  7. x1, y1 = pt1
  8. x2, y2 = pt2
  9. dist = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
  10. return dist
  11. def on_mouse(event, x, y, flags, param):
  12. global img, point1, point2
  13. img2 = img.copy()
  14. if event == cv2.EVENT_LBUTTONDOWN: # 左键点击
  15. point1 = (x, y)
  16. cv2.circle(img2, point1, 10, (0, 255, 0), 2)
  17. cv2.imshow('image', img2)
  18. elif event == cv2.EVENT_MOUSEMOVE and (flags & cv2.EVENT_FLAG_LBUTTON): # 按住左键拖曳
  19. cv2.line(img2, point1, (x, y), (255, 0, 0), 2)
  20. cv2.imshow('image', img2)
  21. elif event == cv2.EVENT_LBUTTONUP: # 左键释放
  22. point2 = (x, y)
  23. cv2.line(img2, point1, point2, (0, 0, 255), 2)
  24. cv2.imshow('image', img2)
  25. distance = calculate_distance(point1, point2)
  26. font = cv2.FONT_HERSHEY_SIMPLEX
  27. cv2.putText(img2, f'{int(distance)}', (point2[0]+2, point2[1]+2), font, 1.2, (255, 255, 255), 2)
  28. cv2.imshow('image', img2)
  29. img = img2
  30. def main():
  31. global img, base_name
  32. img_path = r"D:\work\data\ellipse_fitting\ori_resize\label_viz0.png"
  33. img_name = os.path.split(img_path)[-1]
  34. base_name = os.path.splitext(img_name)[0]
  35. save_name = base_name + '_gt.jpg'
  36. save_dir = r'D:\work\data\ellipse_fitting\gt_axis'
  37. os.makedirs(save_dir, exist_ok=True)
  38. img = cv2.imread(img_path)
  39. cv2.namedWindow('image')
  40. cv2.setMouseCallback('image', on_mouse)
  41. cv2.imshow('image', img)
  42. if cv2.waitKey(0) & 0xFF == ord('s'):
  43. cv2.imwrite(os.path.join(save_dir, save_name), img)
  44. print('save img:{}'.format(os.path.join(save_dir, save_name)))
  45. if __name__ == '__main__':
  46. main()