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

时间:2017-07-27 20:54:51

标签: python user-interface tkinter bmp

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

到目前为止,代码能够标记点并打印出它的位置/坐标。我应该在这种情况下使用matplotlib吗?我能够使用w / tkinter吗?

这是我的代码:

private string originalName;
public void cc()
{
    cbBname.Items.Clear();

    SqlCommand cmd = new SqlCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "select * from BkhurData";
    db.ExeNonQuery(cmd);

    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(dt);
    foreach (DataRow dr in dt.Rows)
    {
        cbBname.Items.Add(dr["Name"].ToString());
        originalName =  dr["Name"].ToString()
    }
}

private void BkhurUpdate_Click(object sender, EventArgs e)
{
    SqlCommand cmd = new SqlCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "update BkhurData set Name='" + tbBpname.Text + "',Details='" + tbBpdetails.Text + "',Price='" + tbBpprice.Text + "',Size='" + tbBpsize.Text + "', Quantity ='"+tbBpquantity.Text+"' where Name = '" + originalName+ "'";
    db.ExeNonQuery(cmd);

    tbBpname.Text = "";
    tbBpdetails.Text = "";

    tbBpprice.Text = "";
    tbBpsize.Text = "";
    tbBpquantity.Text = "";


    cc();

    MessageBox.Show("updated successfully");
}

1 个答案:

答案 0 :(得分:1)

这是你可以玩的东西和微调,但我认为这将接近你想要做的事情。

首先我创建了标有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