mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-18 12:40:14 +08:00
STY: Refactors general parts of line tool to base tool
This commit is contained in:
@@ -34,6 +34,8 @@ class CanvasToolBase(object):
|
||||
on_enter = lambda *args: None
|
||||
self.on_enter = on_enter
|
||||
|
||||
self.connect_event('key_press_event', self._on_key_press)
|
||||
|
||||
def connect_event(self, event, callback):
|
||||
"""Connect callback with an event.
|
||||
|
||||
@@ -79,6 +81,18 @@ class CanvasToolBase(object):
|
||||
self.canvas.blit(self.ax.bbox)
|
||||
else:
|
||||
self.canvas.draw_idle()
|
||||
self.on_update(self.geometry)
|
||||
|
||||
def _on_key_press(self, event):
|
||||
if event.key == 'enter':
|
||||
self.on_enter(self.geometry)
|
||||
self.set_visible(False)
|
||||
self.redraw()
|
||||
|
||||
@property
|
||||
def geometry(self):
|
||||
"""Geometry information that gets passed to callback functions."""
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class ToolHandles(object):
|
||||
|
||||
@@ -43,10 +43,15 @@ class LineTool(CanvasToolBase):
|
||||
self._handles.set_visible(True)
|
||||
self._artists = [self._line, self._handles.artist]
|
||||
|
||||
if on_enter is None:
|
||||
def on_enter(pts):
|
||||
x, y = np.transpose(pts)
|
||||
print "length = %0.2f" % np.sqrt(np.diff(x)**2 + np.diff(y)**2)
|
||||
self.on_enter = on_enter
|
||||
|
||||
self.connect_event('button_press_event', self.on_mouse_press)
|
||||
self.connect_event('button_release_event', self.on_mouse_release)
|
||||
self.connect_event('motion_notify_event', self.on_move)
|
||||
self.connect_event('key_press_event', self.on_key_press)
|
||||
|
||||
def on_mouse_press(self, event):
|
||||
if event.button != 1 and event.inaxes == None:
|
||||
@@ -71,12 +76,6 @@ class LineTool(CanvasToolBase):
|
||||
return
|
||||
self.update(event.xdata, event.ydata)
|
||||
|
||||
def on_key_press(self, event):
|
||||
if event.key == 'enter':
|
||||
self.on_enter(self.end_pts)
|
||||
self.set_visible(False)
|
||||
self.redraw()
|
||||
|
||||
def update(self, x, y):
|
||||
if x is not None:
|
||||
self.end_pts[self._active_pt, :] = x, y
|
||||
@@ -85,7 +84,10 @@ class LineTool(CanvasToolBase):
|
||||
self._line.set_linewidth(self.linewidth)
|
||||
|
||||
self.redraw()
|
||||
self.on_update(self.end_pts)
|
||||
|
||||
@property
|
||||
def geometry(self):
|
||||
return self.end_pts
|
||||
|
||||
|
||||
class ThickLineTool(LineTool):
|
||||
@@ -97,6 +99,7 @@ class ThickLineTool(LineTool):
|
||||
lineprops=lineprops)
|
||||
|
||||
self.connect_event('scroll_event', self.on_scroll)
|
||||
self.connect_event('key_press_event', self.on_key_press)
|
||||
|
||||
def on_scroll(self, event):
|
||||
if not event.inaxes:
|
||||
@@ -107,8 +110,6 @@ class ThickLineTool(LineTool):
|
||||
self._shrink_scan_line()
|
||||
|
||||
def on_key_press(self, event):
|
||||
super(ThickLineTool, self).on_key_press(event)
|
||||
|
||||
if event.key == '+':
|
||||
self._thicken_scan_line()
|
||||
elif event.key == '-':
|
||||
@@ -134,10 +135,6 @@ if __name__ == '__main__':
|
||||
ax.imshow(image, interpolation='nearest')
|
||||
h, w = image.shape
|
||||
|
||||
def printer(pts):
|
||||
x, y = np.transpose(pts)
|
||||
print "length = %0.2f" % np.sqrt(np.diff(x)**2 + np.diff(y)**2)
|
||||
|
||||
# line_tool = LineTool(ax, [w/3, 2*w/3], [h/2, h/2])
|
||||
line_tool = ThickLineTool(ax, [w/3, 2*w/3], [h/2, h/2], on_enter=printer)
|
||||
line_tool = ThickLineTool(ax, [w/3, 2*w/3], [h/2, h/2])
|
||||
plt.show()
|
||||
|
||||
@@ -14,7 +14,6 @@ rad2deg = 180 / np.pi
|
||||
|
||||
class Measure(Plugin):
|
||||
name = 'Measure'
|
||||
draws_on_image = True
|
||||
|
||||
def __init__(self, maxdist=10, **kwargs):
|
||||
super(Measure, self).__init__(**kwargs)
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
from skimage import data
|
||||
from skimage.viewer import ImageViewer
|
||||
from skimage.viewer.plugins.measure import Measure
|
||||
|
||||
|
||||
image = data.camera()
|
||||
viewer = ImageViewer(image)
|
||||
viewer += Measure()
|
||||
viewer.show()
|
||||
Reference in New Issue
Block a user