|
11 | 11 |
|
12 | 12 | import numpy as np
|
13 | 13 |
|
14 |
| -from ..funcs import concat_images, as_closest_canonical, OrientationError |
| 14 | +from ..funcs import concat_images, as_closest_canonical, OrientationError, crop_image |
15 | 15 | from ..analyze import AnalyzeImage
|
16 | 16 | from ..nifti1 import Nifti1Image
|
17 | 17 | from ..loadsave import save
|
@@ -193,3 +193,22 @@ def test_closest_canonical():
|
193 | 193 | img.header.set_dim_info(None, None, 2)
|
194 | 194 | xyz_img = as_closest_canonical(img)
|
195 | 195 | assert_true(xyz_img.header.get_dim_info() == (None, None, 1))
|
| 196 | + |
| 197 | + |
| 198 | +def test_crop_image(): |
| 199 | + # Use 32-bit data so that the AnalyzeImage class doesn't complain |
| 200 | + arr = np.arange(60).reshape((5, 3, 4, 1)).astype(np.int32) |
| 201 | + |
| 202 | + img = AnalyzeImage(arr, np.eye(4)) |
| 203 | + |
| 204 | + cropped_img = crop_image(img, [[1, 3], [1, 1], [1, 2]]) |
| 205 | + assert_equal(cropped_img.shape, (3, 1, 2, 1)) |
| 206 | + assert_array_equal(cropped_img.affine, [[1, 0, 0, 1], |
| 207 | + [0, 1, 0, 1], |
| 208 | + [0, 0, 1, 1], |
| 209 | + [0, 0, 0, 1]]) |
| 210 | + |
| 211 | + cropped_img = crop_image(img, [[1, 3], [1, 1], [1, 2]], margin=1) |
| 212 | + assert_equal(cropped_img.shape, (5, 3, 4, 1)) |
| 213 | + assert_array_equal(cropped_img.affine, img.affine) |
| 214 | + assert_true(cropped_img is img) |
0 commit comments