这是我的代码:
import numpy as np
import cv2
drawing = False
def draw_circle(event,x,y,flags,param):
global ix,iy,drawing
if event == cv2.EVENT_LBUTTONDOWN:
drawing= True
ix,iy=x,y
elif event == cv2.EVENT_MOUSEMOVE and flags == cv2.EVENT_FLAG_LBUTTON:
if drawing == True:
cv2.circle(img,(x,y),3,(0,0,0),-1)
elif event == cv2.EVENT_LBUTTONUP:
drawing == False
img=np.ones((512,512,3),np.uint8)*255
cv2.namedWindow('image')
cv2.setMouseCallback('image',draw_circle)
while(True):
cv2.imshow('image',img)
if cv2.waitKey(1)&0xff == ord('q'):
break
cv2.destroyAllWindows()
第一条线画得慢,第二条线画得快:
我的问题是:
- 是什么导致了这个问题?
- 如何解决。
不要使用circle
,当你使用circle
时,在快速移动时会有间隙。会有间隙,因为当您快速移动时,在绘制前一个圆圈之前会引发新的鼠标事件。使用 line
代替如下:
def draw_circle(event,x,y,flags,param):
global ix,iy,drawing
if event == cv2.EVENT_LBUTTONDOWN:
drawing= True
ix,iy=x,y
elif event == cv2.EVENT_MOUSEMOVE and flags == cv2.EVENT_FLAG_LBUTTON:
if drawing:
cv2.line(img,(ix,iy),(x,y),(0,0,0), 3)
ix,iy = x,y
elif event == cv2.EVENT_LBUTTONUP:
drawing == False
我是一名优秀的程序员,十分优秀!