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:
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib import pylab as plt
class Image(object):
def __init__(self, parent, image, onclick = None):
def __init__(self, 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
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)
#ax = plt.gca()
#ax.set_xticklabels([])
#ax.set_yticklabels([])
self._ax = plt.gca()
self._ax.get_xaxis().set_visible(self._axes_visible)
self._ax.get_yaxis().set_visible(self._axes_visible)
# 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.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
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):
if self._canvas is not None:
self.parent.image = image
self._im.set_data(self.parent.image)
self._fig.tight_layout()
self._canvas.draw()
def close(self):
if self._canvas is not None:
self.parent.image = None
#self._im.set_data(np.zeros((0,0,3), float))
#self._canvas.draw()
self._canvas.get_tk_widget().pack_forget();
self._canvas.get_tk_widget().destroy();
self._canvas.get_tk_widget().pack_forget()
self._canvas.get_tk_widget().destroy()
if self._toolbar is not None:
self._toolbar.pack_forget()
self._toolbar.destroy()
self._im = None
self._canvas = None
self._fig = None
self._ax = None
self._toolbar = None
return True
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):
def ask_image_name(title = 'Open a image'):
file_opt = options = {}
options['defaultextension'] = '.jpg'
options['filetypes'] = [('All supported files', ("*.GIF", "*.gif", "*.JPG", "*.jpg", "*.JPEG", "*.jpeg", "*.PNG", "*.png", "*.TIF", "*.tif")),
('GIF', ("*.GIF", "*.gif")),
options['filetypes'] = [('All supported files', ("*.BMP", "*.bmp", "*.JPG", "*.jpg", "*.JPEG", "*.jpeg", "*.PNG", "*.png", "*.TIF", "*.tif")),
('BMP', ("*.BMP", "*.bmp")),
('JPEG', ("*.JPG", "*.jpg", "*.JPEG", "*.jpeg")),
('PNG', ("*.PNG", "*.png")),
('TIF', ("*.TIF", "*.tif"))]
......
......@@ -34,7 +34,7 @@ class TkInterface(Interface):
self._menus = []
self._image = None
self._image = tk_local.Image(self._root)
self._grid = None
self._logger = None
self._conf_dialog = None
......@@ -68,28 +68,26 @@ class TkInterface(Interface):
if self._image is not None:
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:
self.set_subtitle(title)
def toggle_image_toolbar(self):
self._image.toggle_toolbar()
def toggle_image_axes(self):
self._image.toggle_axes()
def refresh_image(self, image, title = None):
if self._image is not None:
self._image.refresh(image)
if title is not None:
self.set_subtitle(title)
def close_image(self):
if self._image is not None:
if tkMessageBox.askokcancel("Quit", "Do you want to close the image?"):
self._image.close()
self._image = None
return True
return False
return self._image.close()
def open_log(self):
......@@ -107,7 +105,7 @@ class TkInterface(Interface):
if self._logger:
self._logger.clear_logger()
def toogle_log(self):
def toggle_log(self):
if self._logger:
self.close_log()
else:
......
......@@ -34,15 +34,22 @@ if __name__ == "__main__":
tk.add_menu("File")
tk.add_command("Open a image", act.open_image, 'O')
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_separator()
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_command("Choose segmenter", act.select_segmenter)
tk.add_command("Configure", act.config_segmenter, 'g')
......
......@@ -40,6 +40,8 @@ class Act(object):
self._init_dataset(args["dataset"])
self._init_classes(args["classes"], args["colors"])
self._dataset_generator = True
def _init_dataset(self, directory):
if(directory[-1] == '/'):
......@@ -67,7 +69,7 @@ class Act(object):
def open_image(self, imagename = None):
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)
y = int(event.ydata)
self.tk.write_log("Coordinates: x = %d y = %d", x, y)
......@@ -172,6 +174,9 @@ class Act(object):
self._init_classes()
self.tk.refresh_panel_classes(self.classes)
def toggle_dataset_generator(self):
self._dataset_generator = not self._dataset_generator
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