Git Product home page Git Product logo

Comments (8)

zivy avatar zivy commented on June 11, 2024 2

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.

blowekamp avatar blowekamp commented on June 11, 2024 1

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.

blowekamp avatar blowekamp commented on June 11, 2024

@zivy Do you have a proposed interface for this requested feature?

from simpleitk.

zivy avatar zivy commented on June 11, 2024

@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.

blowekamp avatar blowekamp commented on June 11, 2024

@zivy

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.

blowekamp avatar blowekamp commented on June 11, 2024

closed by 595

from simpleitk.

sedghi avatar sedghi commented on June 11, 2024

@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.

sedghi avatar sedghi commented on June 11, 2024

@zivy Thank you for your answer. GetArrayView is indeed better

from simpleitk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.