我正在尝试在图像图上绘制一个 2D 选择框并退出所选区域。我找不到合适的工具来执行此操作。
我认为 RangeSelection2D 适合于此,但它似乎实际上只选择了 2 个轴中的 1 个。
我可以修改 BetterSelectingZoom,其中框模式与我想要的相似。有谁知道最好的方法或 chaco 工具来做到这一点?
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()