Ask Your Question
0

draw with mouse continuously

asked 2015-03-08 10:31:26 -0500

begueradj gravatar image

updated 2015-03-08 11:17:05 -0500

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)

cv2.namedWindow('begueradj')
cv2.setMouseCallback('begueradj',interactive_drawing)
while(1):
    cv2.imshow('begueradje',img)
    k=cv2.waitKey(1)&0xFF
    if k==27:
        break
cv2.destroyAllWindows()
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
4

answered 2015-03-09 08:19:26 -0500

kbarni gravatar image

First, don't draw circles, it's a very slow operation. Use the img.at<...>(i,j) (or its python equivalent) to set a pixel directly.

To draw continuously, just draw a line between the last and the actual position (replace the circle functions with the following code):

cv2.line(img,(ix,iy),(x,y),(0,0,255),10)
ix=x
iy=y
edit flag offensive delete link more

Comments

i wish i can give you a nice dinner smart Man !!!!!!!!! I really asked this question in different ways with different codes in lot of forums with different usernames for almost 3 months and did not get an answer . Thank you so much , you saved me from lot of troubles.

begueradj gravatar imagebegueradj ( 2015-03-09 08:36:34 -0500 )edit

Glad I could help you :)

kbarni gravatar imagekbarni ( 2015-03-09 08:51:53 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2015-03-08 10:31:26 -0500

Seen: 2,308 times

Last updated: Mar 09 '15