python - Draw same lines drawn on a Tkinter Canvas on an OpenCV image -
using mouse, let user draw random curves on tkinter canvas. these curves drawn short lines between points on mouse moves.
my aim save points used draw lines on canvas , draw same curves using same points on simple opencv window.
the drawing on canvas works perfectly, however, wherever place opencv window never succeed fulfill goal. think problem may in wrong functions call order?
from tkinter import * import numpy np import cv2 class test: def __init__(self): self.b1="up" self.xold=none self.yold=none self.liste=[] def test(self,obj): self.drawingarea=canvas(obj) self.drawingarea.pack() self.drawingarea.bind("<motion>",self.motion) self.drawingarea.bind("<buttonpress-1>",self.b1down) self.drawingarea.bind("<buttonrelease-1>",self.b1up) def b1down(self,event): self.b1="down" def b1up(self,event): self.b1="up" self.xold=none self.yold=none def motion(self,event): if self.b1=="down": if self.xold not none , self.yold not none: event.widget.create_line(self.xold,self.yold,event.x,event.y,fill="red",width=3,smooth=true) self.xold=event.x self.yold=event.y self.liste.append((self.xold,self.yold)) self.mc=maclasse() self.mc.dessiner_lignes() self.mc.maclasse() def get_points(self): in range(len(self.liste)): print self.liste[i] return self.liste class maclasse: def __init__(self): self.s=600,600,3 self.les_points=[]# empty list self.ma=np.zeros(self.s,dtype=np.uint8) def maclasse(self): cv2.namedwindow("opencv",cv2.window_autosize) cv2.imshow("opencv",self.ma) cv2.waitkey(0) cv2.destroyallwindows() def dessiner_lignes(self): self.voi=test() self.les_points=self.voi.get_points() # displays 0 print "number of points: {}".format(len(self.les_points)) in range(len(self.les_points)): print if i<len(self.les_points)-1: print self.les_points[i] self.first_point=self.les_points[i] self.second_point=self.les_points[i+1] cv2.line(self.ma,self.first_point,self.second_point,[255,255,255],2) if __name__=="__main__": root=tk() root.wm_title("test") v=test() v.test(root) root.mainloop() mc=maclasse() v.get_points() # points here
you shouldn't create maclasse
instance during motion event, since way create new maclasse
every time new line drawn. want create 1 maclasse
, points test
it. therefore, can separate maclasse
, test
.
you points using
root = tk() v = test() v.test(root) root.mainloop() points = v.get_points()
this sets test
app, , after points have been drawn, uses get_points()
points.
then, can set maclasse
instance, need way pass points it. sensible way (to me) seems to pass them dessiner_lignes
function, since draws lines. if alter dessiner_lignes
accepts les_points
variable (def dessiner_lignes(self, les_points=[]):
), can draw , show image using
mc = maclasse() mc.dessiner_lignes(points) mc.maclasse()
to separate separately drawn curves, can place (none, none)
"coordinate" when mouse button released (so in b1up
). in dessiner_lignes
check if both coordinates not (none, none)
before drawing line segment.
your complete code looks this. note i've removed self
les_points
, first_point
, second_point
in dessiner_lignes
since used in method, there no need save them class attributes.
from tkinter import * import numpy np import cv2 class test: def __init__(self): self.b1="up" self.xold=none self.yold=none self.liste=[] def test(self,obj): self.drawingarea=canvas(obj) self.drawingarea.pack() self.drawingarea.bind("<motion>",self.motion) self.drawingarea.bind("<buttonpress-1>",self.b1down) self.drawingarea.bind("<buttonrelease-1>",self.b1up) def b1down(self,event): self.b1="down" def b1up(self,event): self.b1="up" self.xold=none self.yold=none self.liste.append((self.xold,self.yold)) def motion(self,event): if self.b1=="down": if self.xold not none , self.yold not none: event.widget.create_line(self.xold,self.yold,event.x,event.y,fill="red",width=3,smooth=true) self.xold=event.x self.yold=event.y self.liste.append((self.xold,self.yold)) def get_points(self): #for in range(len(self.liste)): #print self.liste[i] return self.liste class maclasse: def __init__(self): self.s=600,600,3 self.ma=np.zeros(self.s,dtype=np.uint8) def maclasse(self): cv2.namedwindow("opencv",cv2.window_autosize) cv2.imshow("opencv",self.ma) cv2.waitkey(0) cv2.destroyallwindows() def dessiner_lignes(self, les_points=[]): print "number of points: {}".format(len(les_points)) in range(len(les_points)): #print if i<len(les_points)-1: #print les_points[i] first_point=les_points[i] second_point=les_points[i+1] if not first_point == (none, none) , not second_point == (none, none): cv2.line(self.ma,first_point,second_point,[255,255,255],2) if __name__=="__main__": root = tk() root.wm_title("test") v = test() v.test(root) root.mainloop() points = v.get_points() mc = maclasse() mc.dessiner_lignes(points) mc.maclasse()
Comments
Post a Comment