forked from Hendrik-code/spineps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.py
685 lines (559 loc) · 26.4 KB
/
image.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
import os
import numpy as np
import nibabel as nib
import logging
from copy import deepcopy
logger = logging.getLogger(__name__)
class Image(object):
"""
Compact version of SCT's Image Class (https://github.com/spinalcordtoolbox/spinalcordtoolbox/blob/master/spinalcordtoolbox/image.py#L245)
Create an object that behaves similarly to nibabel's image object. Useful additions include: dims, change_orientation and getNonZeroCoordinates.
"""
def __init__(self, param=None, hdr=None, orientation=None, absolutepath=None, dim=None):
"""
:param param: string indicating a path to a image file or an `Image` object.
"""
# initialization of all parameters
self.affine = None
self.data = None
self._path = None
self.ext = ""
if absolutepath is not None:
self._path = os.path.abspath(absolutepath)
# Case 1: load an image from file
if isinstance(param, str):
self.loadFromPath(param)
# Case 2: create a copy of an existing `Image` object
elif isinstance(param, type(self)):
self.copy(param)
# Case 3: create a blank image from a list of dimensions
elif isinstance(param, list):
self.data = np.zeros(param)
self.hdr = hdr.copy() if hdr is not None else nib.Nifti1Header()
self.hdr.set_data_shape(self.data.shape)
# Case 4: create an image from an existing data array
elif isinstance(param, (np.ndarray, np.generic)):
self.data = param
self.hdr = hdr.copy() if hdr is not None else nib.Nifti1Header()
self.hdr.set_data_shape(self.data.shape)
else:
raise TypeError('Image constructor takes at least one argument.')
# Fix any mismatch between the array's datatype and the header datatype
self.fix_header_dtype()
@property
def dim(self):
return get_dimension(self)
@property
def orientation(self):
return get_orientation(self)
@property
def absolutepath(self):
"""
Storage path (either actual or potential)
Notes:
- As several tools perform chdir() it's very important to have absolute paths
- When set, if relative:
- If it already existed, it becomes a new basename in the old dirname
- Else, it becomes absolute (shortcut)
Usually not directly touched (use `Image.save`), but in some cases it's
the best way to set it.
"""
return self._path
@absolutepath.setter
def absolutepath(self, value):
if value is None:
self._path = None
return
elif not os.path.isabs(value) and self._path is not None:
value = os.path.join(os.path.dirname(self._path), value)
elif not os.path.isabs(value):
value = os.path.abspath(value)
self._path = value
@property
def header(self):
return self.hdr
@header.setter
def header(self, value):
self.hdr = value
def __deepcopy__(self, memo):
return type(self)(deepcopy(self.data, memo), deepcopy(self.hdr, memo), deepcopy(self.orientation, memo), deepcopy(self.absolutepath, memo), deepcopy(self.dim, memo))
def copy(self, image=None):
if image is not None:
self.affine = deepcopy(image.affine)
self.data = deepcopy(image.data)
self.hdr = deepcopy(image.hdr)
self._path = deepcopy(image._path)
else:
return deepcopy(self)
def loadFromPath(self, path):
"""
This function load an image from an absolute path using nibabel library
:param path: path of the file from which the image will be loaded
:return:
"""
self.absolutepath = os.path.abspath(path)
im_file = nib.load(self.absolutepath, mmap=True)
self.affine = im_file.affine.copy()
self.data = np.asanyarray(im_file.dataobj)
self.hdr = im_file.header.copy()
if path != self.absolutepath:
logger.debug("Loaded %s (%s) orientation %s shape %s", path, self.absolutepath, self.orientation, self.data.shape)
else:
logger.debug("Loaded %s orientation %s shape %s", path, self.orientation, self.data.shape)
def change_orientation(self, orientation, inverse=False):
"""
Change orientation on image (in-place).
:param orientation: orientation string (SCT "from" convention)
:param inverse: if you think backwards, use this to specify that you actually\
want to transform *from* the specified orientation, not *to*\
it.
"""
change_orientation(self, orientation, self, inverse=inverse)
return self
def getNonZeroCoordinates(self, sorting=None, reverse_coord=False):
"""
This function return all the non-zero coordinates that the image contains.
Coordinate list can also be sorted by x, y, z, or the value with the parameter sorting='x', sorting='y', sorting='z' or sorting='value'
If reverse_coord is True, coordinate are sorted from larger to smaller.
Removed Coordinate object
"""
n_dim = 1
if self.dim[3] == 1:
n_dim = 3
else:
n_dim = 4
if self.dim[2] == 1:
n_dim = 2
if n_dim == 3:
X, Y, Z = (self.data > 0).nonzero()
list_coordinates = [[X[i], Y[i], Z[i], self.data[X[i], Y[i], Z[i]]] for i in range(0, len(X))]
elif n_dim == 2:
try:
X, Y = (self.data > 0).nonzero()
list_coordinates = [[X[i], Y[i], 0, self.data[X[i], Y[i]]] for i in range(0, len(X))]
except ValueError:
X, Y, Z = (self.data > 0).nonzero()
list_coordinates = [[X[i], Y[i], 0, self.data[X[i], Y[i], 0]] for i in range(0, len(X))]
if sorting is not None:
if reverse_coord not in [True, False]:
raise ValueError('reverse_coord parameter must be a boolean')
if sorting == 'x':
list_coordinates = sorted(list_coordinates, key=lambda el: el[0], reverse=reverse_coord)
elif sorting == 'y':
list_coordinates = sorted(list_coordinates, key=lambda el: el[1], reverse=reverse_coord)
elif sorting == 'z':
list_coordinates = sorted(list_coordinates, key=lambda el: el[2], reverse=reverse_coord)
elif sorting == 'value':
list_coordinates = sorted(list_coordinates, key=lambda el: el[3], reverse=reverse_coord)
else:
raise ValueError("sorting parameter must be either 'x', 'y', 'z' or 'value'")
return list_coordinates
def change_type(self, dtype):
"""
Change data type on image.
Note: the image path is voided.
"""
change_type(self, dtype, self)
return self
def fix_header_dtype(self):
"""
Change the header dtype to the match the datatype of the array.
"""
# Using bool for nibabel headers is unsupported, so use uint8 instead:
# `nibabel.spatialimages.HeaderDataError: data dtype "bool" not supported`
dtype_data = self.data.dtype
if dtype_data == bool:
dtype_data = np.uint8
dtype_header = self.hdr.get_data_dtype()
if dtype_header != dtype_data:
logger.warning(f"Image header specifies datatype '{dtype_header}', but array is of type "
f"'{dtype_data}'. Header metadata will be overwritten to use '{dtype_data}'.")
self.hdr.set_data_dtype(dtype_data)
def save(self, path=None, dtype=None, verbose=1, mutable=False):
"""
Write an image in a nifti file
:param path: Where to save the data, if None it will be taken from the\
absolutepath member.\
If path is a directory, will save to a file under this directory\
with the basename from the absolutepath member.
:param dtype: if not set, the image is saved in the same type as input data\
if 'minimize', image storage space is minimized\
(2, 'uint8', np.uint8, "NIFTI_TYPE_UINT8"),\
(4, 'int16', np.int16, "NIFTI_TYPE_INT16"),\
(8, 'int32', np.int32, "NIFTI_TYPE_INT32"),\
(16, 'float32', np.float32, "NIFTI_TYPE_FLOAT32"),\
(32, 'complex64', np.complex64, "NIFTI_TYPE_COMPLEX64"),\
(64, 'float64', np.float64, "NIFTI_TYPE_FLOAT64"),\
(256, 'int8', np.int8, "NIFTI_TYPE_INT8"),\
(512, 'uint16', np.uint16, "NIFTI_TYPE_UINT16"),\
(768, 'uint32', np.uint32, "NIFTI_TYPE_UINT32"),\
(1024,'int64', np.int64, "NIFTI_TYPE_INT64"),\
(1280, 'uint64', np.uint64, "NIFTI_TYPE_UINT64"),\
(1536, 'float128', _float128t, "NIFTI_TYPE_FLOAT128"),\
(1792, 'complex128', np.complex128, "NIFTI_TYPE_COMPLEX128"),\
(2048, 'complex256', _complex256t, "NIFTI_TYPE_COMPLEX256"),
:param mutable: whether to update members with newly created path or dtype
"""
if mutable: # do all modifications in-place
# Case 1: `path` not specified
if path is None:
if self.absolutepath: # Fallback to the original filepath
path = self.absolutepath
else:
raise ValueError("Don't know where to save the image (no absolutepath or path parameter)")
# Case 2: `path` points to an existing directory
elif os.path.isdir(path):
if self.absolutepath: # Use the original filename, but save to the directory specified by `path`
path = os.path.join(os.path.abspath(path), os.path.basename(self.absolutepath))
else:
raise ValueError("Don't know where to save the image (path parameter is dir, but absolutepath is "
"missing)")
# Case 3: `path` points to a file (or a *nonexistent* directory) so use its value as-is
# (We're okay with letting nonexistent directories slip through, because it's difficult to distinguish
# between nonexistent directories and nonexistent files. Plus, `nibabel` will catch any further errors.)
else:
pass
if os.path.isfile(path) and verbose:
logger.warning("File %s already exists. Will overwrite it.", path)
if os.path.isabs(path):
logger.debug("Saving image to %s orientation %s shape %s",
path, self.orientation, self.data.shape)
else:
logger.debug("Saving image to %s (%s) orientation %s shape %s",
path, os.path.abspath(path), self.orientation, self.data.shape)
# Now that `path` has been set and log messages have been written, we can assign it to the image itself
self.absolutepath = os.path.abspath(path)
if dtype is not None:
self.change_type(dtype)
if self.hdr is not None:
self.hdr.set_data_shape(self.data.shape)
self.fix_header_dtype()
# nb. that copy() is important because if it were a memory map, save() would corrupt it
dataobj = self.data.copy()
affine = None
header = self.hdr.copy() if self.hdr is not None else None
nib.save(nib.nifti1.Nifti1Image(dataobj, affine, header), self.absolutepath)
if not os.path.isfile(self.absolutepath):
raise RuntimeError(f"Couldn't save image to {self.absolutepath}")
else:
# if we're not operating in-place, then make any required modifications on a throw-away copy
self.copy().save(path, dtype, verbose, mutable=True)
return self
class SlicerOneAxis(object):
"""
Image slicer to use when you don't care about the 2D slice orientation,
and don't want to specify them.
The slicer will just iterate through the right axis that corresponds to
its specification.
Can help getting ranges and slice indices.
Copied from https://github.com/spinalcordtoolbox/spinalcordtoolbox/image.py
"""
def __init__(self, im, axis="IS"):
opposite_character = {'L': 'R', 'R': 'L', 'A': 'P', 'P': 'A', 'I': 'S', 'S': 'I'}
axis_labels = "LRPAIS"
if len(axis) != 2:
raise ValueError()
if axis[0] not in axis_labels:
raise ValueError()
if axis[1] not in axis_labels:
raise ValueError()
if axis[0] != opposite_character[axis[1]]:
raise ValueError()
for idx_axis in range(2):
dim_nr = im.orientation.find(axis[idx_axis])
if dim_nr != -1:
break
if dim_nr == -1:
raise ValueError()
# SCT convention
from_dir = im.orientation[dim_nr]
self.direction = +1 if axis[0] == from_dir else -1
self.nb_slices = im.dim[dim_nr]
self.im = im
self.axis = axis
self._slice = lambda idx: tuple([(idx if x in axis else slice(None)) for x in im.orientation])
def __len__(self):
return self.nb_slices
def __getitem__(self, idx):
"""
:return: an image slice, at slicing index idx
:param idx: slicing index (according to the slicing direction)
"""
if isinstance(idx, slice):
raise NotImplementedError()
if idx >= self.nb_slices:
raise IndexError("I just have {} slices!".format(self.nb_slices))
if self.direction == -1:
idx = self.nb_slices - 1 - idx
return self.im.data[self._slice(idx)]
def get_dimension(im_file, verbose=1):
"""
Copied from https://github.com/spinalcordtoolbox/spinalcordtoolbox/
Get dimension from Image or nibabel object. Manages 2D, 3D or 4D images.
:param: im_file: Image or nibabel object
:return: nx, ny, nz, nt, px, py, pz, pt
"""
if not isinstance(im_file, (nib.nifti1.Nifti1Image, Image)):
raise TypeError("The provided image file is neither a nibabel.nifti1.Nifti1Image instance nor an Image instance")
# initializating ndims [nx, ny, nz, nt] and pdims [px, py, pz, pt]
ndims = [1, 1, 1, 1]
pdims = [1, 1, 1, 1]
data_shape = im_file.header.get_data_shape()
zooms = im_file.header.get_zooms()
for i in range(min(len(data_shape), 4)):
ndims[i] = data_shape[i]
pdims[i] = zooms[i]
return *ndims, *pdims
def change_orientation(im_src, orientation, im_dst=None, inverse=False):
"""
Copied from https://github.com/spinalcordtoolbox/spinalcordtoolbox/
:param im_src: source image
:param orientation: orientation string (SCT "from" convention)
:param im_dst: destination image (can be the source image for in-place
operation, can be unset to generate one)
:param inverse: if you think backwards, use this to specify that you actually
want to transform *from* the specified orientation, not *to* it.
:return: an image with changed orientation
.. note::
- the resulting image has no path member set
- if the source image is < 3D, it is reshaped to 3D and the destination is 3D
"""
if len(im_src.data.shape) < 3:
pass # Will reshape to 3D
elif len(im_src.data.shape) == 3:
pass # OK, standard 3D volume
elif len(im_src.data.shape) == 4:
pass # OK, standard 4D volume
elif len(im_src.data.shape) == 5 and im_src.header.get_intent()[0] == "vector":
pass # OK, physical displacement field
else:
raise NotImplementedError("Don't know how to change orientation for this image")
im_src_orientation = im_src.orientation
im_dst_orientation = orientation
if inverse:
im_src_orientation, im_dst_orientation = im_dst_orientation, im_src_orientation
perm, inversion = _get_permutations(im_src_orientation, im_dst_orientation)
if im_dst is None:
im_dst = im_src.copy()
im_dst._path = None
im_src_data = im_src.data
if len(im_src_data.shape) < 3:
im_src_data = im_src_data.reshape(tuple(list(im_src_data.shape) + ([1] * (3 - len(im_src_data.shape)))))
# Update data by performing inversions and swaps
# axes inversion (flip)
data = im_src_data[::inversion[0], ::inversion[1], ::inversion[2]]
# axes manipulations (transpose)
if perm == [1, 0, 2]:
data = np.swapaxes(data, 0, 1)
elif perm == [2, 1, 0]:
data = np.swapaxes(data, 0, 2)
elif perm == [0, 2, 1]:
data = np.swapaxes(data, 1, 2)
elif perm == [2, 0, 1]:
data = np.swapaxes(data, 0, 2) # transform [2, 0, 1] to [1, 0, 2]
data = np.swapaxes(data, 0, 1) # transform [1, 0, 2] to [0, 1, 2]
elif perm == [1, 2, 0]:
data = np.swapaxes(data, 0, 2) # transform [1, 2, 0] to [0, 2, 1]
data = np.swapaxes(data, 1, 2) # transform [0, 2, 1] to [0, 1, 2]
elif perm == [0, 1, 2]:
# do nothing
pass
else:
raise NotImplementedError()
# Update header
im_src_aff = im_src.hdr.get_best_affine()
aff = nib.orientations.inv_ornt_aff(
np.array((perm, inversion)).T,
im_src_data.shape)
im_dst_aff = np.matmul(im_src_aff, aff)
im_dst.header.set_qform(im_dst_aff)
im_dst.header.set_sform(im_dst_aff)
im_dst.header.set_data_shape(data.shape)
im_dst.data = data
return im_dst
def _get_permutations(im_src_orientation, im_dst_orientation):
"""
Copied from https://github.com/spinalcordtoolbox/spinalcordtoolbox/
:param im_src_orientation str: Orientation of source image. Example: 'RPI'
:param im_dest_orientation str: Orientation of destination image. Example: 'SAL'
:return: list of axes permutations and list of inversions to achieve an orientation change
"""
opposite_character = {'L': 'R', 'R': 'L', 'A': 'P', 'P': 'A', 'I': 'S', 'S': 'I'}
perm = [0, 1, 2]
inversion = [1, 1, 1]
for i, character in enumerate(im_src_orientation):
try:
perm[i] = im_dst_orientation.index(character)
except ValueError:
perm[i] = im_dst_orientation.index(opposite_character[character])
inversion[i] = -1
return perm, inversion
def get_orientation(im):
"""
Copied from https://github.com/spinalcordtoolbox/spinalcordtoolbox/
:param im: an Image
:return: reference space string (ie. what's in Image.orientation)
"""
res = "".join(nib.orientations.aff2axcodes(im.hdr.get_best_affine()))
return orientation_string_nib2sct(res)
def orientation_string_nib2sct(s):
"""
Copied from https://github.com/spinalcordtoolbox/spinalcordtoolbox/
:return: SCT reference space code from nibabel one
"""
opposite_character = {'L': 'R', 'R': 'L', 'A': 'P', 'P': 'A', 'I': 'S', 'S': 'I'}
return "".join([opposite_character[x] for x in s])
def change_type(im_src, dtype, im_dst=None):
"""
Change the voxel type of the image
:param dtype: if not set, the image is saved in standard type\
if 'minimize', image space is minimize\
if 'minimize_int', image space is minimize and values are approximated to integers\
(2, 'uint8', np.uint8, "NIFTI_TYPE_UINT8"),\
(4, 'int16', np.int16, "NIFTI_TYPE_INT16"),\
(8, 'int32', np.int32, "NIFTI_TYPE_INT32"),\
(16, 'float32', np.float32, "NIFTI_TYPE_FLOAT32"),\
(32, 'complex64', np.complex64, "NIFTI_TYPE_COMPLEX64"),\
(64, 'float64', np.float64, "NIFTI_TYPE_FLOAT64"),\
(256, 'int8', np.int8, "NIFTI_TYPE_INT8"),\
(512, 'uint16', np.uint16, "NIFTI_TYPE_UINT16"),\
(768, 'uint32', np.uint32, "NIFTI_TYPE_UINT32"),\
(1024,'int64', np.int64, "NIFTI_TYPE_INT64"),\
(1280, 'uint64', np.uint64, "NIFTI_TYPE_UINT64"),\
(1536, 'float128', _float128t, "NIFTI_TYPE_FLOAT128"),\
(1792, 'complex128', np.complex128, "NIFTI_TYPE_COMPLEX128"),\
(2048, 'complex256', _complex256t, "NIFTI_TYPE_COMPLEX256"),
:return:
Copied from https://github.com/spinalcordtoolbox/spinalcordtoolbox/
"""
if im_dst is None:
im_dst = im_src.copy()
im_dst._path = None
if dtype is None:
return im_dst
# get min/max from input image
min_in = np.nanmin(im_src.data)
max_in = np.nanmax(im_src.data)
# find optimum type for the input image
if dtype in ('minimize', 'minimize_int'):
# warning: does not take intensity resolution into account, neither complex voxels
# check if voxel values are real or integer
isInteger = True
if dtype == 'minimize':
for vox in im_src.data.flatten():
if int(vox) != vox:
isInteger = False
break
if isInteger:
if min_in >= 0: # unsigned
if max_in <= np.iinfo(np.uint8).max:
dtype = np.uint8
elif max_in <= np.iinfo(np.uint16):
dtype = np.uint16
elif max_in <= np.iinfo(np.uint32).max:
dtype = np.uint32
elif max_in <= np.iinfo(np.uint64).max:
dtype = np.uint64
else:
raise ValueError("Maximum value of the image is to big to be represented.")
else:
if max_in <= np.iinfo(np.int8).max and min_in >= np.iinfo(np.int8).min:
dtype = np.int8
elif max_in <= np.iinfo(np.int16).max and min_in >= np.iinfo(np.int16).min:
dtype = np.int16
elif max_in <= np.iinfo(np.int32).max and min_in >= np.iinfo(np.int32).min:
dtype = np.int32
elif max_in <= np.iinfo(np.int64).max and min_in >= np.iinfo(np.int64).min:
dtype = np.int64
else:
raise ValueError("Maximum value of the image is to big to be represented.")
else:
# if max_in <= np.finfo(np.float16).max and min_in >= np.finfo(np.float16).min:
# type = 'np.float16' # not supported by nibabel
if max_in <= np.finfo(np.float32).max and min_in >= np.finfo(np.float32).min:
dtype = np.float32
elif max_in <= np.finfo(np.float64).max and min_in >= np.finfo(np.float64).min:
dtype = np.float64
dtype = to_dtype(dtype)
else:
dtype = to_dtype(dtype)
# if output type is int, check if it needs intensity rescaling
if "int" in dtype.name:
# get min/max from output type
min_out = np.iinfo(dtype).min
max_out = np.iinfo(dtype).max
# before rescaling, check if there would be an intensity overflow
if (min_in < min_out) or (max_in > max_out):
# This condition is important for binary images since we do not want to scale them
logger.warning(f"To avoid intensity overflow due to convertion to +{dtype.name}+, intensity will be rescaled to the maximum quantization scale")
# rescale intensity
data_rescaled = im_src.data * (max_out - min_out) / (max_in - min_in)
im_dst.data = data_rescaled - (data_rescaled.min() - min_out)
# change type of data in both numpy array and nifti header
im_dst.data = getattr(np, dtype.name)(im_dst.data)
im_dst.hdr.set_data_dtype(dtype)
return im_dst
def to_dtype(dtype):
"""
Take a dtypeification and return an np.dtype
:param dtype: dtypeification (string or np.dtype or None are supported for now)
:return: dtype or None
Copied from https://github.com/spinalcordtoolbox/spinalcordtoolbox/
"""
# TODO add more or filter on things supported by nibabel
if dtype is None:
return None
if isinstance(dtype, type):
if isinstance(dtype(0).dtype, np.dtype):
return dtype(0).dtype
if isinstance(dtype, np.dtype):
return dtype
if isinstance(dtype, str):
return np.dtype(dtype)
raise TypeError("data type {}: {} not understood".format(dtype.__class__, dtype))
def zeros_like(img, dtype=None):
"""
:param img: reference image
:param dtype: desired data type (optional)
:return: an Image with the same shape and header, filled with zeros
Similar to numpy.zeros_like(), the goal of the function is to show the developer's
intent and avoid doing a copy, which is slower than initialization with a constant.
Copied from https://github.com/spinalcordtoolbox/spinalcordtoolbox/image.py
"""
zimg = Image(np.zeros_like(img.data), hdr=img.hdr.copy())
if dtype is not None:
zimg.change_type(dtype)
return zimg
def empty_like(img, dtype=None):
"""
:param img: reference image
:param dtype: desired data type (optional)
:return: an Image with the same shape and header, whose data is uninitialized
Similar to numpy.empty_like(), the goal of the function is to show the developer's
intent and avoid touching the allocated memory, because it will be written to
afterwards.
Copied from https://github.com/spinalcordtoolbox/spinalcordtoolbox/image.py
"""
dst = change_type(img, dtype)
return dst
def find_zmin_zmax(im, threshold=0.1):
"""
Find the min (and max) z-slice index below which (and above which) slices only have voxels below a given threshold.
:param im: Image object
:param threshold: threshold to apply before looking for zmin/zmax, typically corresponding to noise level.
:return: [zmin, zmax]
Copied from https://github.com/spinalcordtoolbox/spinalcordtoolbox/image.py
"""
slicer = SlicerOneAxis(im, axis="IS")
# Make sure image is not empty
if not np.any(slicer):
logger.error('Input image is empty')
# Iterate from bottom to top until we find data
for zmin in range(0, len(slicer)):
if np.any(slicer[zmin] > threshold):
break
# Conversely from top to bottom
for zmax in range(len(slicer) - 1, zmin, -1):
if np.any(slicer[zmax] > threshold):
break
return zmin, zmax