gpt4 book ai didi

Python Tkinter。数据点的最佳拟合椭圆

转载 作者:太空宇宙 更新时间:2023-11-03 14:58:00 33 4
gpt4 key购买 nike

我正在使用 Tkinter 开发 Python GUI。我试图将 BMP 图像中的四个指定点位置保存到变量中,并创建一个或多或少穿过保存点的最佳拟合椭圆。我仍然是使用 Tkinter 和 GUI 的初学者,所以请耐心等待!

到目前为止,代码能够标记点并打印出其位置/坐标。对于这种情况我应该使用 matplotlib 吗?我也可以使用 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()

这是我用于测试的示例图像的前后对比。

之前:

enter image description here

之后:

enter image description here

关于Python Tkinter。数据点的最佳拟合椭圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45360837/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com