user interface - Python Tkinter. Best Fit Ellipse from Data Points -
i working on python gui w/ tkinter. trying save 4 specified point locations bmp image variables, , create best-fit ellipse more or less passes through saved points. still beginner working w/ tkinter , gui's please bear w/ me!
so far, code able mark points , print out position/coordinates. should use matplotlib kind of situation? able use w/ tkinter well?
here code:
from tkinter import * pil import image, imagetk class window(frame): def __init__(self, master=none): frame.__init__(self, master) self.master = master self.pos = [] self.master.title("gui") self.pack(fill=both, expand=1) self.counter = 0 menu = menu(self.master) self.master.config(menu=menu) file = menu(menu) file.add_command(label="exit", command=self.client_exit) menu.add_cascade(label="file", menu=file) analyze = menu(menu) analyze.add_command(label="region of interest",command=self.regionofinterest) analyze.add_command(label="erase", command=self.erasepoints) menu.add_cascade(label="analyze", menu=analyze) load = image.open("ap41.ddr.brf.sdat.bmp") render = imagetk.photoimage(load) img = label(self, image=render) img.image = render img.place(x=0, y=0) def regionofinterest(self): root.config(cursor="plus") canvas.bind("<button-1>", self.imgclick) def erasepoints(self): self.pos = [] def client_exit(self): exit() def imgclick(self, event): if self.counter < 4: x = canvas.canvasx(event.x) y = canvas.canvasy(event.y) self.pos.append((x, y)) print(self.pos) canvas.create_line(x - 5, y, x + 5, y, fill="red", tags="crosshair") canvas.create_line(x, y - 5, x, y + 5, fill="red", tags="crosshair") self.counter += 1 else: canvas.unbind("<button 1>") root.config(cursor="arrow") self.counter = 0 root = tk() imgsize = image.open("ap41.ddr.brf.sdat.bmp") tkimage = imagetk.photoimage(imgsize) w, h = imgsize.size canvas = canvas(root, width=w, height=h) canvas.create_image((w/2,h/2),image=tkimage) canvas.pack() root.geometry("%dx%d"%(w,h)) app = window(root) root.mainloop()
here can play , fine tune think close trying do.
first created anther menu item labeled create ellipse
links method work out top left cords , bottom right cords , uses create_ovel()
command create ellipse on screen. let me know if close want do.
the new method below compare values of each tuple base tuple , if numbers lower change top left cords , if numbers high change bottom right cords. 2 sets of cords figured out create ellipse fit selected.
def createellipse(self): top_left_cords = self.pos[0] bottom_right_cords = self.pos[0] pos in self.pos: if pos[0] < top_left_cords[0]: top_left_cords = (pos[0], top_left_cords[1]) if pos[1] < top_left_cords[1]: top_left_cords = (top_left_cords[0], pos[1]) if pos[0] > bottom_right_cords[0]: bottom_right_cords = (pos[0], bottom_right_cords[1]) if pos[1] > bottom_right_cords[1]: bottom_right_cords = (bottom_right_cords[0], pos[1])
below full code:
from tkinter import * pil import image, imagetk class window(frame): def __init__(self, master=none): frame.__init__(self, master) self.master = master self.pos = [] self.master.title("gui") self.pack(fill=both, expand=1) self.counter = 0 menu = menu(self.master) self.master.config(menu=menu) file = menu(menu) file.add_command(label="exit", command=self.client_exit) menu.add_cascade(label="file", menu=file) analyze = menu(menu) analyze.add_command(label="region of interest",command=self.regionofinterest) analyze.add_command(label="erase", command=self.erasepoints) analyze.add_command(label="create ellipse", command=self.createellipse) menu.add_cascade(label="analyze", menu=analyze) load = image.open("./colors/1.png") render = imagetk.photoimage(load) img = label(self, image=render) img.image = render img.place(x=0, y=0) def createellipse(self): top_left_cords = self.pos[0] bottom_right_cords = self.pos[0] pos in self.pos: if pos[0] < top_left_cords[0]: top_left_cords = (pos[0], top_left_cords[1]) if pos[1] < top_left_cords[1]: top_left_cords = (top_left_cords[0], pos[1]) if pos[0] > bottom_right_cords[0]: bottom_right_cords = (pos[0], bottom_right_cords[1]) if pos[1] > bottom_right_cords[1]: bottom_right_cords = (bottom_right_cords[0], pos[1]) print(top_left_cords, bottom_right_cords) canvas.create_oval(top_left_cords, bottom_right_cords) def regionofinterest(self): root.config(cursor="plus") canvas.bind("<button-1>", self.imgclick) def erasepoints(self): self.pos = [] def client_exit(self): exit() def imgclick(self, event): if self.counter < 4: x = canvas.canvasx(event.x) y = canvas.canvasy(event.y) self.pos.append((x, y)) print(self.pos) canvas.create_line(x - 5, y, x + 5, y, fill="red", tags="crosshair") canvas.create_line(x, y - 5, x, y + 5, fill="red", tags="crosshair") self.counter += 1 else: canvas.unbind("<button 1>") root.config(cursor="arrow") self.counter = 0 root = tk() imgsize = image.open("./colors/1.png") tkimage = imagetk.photoimage(imgsize) w, h = imgsize.size canvas = canvas(root, width=w, height=h) canvas.create_image((w/2,h/2),image=tkimage) canvas.pack() root.geometry("%dx%d"%(w,h)) app = window(root) root.mainloop()
here before , after sample image have testing.
before:
after:
Comments
Post a Comment