Description Usage Arguments Author(s) Examples
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.
1 | writeANTsImageToNumpy(img, fn)
|
img |
input antsImage |
fn |
output filename (probably a .npy extension) |
Avants BB
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
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.