Python: git clone https://github.com/ANTsX/ANTsPy; cd ANTsPy; python3 setup.py install
R: devtools::install_github( "ANTsX/ANTsR" )
Read, write, and visualize images
>>> import ants
>>>
>>> r16_filename = ants.get_ants_data("r16")
>>> r16_image = ants.image_read(r16_filename)
>>>
>>> ants.write_image(r16_image, "r16.nii.gz")
>>>
>>> ants.plot( r16_image )
> library( ANTsR )
>
> r16Filename <- getANTsRData( "r16" )
> r16Image <- antsImageRead( r16Filename )
>
> antsImageWrite( r16Image, "r16.nii.gz" )
>
> plot( r16Image )
>>> import ants
>>>
>>> r16 = ants.image_read(ants.get_ants_data('r16'))
>>> r16
ANTsImage
Pixel Type : float (float32)
Components : 1
Dimensions : (256, 256)
Spacing : (1.0, 1.0)
Origin : (0.0, 0.0)
Direction : [1. 0. 0. 1.]
>>> r16.shape
(256, 256)
>>> r16.origin
(0.0, 0.0)
>>> ants.get_origin(r16)
(0.0, 0.0)
>>> r16.direction
array([[1., 0.],
[0., 1.]])
>>> ants.get_direction(r16)
array([[1., 0.],
[0., 1.]])
>>> r16.spacing
(1.0, 1.0)
>>> ants.get_spacing(r16)
(1.0, 1.0)
> library( ANTsR )
>
> r16 <- antsImageRead( getANTsRData( 'r16' ) )
> r16
antsImage
Pixel Type : float
Components Per Pixel: 1
Dimensions : 256x256
Voxel Spacing : 1x1
Origin : 0 0
Direction : 1 0 0 1
Filename : /Library/Frameworks/R.framework/Versions/4.0/Resources/library/ANTsRCore/extdata/r16slice.jpg
> dim( r16 )
[1] 256 256
> antsGetOrigin( r16 )
[1] 0 0
> antsGetDirection( r16 )
[,1] [,2]
[1,] 1 0
[2,] 0 1
> antsGetSpacing( r16 )
[1] 1 1
Conversion to/from native types
>>> import ants
>>>
>>> r16 = ants.image_read(ants.get_ants_data('r16'))
>>> r16_numpy = r16.numpy()
>>> r16_numpy
array([[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
...,
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.]], dtype=float32)
>>> r16_image = ants.from_numpy(r16_numpy, origin=r16.origin, spacing=r16.spacing, direction=r16.direction)
> library( ANTsR )
>
> r16 <- antsImageRead( getANTsRData( 'r16' ) )
> r16Array <- as.array( r16 )
> r16Image <- as.antsImage( r16Array, spacing = antsGetSpacing( r16 ), origin = antsGetOrigin( r16 ), direction = antsGetDirection( r16 ) )
> r16Image <- as.antsImage( r16Array, reference = r16 )
>>> import ants
>>>
>>> r16 = ants.image_read(ants.get_ants_data('r16'))
>>> r64 = ants.image_read(ants.get_ants_data('r64'))
>>>
>>> r_x = r16 + r64
>>> r_x = r16 - r64
>>> r_x = r16 * r64
>>> r_x = r16 / r64
>>>
>>> r_x = r16 + 3.14
>>> r_x = r16 - 3.14
>>> r_x = r16 * 3.14
>>> r_x = r16 / 3.14
>>> r_x = r16 ** 3.14
> library( ANTsR )
>
> r16 <- antsImageRead( getANTsRData( "r16" ) )
> r64 <- antsImageRead( getANTsRData( "r64" ) )
>
> rx <- r16 + r64
> rx <- r16 - r64
> rx <- r16 * r64
> rx <- r16 / r64
>
> rx <- r16 + 3.14
> rx <- r16 - 3.14
> rx <- r16 * 3.14
> rx <- r16 / 3.14
> rx <- r16 ^ 3.14
>>> import ants
>>>
>>> r16 = ants.image_read(ants.get_ants_data('r16'))
>>>
>>> r16.min()
0.0
>>> r16.max()
254.0
>>> r16.std()
84.8173
>>> r16.mean()
50.461365
> library( ANTsR )
>
> r16 <- antsImageRead( getANTsRData( "r16" ) )
>
> min( r16 )
[1] 0
> max( r16 )
[1] 254
> sd( r16 )
[1] 84.81795
> mean( r16 )
[1] 50.46136
>>> import ants
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>>
>>> x = np.linspace(-4, 4, num=100)
>>> y = np.exp(-np.multiply(x, x)) + np.random.uniform(-0.1, 0.1, len(x))
>>> u = np.linspace(0, 1.0, num=len(x))
>>> scattered_data = np.column_stack((x, y))
>>> parametric_data = np.expand_dims(u, axis=-1)
>>> spacing = 1/(len(x)-1)
>>> bspline_curve = ants.fit_bspline_object_to_scattered_data(scattered_data,
... parametric_data,
... parametric_domain_origin=[0.0], parametric_domain_spacing=[spacing],
... parametric_domain_size=[len(x)], is_parametric_dimension_closed=None,
... number_of_fitting_levels=5, mesh_size=1)
>>> plt.plot(x, y, 'o', label='Noisy points')
[<matplotlib.lines.Line2D object at 0x31b572810>]
>>> plt.plot(bspline_curve[:,0], bspline_curve[:,1], label='B-spline curve')
[<matplotlib.lines.Line2D object at 0x31b53b200>]
>>> plt.grid(True)
>>> plt.axis('tight')
(-4.4, 4.4, -0.1478762733173202, 1.02880490107376)
>>> plt.legend(loc='upper left')
<matplotlib.legend.Legend object at 0x31b235340>
>>> plt.show()
2025-02-17 10:16:47.919 python3[17382:170684] +[IMKClient subclass]: chose IMKClient_Modern
2025-02-17 10:16:47.919 python3[17382:170684] +[IMKInputSession subclass]: chose IMKInputSession_Modern
>>>
>>> # 2-D closed curve example (hypocycloid)
>>>
>>> number_of_sample_points = 100
>>> delta = 1/(number_of_sample_points)
>>> u = np.linspace(0, 1.0-delta, num=number_of_sample_points)
>>> x = 9 * np.cos(u * 2 * np.pi) + 3 * np.cos(3 * u * 2 * np.pi) + np.random.uniform(-1, 1, len(u))
>>> y = 9 * np.sin(u * 2 * np.pi) - 3 * np.sin(3 * u * 2 * np.pi) + np.random.uniform(-1, 1, len(u))
>>> scattered_data = np.column_stack((x, y))
>>> parametric_data = np.expand_dims(u, axis=-1)
>>> spacing = 1/(len(x)-1)
>>> bspline_curve = ants.fit_bspline_object_to_scattered_data(scattered_data,
... parametric_data,
... parametric_domain_origin=[0.0], parametric_domain_spacing=[spacing],
... parametric_domain_size=[len(x)], is_parametric_dimension_closed=[True],
... number_of_fitting_levels=3, mesh_size=4)
>>> plt.plot(x, y, 'o', label='Noisy points')
[<matplotlib.lines.Line2D object at 0x32484d0d0>]
>>> plt.plot(bspline_curve[:,0], bspline_curve[:,1], label='B-spline curve')
[<matplotlib.lines.Line2D object at 0x3248b1550>]
>>> plt.grid(True)
>>> plt.axis('tight')
(-14.009181683424856, 14.060594273893225, -13.874089327839798, 12.798694412376495)
>>> plt.legend(loc='upper left')
<matplotlib.legend.Legend object at 0x32483fd70>
>>> plt.show()
>>>
>>> # 3-D B-spline surface of a trefoil knot
>>>
>>> import ants
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>>
>>> uv = np.meshgrid(np.linspace(-2.0 * np.pi, 2.0 * np.pi, 100),
... np.linspace(-1.0 * np.pi, 1.0 * np.pi, 100))
>>> uR = uv[0].flatten()
>>> vR = uv[1].flatten()
>>>
>>> u = (uR + 2.0 * np.pi) / (4.0 * np.pi)
>>> v = (vR + 1.0 * np.pi) / (2.0 * np.pi)
>>> spacing = 1/99
>>>
>>> x = np.cos(uR) * np.cos(vR) + 3.0 * np.cos(uR) * (1.5 + 0.5 * np.sin(1.5 * uR))
>>> y = np.sin(uR) * np.cos(vR) + 3.0 * np.sin(uR) * (1.5 + 0.5 * np.sin(1.5 * uR))
>>> z = np.sin(vR) + 2.0 * np.cos(1.5 * uR)
>>>
>>> scattered_data_x = np.expand_dims(x, axis=-1)
>>> scattered_data_y = np.expand_dims(y, axis=-1)
>>> scattered_data_z = np.expand_dims(z, axis=-1)
>>>
>>> parametric_data = np.column_stack((u, v))
>>>
>>> bspline_mesh_x = ants.fit_bspline_object_to_scattered_data(
... scattered_data_x, parametric_data,
... parametric_domain_origin=[0.0, 0.0], parametric_domain_spacing=[0.1 * spacing, spacing],
... parametric_domain_size=[1000, 100], is_parametric_dimension_closed=[True, True],
... number_of_fitting_levels=4, mesh_size=[4, 7])
>>> bspline_mesh_y = ants.fit_bspline_object_to_scattered_data(
... scattered_data_y, parametric_data,
... parametric_domain_origin=[0.0, 0.0], parametric_domain_spacing=[0.1 * spacing, spacing],
... parametric_domain_size=[1000, 100], is_parametric_dimension_closed=[True, True],
... number_of_fitting_levels=4, mesh_size=[4, 7])
>>> bspline_mesh_z = ants.fit_bspline_object_to_scattered_data(
... scattered_data_z, parametric_data,
... parametric_domain_origin=[0.0, 0.0], parametric_domain_spacing=[0.1 * spacing, spacing],
... parametric_domain_size=[1000, 100], is_parametric_dimension_closed=[True, True],
... number_of_fitting_levels=4, mesh_size=[4, 7])
>>>
>>> xx = bspline_mesh_x.numpy()
>>> yy = bspline_mesh_y.numpy()
>>> zz = bspline_mesh_z.numpy()
>>>
>>> fig = plt.figure()
>>> ax = fig.add_subplot(111, projection='3d')
>>> ax.plot_surface(xx, yy, zz)
<mpl_toolkits.mplot3d.art3d.Poly3DCollection object at 0x3248b26c0>
>>> plt.show()
>>>
>>> # 2-D B-spline image approximation
>>>
>>> number_of_random_points = 10000
>>> img = ants.image_read( ants.get_ants_data("r16"))
>>> img_array = img.numpy()
>>> row_indices = np.random.choice(range(2, img_array.shape[0]), number_of_random_points)
>>> col_indices = np.random.choice(range(2, img_array.shape[1]), number_of_random_points)
>>> scattered_data = np.zeros((number_of_random_points, 1))
>>> parametric_data = np.zeros((number_of_random_points, 2))
>>>
>>> for i in range(number_of_random_points):
... scattered_data[i,0] = img_array[row_indices[i], col_indices[i]]
... parametric_data[i,0] = row_indices[i]
... parametric_data[i,1] = col_indices[i]
...
>>> bspline_img = ants.fit_bspline_object_to_scattered_data(
... scattered_data, parametric_data,
... parametric_domain_origin=[0.0, 0.0],
... parametric_domain_spacing=[1.0, 1.0],
... parametric_domain_size = img.shape,
... number_of_fitting_levels=7, mesh_size=1)
>>>
>>> f, ax = plt.subplots(1, 2)
>>> ax[0].imshow(np.rot90(img.numpy(), 1), cmap='gray')
<matplotlib.image.AxesImage object at 0x3672018e0>
>>> ax[0].set_title('Original image')
Text(0.5, 1.0, 'Original image')
>>> ax[0].set_xticklabels([])
[Text(-100.0, 0, ''), Text(0.0, 0, ''), Text(100.0, 0, ''), Text(200.0, 0, ''), Text(300.0, 0, '')]
>>> ax[0].set_yticklabels([])
[Text(0, -50.0, ''), Text(0, 0.0, ''), Text(0, 50.0, ''), Text(0, 100.0, ''), Text(0, 150.0, ''), Text(0, 200.0, ''), Text(0, 250.0, ''), Text(0, 300.0, '')]
>>> ax[1].imshow(np.rot90(bspline_img.numpy(), 1), cmap='gray')
<matplotlib.image.AxesImage object at 0x36725c320>
>>> ax[1].set_title('B-spline image')
Text(0.5, 1.0, 'B-spline image')
>>> ax[1].set_xticklabels([])
[Text(-100.0, 0, ''), Text(0.0, 0, ''), Text(100.0, 0, ''), Text(200.0, 0, ''), Text(300.0, 0, '')]
>>> ax[1].set_yticklabels([])
[Text(0, -50.0, ''), Text(0, 0.0, ''), Text(0, 50.0, ''), Text(0, 100.0, ''), Text(0, 150.0, ''), Text(0, 200.0, ''), Text(0, 250.0, ''), Text(0, 300.0, '')]
>>> plt.show()
> # 2-D curve example
>
> x <- seq( from = -4, to = 4, by = 0.1 )
> y <- exp( -( x * x ) ) + runif( length( x ), min = -0.1, max = 0.1 )
> u <- seq( from = 0.0, to = 1.0, length.out = length( x ) )
> scatteredData <- cbind( x, y )
> parametricData <- as.matrix( u, ncol = 1 )
> numberOfSamplePoints <- 100
> spacing <- 1 / ( numberOfSamplePoints - 1 )
> bsplineCurve <- fitBsplineObjectToScatteredData( scatteredData, parametricData,
+ parametricDomainOrigin = c( 0.0 ), parametricDomainSpacing = c( spacing ),
+ parametricDomainSize = c( numberOfSamplePoints ), isParametricDimensionClosed = c( FALSE ),
+ numberOfFittingLevels = 5, meshSize = 1 )
> plot( x, y, "p", col = "red" )
> points( scatteredData[, 1], scatteredData[, 2], col = "green" )
> lines( bsplineCurve[, 1], bsplineCurve[, 2], col = "blue" )
> legend( "topleft", legend = c( "Noisy points", "B-spline curve" ),
+ pch = c( 'o', '-' ), col = c( "green", "blue" ) )
>
> # 2-D closed curve example (hypocycloid)
>
> numberOfSamplePoints <- 100
> delta <- 1 / numberOfSamplePoints
> u <- seq(from = 0.0, to = 1.0 - delta, by = delta)
> x <- 9 * cos(u * 2 * pi) + 3 * cos(3 * u * 2 * pi) + runif( length( u ), min = -1, max = 1 )
> y <- 9 * sin(u * 2 * pi) - 3 * sin(3 * u * 2 * pi) + runif( length( u ), min = -1, max = 1 )
> scatteredData <- cbind( x, y )
> parametricData <- as.matrix( u, ncol = 1 )
> spacing <- 1 / ( numberOfSamplePoints - 1 )
> bsplineCurve <- fitBsplineObjectToScatteredData(scatteredData, parametricData,
+ parametricDomainOrigin = c( 0.0 ), parametricDomainSpacing = c( spacing ),
+ parametricDomainSize = c( numberOfSamplePoints ), isParametricDimensionClosed = c( TRUE ),
+ numberOfFittingLevels = 3, meshSize = 4 )
> plot( x, y, "p", col = "red" )
> points( scatteredData[, 1], scatteredData[, 2], col = "green" )
> lines( bsplineCurve[, 1], bsplineCurve[, 2], col = "blue" )
> legend( "topleft", legend = c("Noisy points", "B-spline-curve" ),
+ pch = c( 'o', '-' ), col = c( "green", "blue" ) )
>
> # 3-D B-spline surface of a trefoil knot
>
> library( plot3D )
>
> meshGrid <- function( u, v )
+ {
+ uv <- as.vector( t( outer( u, rep( 1, length( v ) ) ) ) )
+ vu <- as.vector( t( outer( rep( 1, length( u ) ), v ) ) )
+ return( list( uv, vu ) )
+ }
>
> uv <- meshGrid( seq( from = -2.0 * pi, to = 2.0 * pi, by = 0.1 ),
+ seq( from = -1.0 * pi, to = 1.0 * pi, by = 0.1 ) )
> uR <- uv[[1]]
> vR <- uv[[2]]
>
> u <- ( uR + 2.0 * pi ) / ( 4.0 * pi )
> v <- ( vR + 1.0 * pi ) / ( 2.0 * pi )
>
> x <- cos( uR ) * cos( vR ) + 3.0 * cos( uR ) * ( 1.5 + 0.5 * sin( 1.5 * uR ) )
> y <- sin( uR ) * cos( vR ) + 3.0 * sin( uR ) * ( 1.5 + 0.5 * sin( 1.5 * uR ) )
> z <- sin( vR ) + 2.0 * cos( 1.5 * uR )
>
> scatteredDataX <- as.matrix( x, ncol = 1 )
> scatteredDataY <- as.matrix( y, ncol = 1 )
> scatteredDataZ <- as.matrix( z, ncol = 1 )
>
> parametricData <- cbind( u, v )
>
> bsplineMeshX <- fitBsplineObjectToScatteredData(scatteredDataX, parametricData,
+ parametricDomainOrigin = c( 0.0, 0.0 ), parametricDomainSpacing = c( 0.001, 0.01 ),
+ parametricDomainSize = c( 1000, 100 ), isParametricDimensionClosed = c( TRUE, TRUE ),
+ numberOfFittingLevels = 4, meshSize = c( 4, 7 ) )
> bsplineMeshY <- fitBsplineObjectToScatteredData(scatteredDataY, parametricData,
+ parametricDomainOrigin = c( 0.0, 0.0 ), parametricDomainSpacing = c( 0.001, 0.01 ),
+ parametricDomainSize = c( 1000, 100 ), isParametricDimensionClosed = c( TRUE, TRUE ),
+ numberOfFittingLevels = 4, meshSize = c( 4, 7 ) )
> bsplineMeshZ <- fitBsplineObjectToScatteredData(scatteredDataZ, parametricData,
+ parametricDomainOrigin = c( 0.0, 0.0 ), parametricDomainSpacing = c( 0.001, 0.01 ),
+ parametricDomainSize = c( 1000, 100 ), isParametricDimensionClosed = c( TRUE, TRUE ),
+ numberOfFittingLevels = 4, meshSize = c( 4, 7 ) )
>
> xx <- as.array( bsplineMeshX )
> yy <- as.array( bsplineMeshY )
> zz <- as.array( bsplineMeshZ )
>
> surf3D( x = xx, y = yy, z = zz )
>
> # 2-D B-spline image approximation
>
> numberOfRandomPoints <- 10000
> img <- antsImageRead( getANTsRData( "r16" ) )
> imgArray <- as.array( img )
> rowIndices <- sample( 2:( dim( imgArray)[1] - 1 ), numberOfRandomPoints, replace = TRUE )
> colIndices <- sample( 2:( dim( imgArray)[2] - 1 ), numberOfRandomPoints, replace = TRUE )
>
> scatteredData <- as.matrix( array( data = 0, dim = c( numberOfRandomPoints, 1 ) ) )
> parametricData <- as.matrix( array( data = 0, dim = c( numberOfRandomPoints, 2 ) ) )
> for( i in seq_len( numberOfRandomPoints ) )
+ {
+ scatteredData[i, 1] <- imgArray[rowIndices[i], colIndices[i]]
+ parametricData[i, 1] <- rowIndices[i]
+ parametricData[i, 2] <- colIndices[i]
+ }
>
> bsplineImage <- fitBsplineObjectToScatteredData( scatteredData, parametricData,
+ parametricDomainOrigin = c( 0.0, 0.0 ), parametricDomainSpacing = c( 1.0, 1.0 ),
+ parametricDomainSize = dim( img ),
+ numberOfFittingLevels = 7, meshSize = c( 1, 1 ) )
>
> img <- iMath( img, operation = "Normalize" )
> bsplineImage <- iMath( bsplineImage, operation = "Normalize" )
>
> nf <- layout( matrix( c( 1, 2 ), ncol = 2 ), 1, 1 )
> image( as.array( img ), col = gray.colors( 100, start = 0.0, end = 1.0 ), axes = FALSE )
> title( main = "Original image", font.main = 4 )
> image( as.array( bsplineImage ), col = gray.colors( 100, start = 0.0, end = 1.0 ), axes = FALSE )
> title( main = "B-spline image", font.main = 4 )