如何用python

时间:2016-05-25 11:21:30

标签: python opencv

这是我的代码:

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()

第一条线被慢慢拉出,第二条线被拉得很快:

the first line was drawn slowly, the 2nd one was drawn fast

我的问题是:

  1. 导致这个问题的是什么?
  2. 如何解决。

1 个答案:

答案 0 :(得分:1)

当你使用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