Skip to content

Commit 7f1fd04

Browse files
committed
RF: Remove crop_image (for now)
1 parent 494784a commit 7f1fd04

File tree

2 files changed

+1
-86
lines changed

2 files changed

+1
-86
lines changed

nibabel/funcs.py

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -219,69 +219,3 @@ def _aff_is_diag(aff):
219219
''' Utility function returning True if affine is nearly diagonal '''
220220
rzs_aff = aff[:3, :3]
221221
return np.allclose(rzs_aff, np.diag(np.diag(rzs_aff)))
222-
223-
224-
def crop_image(img, bounds, margin=0):
225-
''' Crop ``img`` to specified bounds
226-
227-
The image is cropped in the current orientation; no rotation or resampling
228-
is performed.
229-
The affine matrix is updated with the new intercept, so that all values are
230-
found at the same RAS locations.
231-
232-
Parameters
233-
----------
234-
img : ``spatialimage``
235-
Image to be cropped
236-
bounds : (3, 2) array-like
237-
Minimum and maximum (inclusive) voxel indices, in each dimension, to be
238-
included in cropped image
239-
margin : int, optional
240-
Margin, in voxels, to include on each side of cropped image
241-
242-
Returns
243-
-------
244-
cropped_img : ``spatialimage``
245-
Version of `img` with cropped data array and updated affine matrix
246-
'''
247-
248-
shape = np.reshape(img.shape[:3], (3, 1))
249-
bounds = np.asanyarray(bounds)
250-
if bounds.shape != (3, 2):
251-
raise ValueError("bounds must be interpretable as a 3x2 array")
252-
elif np.any(bounds > shape):
253-
raise ValueError("bounds must not exceed image dimensions")
254-
255-
# Permit negative bounds
256-
if np.any(bounds < 0):
257-
bounds = (bounds + shape) % shape
258-
259-
if np.any(bounds < 0):
260-
raise ValueError("negative bounds may not exceed image dimensions")
261-
elif np.any(bounds[:, 0] > bounds[:, 1]):
262-
raise ValueError("degenerate (0 width) crops are not permitted")
263-
264-
# Add margin in all directions
265-
bounds += np.array([-margin, margin])
266-
267-
# Set min/max
268-
bounds[bounds < 0] = 0
269-
over = bounds[:, 1] > shape.reshape(-1) - 1
270-
bounds[over, 1] = shape[over, 0] - 1
271-
272-
# Include upper bounds
273-
bounds[:, 1] += 1
274-
275-
# Return original image if no cropping required
276-
if np.array_equal(bounds, np.hstack(([[0], [0], [0]], shape))):
277-
return img
278-
279-
x, y, z = bounds
280-
new_origin = np.vstack((bounds[:, [0]], 1))
281-
282-
bounded_data = img.get_data()[x[0]:x[1], y[0]:y[1], z[0]:z[1]]
283-
284-
new_aff = img.affine.copy()
285-
new_aff[:, [3]] = img.affine.dot(new_origin)
286-
287-
return img.__class__(bounded_data, new_aff, img.header)

nibabel/tests/test_funcs.py

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import numpy as np
1313

14-
from ..funcs import concat_images, as_closest_canonical, OrientationError, crop_image
14+
from ..funcs import concat_images, as_closest_canonical, OrientationError
1515
from ..analyze import AnalyzeImage
1616
from ..nifti1 import Nifti1Image
1717
from ..loadsave import save
@@ -193,22 +193,3 @@ def test_closest_canonical():
193193
img.header.set_dim_info(None, None, 2)
194194
xyz_img = as_closest_canonical(img)
195195
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

Comments
 (0)