本文主要是介绍使用cv2控制鼠标实现circle的拖拽,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
2.代码
import numpy as np
import cv2x_center = [100,200,300,400]
y_center = [200,200,200,200]
radius = 30def mouse_LButtonDown(event, x, y, flags, param):global tempif event == cv2.EVENT_LBUTTONDOWN:print(f" Down Clicked at ({x}, {y})")for i in range(len(x_center)):if (x-x_center[i])**2 + (y-y_center[i])**2 <= radius**2:temp = iprint("选中")if event == cv2.EVENT_LBUTTONUP:print(f" Up Clicked at ({x}, {y})")print(temp)x_center[temp] = xy_center[temp] = yprint('更新成功')print(x_center)print(y_center)cv2.namedWindow('mouse',cv2.WINDOW_NORMAL)
cv2.resizeWindow('mouse',640,480)
cv2.setMouseCallback('mouse', mouse_LButtonDown)color = (0, 255, 0)while True:img = np.zeros((480, 640, 3), dtype=np.uint8)for i in range(len(x_center)):cv2.circle(img, (x_center[i], y_center[i]), 30, color, -1, lineType=cv2.FILLED)cv2.imshow('mouse', img)key = cv2.waitKey(1)if key == ord('q'):breakcv2.destroyAllWindows()
这篇关于使用cv2控制鼠标实现circle的拖拽的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!