MCPcopy Create free account
hub / github.com/SimpleITK/SimpleITK / GetImageFromArray

Function GetImageFromArray

Wrapping/Python/SimpleITK/extra.py:284–319  ·  view source on GitHub ↗

Get a SimpleITK Image from a numpy array. If isVector is True, then the Image will have a Vector pixel type, and the last dimension of the array will be considered the component index. By default when isVector is None, 4D arrays are automatically considered 3D vector images, but 3D arra

(arr: "numpy.ndarray", isVector: Optional[bool] = None)

Source from the content-addressed store, hash-verified

282
283
284def GetImageFromArray(arr: "numpy.ndarray", isVector: Optional[bool] = None) -> Image:
285 """Get a SimpleITK Image from a numpy array.
286
287 If isVector is True, then the Image will have a Vector pixel type, and the last dimension of the array will be
288 considered the component index. By default when isVector is None, 4D arrays
289 are automatically considered 3D vector images, but 3D arrays are 3D images.
290 """
291
292 if not HAVE_NUMPY:
293 raise ImportError("Numpy not available.")
294
295 z = numpy.asarray(arr)
296
297 if isVector is None:
298 if z.ndim == 4 and z.dtype != numpy.complex64 and z.dtype != numpy.complex128:
299 isVector = True
300
301 if isVector:
302 id = _get_sitk_vector_pixelid(z)
303 if z.ndim > 2:
304 number_of_components = z.shape[-1]
305 shape = z.shape[-2::-1]
306 else:
307 number_of_components = 1
308 shape = z.shape[::-1]
309 else:
310 number_of_components = 1
311 id = _get_sitk_pixelid(z)
312 shape = z.shape[::-1]
313
314 # SimpleITK throws an exception if the image dimension is not supported
315 img = Image(shape, id, number_of_components)
316
317 _SetImageFromArray(z, img)
318
319 return img
320
321
322def ReadImage(

Callers

nothing calls this directly

Calls 3

_get_sitk_vector_pixelidFunction · 0.85
_get_sitk_pixelidFunction · 0.85
ImageClass · 0.50

Tested by

no test coverage detected