I am trying to draw a 2D selection box on an image plot and get out the selected region. I cannot find the appropriate tool to do this.
I assumed RangeSelection2D would be appropriate for this but it seems to only actually select 1 of the 2 axes.
I could modify the BetterSelectingZoom where the box mode is similar to what I want. Does anyone know the best way or chaco tool to do this?
import enthought.traits.api as traits
import enthought.chaco.api as chaco
import enthought.chaco.tools.api as ctools
import enthought.traits.ui.api as ui
from enthought.enable.component_editor import ComponentEditor
class ImageViewer(traits.HasTraits):
plot = traits.Instance(chaco.Plot)
view = ui.View(
ui.Item('plot', editor=ComponentEditor(), show_label=False),
width=600, height=600, resizable=True)
def __init__(self, image_array, *args, **kwargs):
super(ImageViewer, self).__init__(*args, **kwargs)
pd = chaco.ArrayPlotData(imagedata=image_array)
self.plot = chaco.Plot(pd, default_origin='top left')
img_plot = self.plot.img_plot("imagedata")[0]
range_select = ctools.RangeSelection2D(component=img_plot)
range_select.left_button_selects = True
img_plot.tools.append(range_select)
range_overlay = ctools.RangeSelectionOverlay(component=img_plot)
img_plot.overlays.append(range_overlay)
if __name__ == "__main__":
import numpy as np
image = np.random.random_integers(0, 255, size=(20, 20))
imageui = ImageViewer(image)
imageui.configure_traits()