Comments (8)
Hello @sedghi ,
I remembered that option 1 worked for me in the past, but just to confirm I wrote the following code:
import SimpleITK as sitk
import numpy as np
# Create the transformation (when working with images it is easier to use the BSplineTransformInitializer function
# or its object oriented counterpart BSplineTransformInitializerFilter).
dimension = 3
spline_order = 3
direction_matrix_row_major = [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0] # identity, mesh is axis aligned
origin = [-1.0,-1.0,-1.0]
domain_physical_dimensions = [2,2,2]
bspline = sitk.BSplineTransform(dimension, spline_order)
bspline.SetTransformDomainOrigin(origin)
bspline.SetTransformDomainDirection(direction_matrix_row_major)
bspline.SetTransformDomainPhysicalDimensions(domain_physical_dimensions)
bspline.SetTransformDomainMeshSize((3,4,5))
# Random displacement of the control points.
original_parameters = np.random.random(len(bspline.GetParameters()))
bspline.SetParameters(original_parameters)
image_0, image_1, image_2 = bspline.GetCoefficientImages()
retrieved_parameters = list(sitk.GetArrayViewFromImage(image_0).flatten()) + list(sitk.GetArrayViewFromImage(image_1).flatten()) + list(sitk.GetArrayViewFromImage(image_2).flatten())
print(np.max(original_parameters - retrieved_parameters))
Note that the code uses GetArrayView and not GetArray, no need to copy the data, just view it as a numpy array.
Hopefully this resolves your question.
from simpleitk.
Thank you for the feed back.
There certainly should be a way to set the bulk data of a BSpline from an image. I thought there was an initializer which did that, but I can't find it. I'll look into this further.
from simpleitk.
@zivy Do you have a proposed interface for this requested feature?
from simpleitk.
@blowekamp I am against supporting the SetCoefficientImages() API.
The reasoning is based on the ITK documentation: "Warning: use either the SetParameters() or SetCoefficientImages() API. Mixing the two modes may results in unexpected results."
Using images to do the initialization is convenient, but it is trivial to move from an image to a vector of coefficients:
R - c(as.array(img))
Python - (sitk.GetArrayFromImage(img)).flatten()
from simpleitk.
This interface is really needed to be able to resample the BSpline transforms. Perhaps we should add a constructor which takes the CoefficientImages?
from simpleitk.
closed by 595
from simpleitk.
@zivy @blowekamp
Regarding your solution to set the parameters with flattening the arrays:
Do we need to take into account that the GetArrayFromImage()
returns the swapped x, z axis?
Which of the following codes are correct?
1:
image_0, image_1, image_2 = bspline_tx.GetCoefficientImages()
image_0_arr = sitk.GetArrayFromImage(image_0)
image_1_arr = sitk.GetArrayFromImage(image_1)
image_2_arr = sitk.GetArrayFromImage(image_2)
aggregated_params = (
list(image_0_arr.flatten())
+ list(image_1_arr.flatten())
+ list(image_2_arr.flatten())
)
bspline_tx.SetParameters(aggregated_params)
Or 2:
image_0, image_1, image_2 = bspline_tx.GetCoefficientImages()
image_0_arr = np.swapaxes(sitk.GetArrayFromImage(image_0), 0, 2)
image_1_arr = np.swapaxes(sitk.GetArrayFromImage(image_1), 0, 2)
image_2_arr = np.swapaxes(sitk.GetArrayFromImage(image_2), 0, 2)
aggregated_params = (
list(image_0_arr.flatten())
+ list(image_1_arr.flatten())
+ list(image_2_arr.flatten())
)
bspline_tx.SetParameters(aggregated_params)
from simpleitk.
@zivy Thank you for your answer. GetArrayView is indeed better
from simpleitk.
Related Issues (20)
- PEP 688 add buffer protocol support
- After registration, is the order of transforms composition specified in the documentation correct? HOT 4
- `MetaData dictionary key must be str`? HOT 2
- simpleITK python acts differently while reading metadata with upper or lower case hexadecimal label HOT 5
- Figure illustrating image concepts doesn't have clear text HOT 5
- ThresholdImage filter in Python HOT 2
- Add way to search docs
- Add docs on how to set/get the mask on an image HOT 1
- Provide reference documentation for transform parameters HOT 1
- Groupwise registration of 4D objects. Bad direction, determinant is 0. Direction is 1 0 0 HOT 1
- How to convert DisplacementFieldTransform to Image format and save it? HOT 1
- There was a problem merging two.NIIs into one
- There was a problem merging two.NIIs into one HOT 6
- Some special characters in metadata are truncated HOT 2
- Changing number of iterations per level of registration HOT 1
- Race condition in ErodeObjectMorphologyImageFilter HOT 2
- Incorrect resampling result with SimpleITK.sitkLinear Interpolator option HOT 4
- Update Elastix SuperBuild version HOT 2
- Linux availability of native library for java HOT 2
- Dicom series loaded with incorrect origin and spacing HOT 11
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from simpleitk.