Skip to content

Commit

Permalink
Merge pull request #36 from BiAPoL/save_coordinates
Browse files Browse the repository at this point in the history
add bbox to metadata
  • Loading branch information
haesleinhuepf authored Feb 17, 2023
2 parents 7fff6ff + eb23fae commit c29bd0a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion napari_crop/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

__version__ = "0.1.6"
__version__ = "0.1.7"



Expand Down
14 changes: 11 additions & 3 deletions napari_crop/_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,20 @@ def crop_region(

# trim zeros
non_zero = np.where(cropped_data != 0)
indices = [slice(min(i_nz), max(i_nz) + 1) for i_nz in non_zero]
slices = [slice(min(i_nz), max(i_nz) + 1) for i_nz in non_zero]
if rgb:
indices[-1] = slice(None)
cropped_data = cropped_data[tuple(indices)]
slices[-1] = slice(None)
cropped_data = cropped_data[tuple(slices)]

new_layer_props = layer_props.copy()
# Update start and stop values for bbox
start = [slc.start for slc in slices if slc is not None]
stop = [slc.stop for slc in slices if slc is not None]
# Add cropped coordinates as metadata
# bounding box: (min_row, max_row, min_col, max_col)
# Pixels belonging to the bounding box are in the half-open interval [min_row; max_row) and [min_col; max_col).
new_layer_props['metadata'] = {'bbox': (start[0], stop[0], start[1], stop[1])}

# If layer name is in viewer or is about to be added,
# increment layer name until it has a different name
while True:
Expand Down
20 changes: 19 additions & 1 deletion napari_crop/_tests/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
[ 0, 6, 7, 8, 0],
[10, 11, 12, 13, 14],
[ 0, 0, 17, 0, 0]]), # fmt: skip

]
bbox_expected = [(1.0, 5.0, 1.0, 4.0),
(1.0, 5.0, 1.0, 4.0),
(0.0, 4.0, 0.0, 5.0)]

# rectangle crop
# array([[ 6, 7, 8],
Expand Down Expand Up @@ -62,6 +64,22 @@ def test_crop_function_values_2d(make_napari_viewer, shape, shape_type,
assert np.array_equal(crop_expected, cropped_actual_arrays)


@pytest.mark.parametrize(
"shape,shape_type,bbox_expected",
zip(shapes, shape_types, bbox_expected)
)
def test_bbox_values(make_napari_viewer, shape, shape_type,
bbox_expected):
"""Test that the bbox returned in metadata is correct."""

viewer = make_napari_viewer()
img_layer = viewer.add_image(arr_2d)
shapes_layer = viewer.add_shapes(shape, shape_type=shape_type)
cropped_actual = crop_region(img_layer, shapes_layer)[0][1] # get layer properties
bbox = cropped_actual['metadata']['bbox']
assert np.array_equal(bbox_expected, bbox)


def test_crop_multiple_shapes(make_napari_viewer):
"""Test that 'n' drawn shapes return 'n' new cropped layers"""

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = napari-crop
version = 0.1.6
version = 0.1.7
author = Robert Haase, Tim Morello, Marcelo Leomil Zoccoler, Johannes Muller
author_email = [email protected]
license = BSD-3-Clause
Expand Down

0 comments on commit c29bd0a

Please sign in to comment.