1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
| from tkinter import *
import numpy as np
import matplotlib.pyplot as plt
from skimage import data
from PIL import Image as imge
from PIL import ImageTk as imtk
from scipy import ndimage
from skimage import morphology
from skimage.filter import sobel
class AppUI(Frame):
def __init__(self, master=None):
Frame.__init__(self, master,relief=SUNKEN,bd=2)
self.menubar = Menu(self)
menu = Menu(self.menubar, tearoff=0)
self.menubar.add_cascade(label="Fichier", menu=menu)
menu.add_command(label="Ouvrir une image", command=self.ouvrir)
menu.add_command(label="Fermer l'image", command=self.fermer)
menu.add_command(label="Enregistrer le résultat", command=self.enregistrer, state='disabled')
#menu.add_command(label="Quitter", command=root.destroy)
menu = Menu(self.menubar, tearoff=0)
self.menubar.add_cascade(label="Traitement", menu=menu)
menu.add_command(label="Algo 1", command=self.edit)
menu.add_command(label="Algo 2", state='disabled')
menu.add_command(label="Algo 3", state='disabled')
self.menubar.add_command(label="Calculer", command=self.calculer)
self.master.config(menu=self.menubar)
try:
self.master.config(menu=self.menubar)
except AttributeError:
# master is a toplevel window (Python 1.4/Tkinter 1.63)
self.master.tk.call(master, "config", "-menu", self.menubar)
self.can = Canvas(self, bg="white", width=400, height=400,
bd=0, highlightthickness=0)
self.can.pack()
#self.lab = Scale(length=300, orient=HORIZONTAL, sliderlength=20,
# label='Paramètre :', from_=1, to=10, tickinterval =1, showvalue =1, command=self.updatelabel)
#self.lab.configure(bg='white', bd=2, relief=SOLID)
#self.lab.pack()
bouton_sortir = Button(self,text="Quitter",command=self.end)
bouton_sortir.pack()
topho = 0
def end(self):
if messagebox.askyesno('Question',"Êtes-vous sûr de vouloir partir ?"):
messagebox.showinfo('Message',"A bientôt !")
root.destroy()
else:
messagebox.showwarning('Mais...', "Pourquoi t'appuies sur quitter alors ? -_-")
def ouvrir(self):
# Utilisation d'un dictionnaire pour conserver une référence
gifdict={}
# on efface la zone graphique
self.can.delete(ALL) #-> AttributeError: 'str' object has no attribute 'tk'
filename = filedialog.askopenfilename(title="Ouvrir une image",filetypes=[('tiff files','.tif'),('jpeg files','.jpg'),
('png files','.png'),('gif files','.gif'),('all files','.*')])
print(filename)
monimage = imge.open(filename)
photo = imtk.PhotoImage(monimage)
#photo = PhotoImage(file=filename)
gifdict[filename] = photo # référence
print(gifdict)
self.topho = photo
self.can.config(height=photo.height(),width=photo.width())
self.can.create_image(0,1,anchor=NW,image=photo)
#ligne suivante créée car si pas de ligne de commande sur self.can, image apparaît pas dans canevas
#self.can.create_title("Image "+str(photo.width())+" x "+str(photo.height()))
def fermer(self):
self.can.delete(ALL)
self.title("Image")
def enregistrer(self): print ("enregistrer")
def calculer(self): print ("calculer")
def edit (self): #algorithme de traitement d'image
#-------------------- Transformation 3D -> 2D (gray levels)
gray = self.topho.convert('L') # 'L' stands for 'luminosity'
coins = np.asarray(gray)
#-------------------- Elevation map (Sobel gradient)
elevation_map = sobel(coins)
#-------------------- Find markers : histogram of grey values
markers = np.zeros_like(coins) #retourne une image de zero de même type/forme
#et dimension que l'input
markers[coins < 80] = 3
markers[coins > 160] = 1
plt.figure(figsize=(8, 6))
plt.imshow(markers, cmap=plt.cm.spectral, interpolation='nearest')
plt.axis('off')
plt.title('markers')
#-------------------- Watershed transform
segmentation = morphology.watershed(elevation_map, markers)
plt.figure(figsize=(8, 6))
plt.imshow(segmentation, cmap=plt.cm.gray, interpolation='nearest')
plt.axis('off')
plt.title('Segmentation')
plt.show()
root = Tk()
app = AppUI(root)
app.pack()
root.mainloop() |
Partager