BiAPoL / napari-crop

BSD 3-Clause "New" or "Revised" License
7 stars 4 forks source link

Shape error for irregular shapes drawn close to image edge #13

Closed zoccoler closed 2 years ago

zoccoler commented 2 years ago

Applying crop to shapes like these:

import numpy as np
arr_2d = np.arange(0, 25).reshape((5, 5))
shapes = [
    np.array([[1, 1], [1, 3], [5, 3], [5, 1]]),
    np.array([[0.5, 0.5], [0.5, 3.5], [4.51, 3.5], [4.51, 0.5]]),
    np.array([[0, 2], [5, 5], [5, 2], [2, 0]]),

]
shape_types = ["rectangle", "ellipse", "polygon"]

viewer.add_image(arr_2d)
shapes_layer = viewer.add_shapes(shapes, shape_type=shape_types, edge_width=0)

shapes

Only works with the rectangle. For irregular shapes drawn close or over image edge, it gives an error like this:

ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (6,6)  and requested shape (5,5)
tdmorello commented 2 years ago

Maybe crop_region should be refactored to mask first and then crop? I wrote it in the other order to limit memory usage (e.g. if image were very large), but if we could keep the mask/crop as views onto the array, it would not take up so much memory.

It might fix this and #8

tdmorello commented 2 years ago

Alternatively, we could not worry about minimizing memory usage. Plus large arrays would probably be dask arrays, so they would not be entirely loaded into memory.