Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

draw with mouse continuously

I am trying to draw following the movements of the mouse. It works ok.

When I move the mouse slowly, what is drawn looks continuous, but when I speed a little bit the mouse movement, I get discontinuous drawings.

How can I draw with the mouse continusouly ?

import cv2
import numpy as np 

drawing=False # true if mouse is pressed
mode=True # if True, draw rectangle. Press 'm' to toggle to curve

# mouse callback function
def interactive_drawing(event,x,y,flags,param):
    global ix,iy,drawing, mode

    if event==cv2.EVENT_LBUTTONDOWN:
        drawing=True
        ix,iy=x,y

    elif event==cv2.EVENT_MOUSEMOVE:
        if drawing==True:
            if mode==True:
                cv2.circle(img,(x,y),1,(0,0,255),-1)
                print x,y
    elif event==cv2.EVENT_LBUTTONUP:
        drawing=False
        if mode==True:
            cv2.circle(img,(x,y),1,(0,0,255),-1)
            #print x,y
            #cv2.line(img,(x,y),(x,y),(0,0,255),10)
    return x,y



#img = np.zeros((512,512,3), np.uint8)
img_file='photo.png'
img=cv2.imread(img_file)
cv2.namedWindow('begueradj')
cv2.setMouseCallback('begueradj',interactive_drawing)
while(1):
    cv2.imshow('begueradje',img)
    k=cv2.waitKey(1)&0xFF
    if k==27:
        break
    elif k==ord('s'):
        cv2.imwrite('result.jpg',img)
        break
cv2.destroyAllWindows()

draw with mouse continuously

I am trying to draw following the movements of the mouse. It works ok.

When I move the mouse slowly, what is drawn looks continuous, but when I speed a little bit the mouse movement, I get discontinuous drawings.

How can I draw with the mouse continusouly ?

import cv2
import numpy as np 

drawing=False # true if mouse is pressed
mode=True # if True, draw rectangle. Press 'm' to toggle to curve

# mouse callback function
def interactive_drawing(event,x,y,flags,param):
    global ix,iy,drawing, mode

    if event==cv2.EVENT_LBUTTONDOWN:
        drawing=True
        ix,iy=x,y

    elif event==cv2.EVENT_MOUSEMOVE:
        if drawing==True:
            if mode==True:
                cv2.circle(img,(x,y),1,(0,0,255),-1)
                print x,y
    elif event==cv2.EVENT_LBUTTONUP:
        drawing=False
        if mode==True:
            cv2.circle(img,(x,y),1,(0,0,255),-1)
            #print x,y
            #cv2.line(img,(x,y),(x,y),(0,0,255),10)
    return x,y



#img 
img = np.zeros((512,512,3), np.uint8)
img_file='photo.png'
img=cv2.imread(img_file)
 cv2.namedWindow('begueradj')
cv2.setMouseCallback('begueradj',interactive_drawing)
while(1):
    cv2.imshow('begueradje',img)
    k=cv2.waitKey(1)&0xFF
    if k==27:
        break
    elif k==ord('s'):
        cv2.imwrite('result.jpg',img)
        break
cv2.destroyAllWindows()