Mercurial > repos > imgteam > crop_image
annotate crop_image.py @ 0:f8bfa85cac4c draft default tip
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
| author | imgteam |
|---|---|
| date | Fri, 06 Jun 2025 12:46:50 +0000 |
| parents | |
| children |
| rev | line source |
|---|---|
|
0
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
1 import argparse |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
2 import os |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
3 |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
4 import numpy as np |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
5 from giatools.image import Image |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
6 |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
7 |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
8 def crop_image( |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
9 image_filepath: str, |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
10 labelmap_filepath: str, |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
11 output_ext: str, |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
12 output_dir: str, |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
13 skip_labels: frozenset[int], |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
14 ): |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
15 image = Image.read(image_filepath) |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
16 labelmap = Image.read(labelmap_filepath) |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
17 |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
18 if image.axes != labelmap.axes: |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
19 raise ValueError(f'Axes mismatch between image ({image.axes}) and label map ({labelmap.axes}).') |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
20 |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
21 if image.data.shape != labelmap.data.shape: |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
22 raise ValueError(f'Shape mismatch between image ({image.data.shape}) and label map ({labelmap.data.shape}).') |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
23 |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
24 for label in np.unique(labelmap.data): |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
25 if label in skip_labels: |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
26 continue |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
27 roi_mask = (labelmap.data == label) |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
28 roi = crop_image_to_mask(image.data, roi_mask) |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
29 roi_image = Image(roi, image.axes).normalize_axes_like(image.original_axes) |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
30 roi_image.write(os.path.join(output_dir, f'{label}.{output_ext}')) |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
31 |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
32 |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
33 def crop_image_to_mask(data: np.ndarray, mask: np.ndarray) -> np.ndarray: |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
34 """ |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
35 Crop the `data` array to the minimal bounding box in `mask`. |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
36 |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
37 The arguments are not modified. |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
38 """ |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
39 assert data.shape == mask.shape |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
40 |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
41 # Crop `data` to the convex hull of the mask in each dimension |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
42 for dim in range(data.ndim): |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
43 mask1d = mask.any(axis=tuple(i for i in range(mask.ndim) if i != dim)) |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
44 mask1d_indices = np.where(mask1d)[0] |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
45 mask1d_indices_cvxhull = np.arange(min(mask1d_indices), max(mask1d_indices) + 1) |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
46 data = data.take(axis=dim, indices=mask1d_indices_cvxhull) |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
47 |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
48 return data |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
49 |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
50 |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
51 if __name__ == "__main__": |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
52 parser = argparse.ArgumentParser() |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
53 parser.add_argument('image', type=str) |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
54 parser.add_argument('labelmap', type=str) |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
55 parser.add_argument('skip_labels', type=str) |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
56 parser.add_argument('output_ext', type=str) |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
57 parser.add_argument('output_dir', type=str) |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
58 args = parser.parse_args() |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
59 |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
60 crop_image( |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
61 image_filepath=args.image, |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
62 labelmap_filepath=args.labelmap, |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
63 output_ext=args.output_ext, |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
64 output_dir=args.output_dir, |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
65 skip_labels=frozenset( |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
66 int(label.strip()) for label in args.skip_labels.split(',') if label.strip() |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
67 ) if args.skip_labels.strip() else frozenset(), |
|
f8bfa85cac4c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/crop_image/ commit 7a5037206d267aa7d9b7e5e062327c3464942471
imgteam
parents:
diff
changeset
|
68 ) |
