# HG changeset patch # User imgteam # Date 1749214010 0 # Node ID f8bfa85cac4cb007315ab1838cb417ea29de6e78 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471 diff -r 000000000000 -r f8bfa85cac4c creators.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/creators.xml Fri Jun 06 12:46:50 2025 +0000 @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff -r 000000000000 -r f8bfa85cac4c crop_image.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/crop_image.py Fri Jun 06 12:46:50 2025 +0000 @@ -0,0 +1,68 @@ +import argparse +import os + +import numpy as np +from giatools.image import Image + + +def crop_image( + image_filepath: str, + labelmap_filepath: str, + output_ext: str, + output_dir: str, + skip_labels: frozenset[int], +): + image = Image.read(image_filepath) + labelmap = Image.read(labelmap_filepath) + + if image.axes != labelmap.axes: + raise ValueError(f'Axes mismatch between image ({image.axes}) and label map ({labelmap.axes}).') + + if image.data.shape != labelmap.data.shape: + raise ValueError(f'Shape mismatch between image ({image.data.shape}) and label map ({labelmap.data.shape}).') + + for label in np.unique(labelmap.data): + if label in skip_labels: + continue + roi_mask = (labelmap.data == label) + roi = crop_image_to_mask(image.data, roi_mask) + roi_image = Image(roi, image.axes).normalize_axes_like(image.original_axes) + roi_image.write(os.path.join(output_dir, f'{label}.{output_ext}')) + + +def crop_image_to_mask(data: np.ndarray, mask: np.ndarray) -> np.ndarray: + """ + Crop the `data` array to the minimal bounding box in `mask`. + + The arguments are not modified. + """ + assert data.shape == mask.shape + + # Crop `data` to the convex hull of the mask in each dimension + for dim in range(data.ndim): + mask1d = mask.any(axis=tuple(i for i in range(mask.ndim) if i != dim)) + mask1d_indices = np.where(mask1d)[0] + mask1d_indices_cvxhull = np.arange(min(mask1d_indices), max(mask1d_indices) + 1) + data = data.take(axis=dim, indices=mask1d_indices_cvxhull) + + return data + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument('image', type=str) + parser.add_argument('labelmap', type=str) + parser.add_argument('skip_labels', type=str) + parser.add_argument('output_ext', type=str) + parser.add_argument('output_dir', type=str) + args = parser.parse_args() + + crop_image( + image_filepath=args.image, + labelmap_filepath=args.labelmap, + output_ext=args.output_ext, + output_dir=args.output_dir, + skip_labels=frozenset( + int(label.strip()) for label in args.skip_labels.split(',') if label.strip() + ) if args.skip_labels.strip() else frozenset(), + ) diff -r 000000000000 -r f8bfa85cac4c crop_image.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/crop_image.xml Fri Jun 06 12:46:50 2025 +0000 @@ -0,0 +1,105 @@ + + with giatools + + creators.xml + tests.xml + 0.4.1 + 0 + + + + + + operation_3443 + + + giatools + + + giatools + + + + + + + ^\d+(,\d+)*$|^$ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + **Crops an image using one or more regions of interest.** + + The image is cropped using a label map that identifies individual regions of interest. The image and the label map must be of equal size. + + This operation preserves the file type of the image, the brightness, and the range of values. + + + + 10.1016/j.jbiotec.2017.07.019 + + diff -r 000000000000 -r f8bfa85cac4c test-data/yx_float32.tiff Binary file test-data/yx_float32.tiff has changed diff -r 000000000000 -r f8bfa85cac4c test-data/yx_float32_uint8_0.tiff Binary file test-data/yx_float32_uint8_0.tiff has changed diff -r 000000000000 -r f8bfa85cac4c test-data/yx_float32_uint8_1.tiff Binary file test-data/yx_float32_uint8_1.tiff has changed diff -r 000000000000 -r f8bfa85cac4c test-data/yx_float32_uint8_2.tiff Binary file test-data/yx_float32_uint8_2.tiff has changed diff -r 000000000000 -r f8bfa85cac4c test-data/yx_uint8.tiff Binary file test-data/yx_uint8.tiff has changed diff -r 000000000000 -r f8bfa85cac4c test-data/yxc_uint8.png Binary file test-data/yxc_uint8.png has changed diff -r 000000000000 -r f8bfa85cac4c test-data/yxc_uint8_mask.png Binary file test-data/yxc_uint8_mask.png has changed diff -r 000000000000 -r f8bfa85cac4c test-data/yxc_uint8_uint8_2.png Binary file test-data/yxc_uint8_uint8_2.png has changed diff -r 000000000000 -r f8bfa85cac4c test-data/yxz_uint8.tiff Binary file test-data/yxz_uint8.tiff has changed diff -r 000000000000 -r f8bfa85cac4c test-data/zyx_uint16.tiff Binary file test-data/zyx_uint16.tiff has changed diff -r 000000000000 -r f8bfa85cac4c test-data/zyx_uint16_uint8_1.tiff Binary file test-data/zyx_uint16_uint8_1.tiff has changed diff -r 000000000000 -r f8bfa85cac4c tests.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests.xml Fri Jun 06 12:46:50 2025 +0000 @@ -0,0 +1,95 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +