0

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()
user2175850
  • 193
  • 2
  • 13

1 Answers1

0

Unfortunately, there's nothing in Chaco, currently, that does what you need. That said, there's an open pull request that adds that functionality: https://github.com/enthought/chaco/pull/162.

As that PR suggests, there's a bit of design discussion about Chaco tools, in general, that needs to happen before more tools are added.

I hope that helps,

-Tony

Tony S Yu
  • 3,003
  • 30
  • 40