微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

刷新图像并在同一Tkinter画布上绘图

如何解决刷新图像并在同一Tkinter画布上绘图

我正在用matplotlib画布对tkinter GUI进行编码,以显示带有图片的灰度图片。按下按钮后,我希望显示一个新图像,并在顶部显示一个新图。这是一个MWE,它使用skimage.data中的库存图片以及该图片上的最大值的散布图,并使用ndimage.maximum_filter(来自there)。

初始化时显示图片显示每张新图片的外观。 我设法改变了图片,但是

  • 我不知道如何将画布调整为新图片的大小。
  • 散点的坐标与显示的不匹配 图片
  • 在每次刷新时不会清除画布,因此点只是 堆积起来。

关于如何解决这些不同问题的任何想法?我也想知道是否应该使用基于类的方法...我在使用Python 3

import tkinter as tk

import numpy as np

import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import figureCanvasTkAgg,NavigationToolbar2Tk
from skimage import data,img_as_float
from scipy import ndimage as ndi

def disp_img1():
    root.image = img_as_float(data.coins())
    im.set_data(root.image)
    
    image_max = ndi.maximum_filter(root.image,size=20) == root.image
    j,i = np.where(image_max)
    ax.plot(i,j,'b.')

    canvas.draw()
    toolbar.update()
    
def disp_img2():
    root.image = img_as_float(data.camera())
    im.set_data(root.image)
    
    image_max = ndi.maximum_filter(root.image,'g.')
    
    canvas.draw()
    toolbar.update()

root = tk.Tk()

# Create frames
LeftFrame = tk.Frame(root)
LeftFrame.pack( side = tk.LEFT )

RightFrame = tk.Frame(root)
RightFrame.pack()

# Create Buttons
Button1 = tk.Button( LeftFrame,text="Image 1",command=disp_img1) 
Button1.pack(padx = 5,pady = 5)

Button2 = tk.Button( LeftFrame,text="Image 2",command=disp_img2 )
Button2.pack(padx = 5,pady = 5)

# Initial image
root.image = img_as_float(data.cell())
fig,ax = plt.subplots()

image_max = ndi.maximum_filter(root.image,size=40) == root.image
j,i = np.where(image_max)

im = plt.imshow(root.image,cmap=plt.cm.gray)
ax.plot(i,'r.')

canvas = figureCanvasTkAgg(fig,master=RightFrame)
canvas.draw()

toolbar = NavigationToolbar2Tk(canvas,RightFrame)
toolbar.update()
canvas.get_tk_widget().pack(side=tk.TOP,fill=tk.BOTH,expand=1)

root.mainloop()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。