Commit 0c9322be authored by Alessandro dos Santos Ferreira's avatar Alessandro dos Santos Ferreira
Browse files

Pynovisao - Adicionando toolbar / zoom

parent d96d7a81
...@@ -16,49 +16,108 @@ else: ...@@ -16,49 +16,108 @@ else:
import matplotlib import matplotlib
matplotlib.use('TkAgg') matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib import pylab as plt from matplotlib import pylab as plt
class Image(object): class Image(object):
def __init__(self, parent, image, onclick = None): def __init__(self, parent):
self.parent = parent self.parent = parent
self._im = None
self._canvas = None
self._fig = None
self._ax = None
self._toolbar = None
self._axes_visible = True
self._toolbar_visible = True
def toggle_axes(self):
self._axes_visible = not self._axes_visible
if self._canvas is not None:
self._ax.get_xaxis().set_visible(self._axes_visible)
self._ax.get_yaxis().set_visible(self._axes_visible)
self._fig.tight_layout()
self._canvas.draw()
def toggle_toolbar(self):
self._toolbar_visible = not self._toolbar_visible
if self._toolbar_visible == True:
self._show_toolbar()
else:
self._hide_toolbar()
def render(self, image, onclick = None):
self.parent.image = image self.parent.image = image
fig = plt.figure(facecolor='white', edgecolor='black', linewidth=1) self._fig = plt.figure(facecolor='white', edgecolor='black', linewidth=1)
self._im = plt.imshow(self.parent.image) # later use a.set_data(new_data) self._im = plt.imshow(self.parent.image) # later use a.set_data(new_data)
#ax = plt.gca()
#ax.set_xticklabels([]) self._ax = plt.gca()
#ax.set_yticklabels([]) self._ax.get_xaxis().set_visible(self._axes_visible)
self._ax.get_yaxis().set_visible(self._axes_visible)
# a tk.DrawingArea # a tk.DrawingArea
self._canvas = FigureCanvasTkAgg(fig, master=self.parent) self._fig.tight_layout()
self._canvas = FigureCanvasTkAgg(self._fig, master=self.parent)
self._canvas.show() self._canvas.show()
self._canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1) self._canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
if onclick is not None: if onclick is not None:
fig.canvas.mpl_connect('button_press_event', func = onclick) self._fig.canvas.mpl_connect('button_press_event', func = onclick)
if self._toolbar_visible == True:
self._show_toolbar()
def refresh(self, image): def refresh(self, image):
if self._canvas is not None: if self._canvas is not None:
self.parent.image = image self.parent.image = image
self._im.set_data(self.parent.image) self._im.set_data(self.parent.image)
self._fig.tight_layout()
self._canvas.draw() self._canvas.draw()
def close(self): def close(self):
if self._canvas is not None: if self._canvas is not None:
self.parent.image = None self.parent.image = None
#self._im.set_data(np.zeros((0,0,3), float)) #self._im.set_data(np.zeros((0,0,3), float))
#self._canvas.draw() #self._canvas.draw()
self._canvas.get_tk_widget().pack_forget(); self._canvas.get_tk_widget().pack_forget()
self._canvas.get_tk_widget().destroy(); self._canvas.get_tk_widget().destroy()
if self._toolbar is not None:
self._toolbar.pack_forget()
self._toolbar.destroy()
self._im = None self._im = None
self._canvas = None self._canvas = None
self._fig = None
self._ax = None
self._toolbar = None
return True return True
return False return False
def _show_toolbar(self):
if self._toolbar is None and self._canvas is not None:
self._toolbar = NavigationToolbar2TkAgg(self._canvas, self.parent)
self._toolbar.configure(background='white', borderwidth=0)
for child in self._toolbar.winfo_children():
child.configure(background='white', foreground='black')
#self._toolbar.update()
self._toolbar.pack(side=Tk.TOP, fill=Tk.X, expand=False)
def _hide_toolbar(self):
if self._toolbar is not None:
self._toolbar.pack_forget()
self._toolbar.destroy()
self._toolbar = None
...@@ -22,8 +22,8 @@ class Utils(object): ...@@ -22,8 +22,8 @@ class Utils(object):
def ask_image_name(title = 'Open a image'): def ask_image_name(title = 'Open a image'):
file_opt = options = {} file_opt = options = {}
options['defaultextension'] = '.jpg' options['defaultextension'] = '.jpg'
options['filetypes'] = [('All supported files', ("*.GIF", "*.gif", "*.JPG", "*.jpg", "*.JPEG", "*.jpeg", "*.PNG", "*.png", "*.TIF", "*.tif")), options['filetypes'] = [('All supported files', ("*.BMP", "*.bmp", "*.JPG", "*.jpg", "*.JPEG", "*.jpeg", "*.PNG", "*.png", "*.TIF", "*.tif")),
('GIF', ("*.GIF", "*.gif")), ('BMP', ("*.BMP", "*.bmp")),
('JPEG', ("*.JPG", "*.jpg", "*.JPEG", "*.jpeg")), ('JPEG', ("*.JPG", "*.jpg", "*.JPEG", "*.jpeg")),
('PNG', ("*.PNG", "*.png")), ('PNG', ("*.PNG", "*.png")),
('TIF', ("*.TIF", "*.tif"))] ('TIF', ("*.TIF", "*.tif"))]
......
...@@ -34,7 +34,7 @@ class TkInterface(Interface): ...@@ -34,7 +34,7 @@ class TkInterface(Interface):
self._menus = [] self._menus = []
self._image = None self._image = tk_local.Image(self._root)
self._grid = None self._grid = None
self._logger = None self._logger = None
self._conf_dialog = None self._conf_dialog = None
...@@ -68,30 +68,28 @@ class TkInterface(Interface): ...@@ -68,30 +68,28 @@ class TkInterface(Interface):
if self._image is not None: if self._image is not None:
self._image.close() self._image.close()
self._image = tk_local.Image(self._root, image, lambda event, *_: self._apply( onclick, event )) self._image.render(image, lambda event, *_: self._apply( onclick, event ))
if title is not None: if title is not None:
self.set_subtitle(title) self.set_subtitle(title)
def refresh_image(self, image, title = None):
if self._image is not None:
self._image.refresh(image)
if title is not None: def toggle_image_toolbar(self):
self.set_subtitle(title) self._image.toggle_toolbar()
def toggle_image_axes(self):
self._image.toggle_axes()
def refresh_image(self, image, title = None):
self._image.refresh(image)
if title is not None:
self.set_subtitle(title)
def close_image(self): def close_image(self):
if self._image is not None: if tkMessageBox.askokcancel("Quit", "Do you want to close the image?"):
if tkMessageBox.askokcancel("Quit", "Do you want to close the image?"): return self._image.close()
self._image.close()
self._image = None
return True
return False
def open_log(self): def open_log(self):
self._logger = tk_local.Log(self._root) self._logger = tk_local.Log(self._root)
...@@ -107,7 +105,7 @@ class TkInterface(Interface): ...@@ -107,7 +105,7 @@ class TkInterface(Interface):
if self._logger: if self._logger:
self._logger.clear_logger() self._logger.clear_logger()
def toogle_log(self): def toggle_log(self):
if self._logger: if self._logger:
self.close_log() self.close_log()
else: else:
......
...@@ -34,15 +34,22 @@ if __name__ == "__main__": ...@@ -34,15 +34,22 @@ if __name__ == "__main__":
tk.add_menu("File") tk.add_menu("File")
tk.add_command("Open a image", act.open_image, 'O') tk.add_command("Open a image", act.open_image, 'O')
tk.add_separator() tk.add_separator()
tk.add_command("Add new class", act.add_class, 'A')
tk.add_command("Set dataset path", act.set_dataset_path, 'd')
tk.add_separator()
tk.add_check_button("Show log", tk.toogle_log)
tk.add_separator()
tk.add_command("Close image", act.close_image, 'W') tk.add_command("Close image", act.close_image, 'W')
tk.add_separator() tk.add_separator()
tk.add_command("Quit", tk.quit, 'Q') tk.add_command("Quit", tk.quit, 'Q')
tk.add_menu("View")
tk.add_check_button("Show image axes", tk.toggle_image_axes)
tk.add_check_button("Show image toolbar", tk.toggle_image_toolbar)
tk.add_separator()
tk.add_check_button("Show log", tk.toggle_log)
tk.add_menu("Dataset")
tk.add_command("Add new class", act.add_class, 'A')
tk.add_command("Set dataset path", act.set_dataset_path, 'd')
tk.add_separator()
tk.add_check_button("Dataset generator", act.toggle_dataset_generator, 't')
tk.add_menu("Segmentation") tk.add_menu("Segmentation")
tk.add_command("Choose segmenter", act.select_segmenter) tk.add_command("Choose segmenter", act.select_segmenter)
tk.add_command("Configure", act.config_segmenter, 'g') tk.add_command("Configure", act.config_segmenter, 'g')
......
...@@ -39,6 +39,8 @@ class Act(object): ...@@ -39,6 +39,8 @@ class Act(object):
self._init_dataset(args["dataset"]) self._init_dataset(args["dataset"])
self._init_classes(args["classes"], args["colors"]) self._init_classes(args["classes"], args["colors"])
self._dataset_generator = True
def _init_dataset(self, directory): def _init_dataset(self, directory):
...@@ -62,12 +64,12 @@ class Act(object): ...@@ -62,12 +64,12 @@ class Act(object):
self.add_class(dialog = False, color='Yellow') self.add_class(dialog = False, color='Yellow')
self._current_class = 0 self._current_class = 0
def open_image(self, imagename = None): def open_image(self, imagename = None):
def onclick(event): def onclick(event):
if event.xdata != None and event.ydata != None and int(event.ydata) != 0: if event.xdata != None and event.ydata != None and int(event.ydata) != 0 and self._dataset_generator == True:
x = int(event.xdata) x = int(event.xdata)
y = int(event.ydata) y = int(event.ydata)
self.tk.write_log("Coordinates: x = %d y = %d", x, y) self.tk.write_log("Coordinates: x = %d y = %d", x, y)
...@@ -171,6 +173,9 @@ class Act(object): ...@@ -171,6 +173,9 @@ class Act(object):
self._init_classes() self._init_classes()
self.tk.refresh_panel_classes(self.classes) self.tk.refresh_panel_classes(self.classes)
def toggle_dataset_generator(self):
self._dataset_generator = not self._dataset_generator
def select_segmenter(self): def select_segmenter(self):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment