2.代码
import numpy as np
import cv2
x_center = [100,200,300,400]
y_center = [200,200,200,200]
radius = 30
def mouse_LButtonDown(event, x, y, flags, param):
global temp
if 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 = i
print("选中")
if event == cv2.EVENT_LBUTTONUP:
print(f" Up Clicked at ({x}, {y})")
print(temp)
x_center[temp] = x
y_center[temp] = y
print('更新成功')
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'):
break
cv2.destroyAllWindows()
本站资源均来自互联网,仅供研究学习,禁止违法使用和商用,产生法律纠纷本站概不负责!如果侵犯了您的权益请与我们联系!
转载请注明出处: 免费源码网-免费的源码资源网站 » 使用cv2控制鼠标实现circle的拖拽
发表评论 取消回复