writeANTsImageToNumpy: writeANTsImageToNumpy

Description Usage Arguments Author(s) Examples

Description

Write antsImage type to numpy array. This will produce a vector for a single channel image and a matrix for a multi-channel image. We do not reorder the data to match R and numpy indexing.

Usage

1

Arguments

img

input antsImage

fn

output filename (probably a .npy extension)

Author(s)

Avants BB

Examples

 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
img = ANTsR::antsImageRead( ANTsR::getANTsRData( "r16" ) )
ofn = tempfile( fileext=".npy" )
writeANTsImageToNumpy( img, ofn )
img = ANTsR::makeImage( c( 5, 7 ), rnorm( 35 ) )
writeANTsImageToNumpy( img, ofn )
# to read in python, do
# from PIL import Image
# import numpy as np
# from scipy.misc import toimage
# data = np.load( ofn )
# array = np.reshape( data, [ 7,5] )
# toimage(array).show()
img = ANTsR::makeImage( c( 5, 6, 7 ), rnorm( 5*6*7) )
writeANTsImageToNumpy( img, ofn )
# to read in python, do
# from PIL import Image
# import numpy as np
# from scipy.misc import toimage
# data = np.load( ofn )
# array = np.reshape( data, [ 7, 6, 5 ] )
# then we have
# array[ 3, 1, 4 ] # index in python
# as.array( img )[ 5, 2, 4 ] # index in R
# so the python indices are in reverse order and minus one compared to R
# toimage(array[:,:,2]).show()

# to read in python, do
# from PIL import Image
# import numpy as np
# n = 256
# data = np.load( ofn )
# array = np.reshape( data, [n, n] )
# from scipy.misc import toimage
# toimage(data).show()

# now a multi-channel example
i1 = ANTsR::makeImage( c( 5, 6, 7 ), rnorm( 5*6*7) )
i2 = ANTsR::makeImage( c( 5, 6, 7 ), rnorm( 5*6*7) )
img = mergeChannels( list( i1, i2 ) )
writeANTsImageToNumpy( img, ofn )
# now to read this in python, follow the examples above but each channel
# corresponds to each row in the numpy matrix

stnava/ANTsRNpy documentation built on May 30, 2019, 7:20 p.m.