Python Tkinter。来自数据点的最佳拟合椭圆
我正在使用Python GUI w/Tkinter。我试图将BMP图像中的四个指定点位置保存为变量,并创建一个最适合的椭圆,或多或少地通过保存的点。我仍然是一名初学者,工作着W/Tkinter和GUI,所以请耐心等待!Python Tkinter。来自数据点的最佳拟合椭圆
到目前为止,代码可以标记点并打印出其位置/坐标。我应该在这种情况下使用matplotlib吗?我能够使用那个w/tkinter吗?
这里是我的代码:
from tkinter import *
from 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()
这里是你可以用和微调玩,但我认为这将是接近你正在尝试做的。
首先,我创建了另一个标记为Create Ellipse
的菜单项,它链接到计算左上角帘线和右下角帘线的方法,然后将其与create_ovel()
命令配合使用以在屏幕上创建椭圆。让我知道这是否接近你想要做的事情。
下面的新方法会将每个元组的值与基元组进行比较,如果数值较低,它将更改左上角的连线,如果数字较高,则会更改右下角的连线。用这两组绳索计算出来后,它将创建一个椭圆,以便粗略地适合你所选择的。
def createEllipse(self):
top_left_cords = self.pos[0]
bottom_right_cords = self.pos[0]
for 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])
以下是完整代码:
from tkinter import *
from 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]
for 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()
以下是我对测试样本图像的之前和之后。
前:
后:
你好!对于迟到的回复感到抱歉。感谢您的代码的详细运行!不幸的是,当我尝试运行椭圆时,椭圆不会显示在带有标记点的bmp图像上,但它仍然会打印出top_left_cords&bottom_right_cords的值,所以我确实相信它可行!我有一种感觉,它必须做的帆布或许。 – FLCL
@FLCL嗡嗡声。那么它对我有用。我只是将图像更改为我可以测试的东西。我在测试的一侧添加了一张前后图像。 –
@FLCL您是否使用新的菜单按钮来创建椭圆? –
您好再次FLCL。你有没有尝试过使用'create_oval()' –
我刚刚更新了我的答案。我有一个需要修复的错误。 –
4分是不足以定义一个独特的椭圆,5是最小的 – f5r5e5d