Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions brainles_preprocessing/registration/ANTs/ANTs.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,24 +101,30 @@ def register(

fixed_image = ants.image_read(str(fixed_image_path))
moving_image = ants.image_read(str(moving_image_path))

registration_result = ants.registration(
fixed=fixed_image,
moving=moving_image,
**registration_kwargs,
)
transformed_image = registration_result["warpedmovout"]

# Ensure output directories exist
transformed_image_path.parent.mkdir(parents=True, exist_ok=True)
matrix_path.parent.mkdir(parents=True, exist_ok=True)

ants.image_write(transformed_image, str(transformed_image_path))

shutil.copyfile(
src=registration_result["fwdtransforms"][0],
dst=str(matrix_path),
)

self.transform(
fixed_image_path=fixed_image_path,
moving_image_path=moving_image_path,
transformed_image_path=transformed_image_path,
matrix_path=matrix_path,
log_file_path=log_file_path,
)

end_time = datetime.datetime.now()

# TODO nicer logging
Expand Down Expand Up @@ -147,6 +153,7 @@ def transform(
) -> None:
"""
Apply a transformation using ANTs.
By default the padding value corresponds to the minimum of the moving image. Can be adjusted using the defaultvalue parameter.

Args:
fixed_image_path (str or Path): Path to the fixed image.
Expand Down Expand Up @@ -195,6 +202,11 @@ def transform(
fixed_image = ants.image_read(str(fixed_image_path))
moving_image = ants.image_read(str(moving_image_path))

if "defaultvalue" not in transform_kwargs:
transform_kwargs["defaultvalue"] = (
moving_image.min()
) # set out of view voxels by default to min value, i.e. air/ bg

# Ensure output directory exist
transformed_image_path.parent.mkdir(parents=True, exist_ok=True)

Expand Down
12 changes: 11 additions & 1 deletion brainles_preprocessing/registration/niftyreg/niftyreg.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import numpy as np
from auxiliary.runscript import ScriptRunner
from auxiliary.turbopath import turbopath
from auxiliary.io import read_image

from brainles_preprocessing.registration.registrator import Registrator

Expand Down Expand Up @@ -79,12 +80,16 @@ def register(

matrix_path = Path(matrix_path).with_suffix(".txt")

# read moving image to get padding value
padding_value = float(read_image(moving_image_path).min())

input_params = [
turbopath(niftyreg_executable),
turbopath(fixed_image_path),
turbopath(moving_image_path),
turbopath(transformed_image_path),
str(matrix_path),
str(padding_value),
]

# Call the run method to execute the script and capture the output in the log file
Expand Down Expand Up @@ -155,6 +160,7 @@ def transform(
) -> None:
"""
Apply a transformation using NiftyReg.
By default the padding value corresponds to the minimum of the moving image.

Args:
fixed_image_path (str): Path to the fixed image.
Expand Down Expand Up @@ -199,13 +205,17 @@ def transform(
else:
transform_path = Path(matrix_path).with_suffix(".txt")

# read moving image to get padding value
padding_value = float(read_image(moving_image_path).min())

input_params = [
turbopath(niftyreg_executable),
turbopath(fixed_image_path),
turbopath(moving_image_path),
turbopath(transformed_image_path),
str(transform_path),
interpolator, # interpolation method, 3 is Cubic
str(interpolator), # interpolation method, 3 is Cubic
str(padding_value),
]

# Call the run method to execute the script and capture the output in the log file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ file_exists() {
}

# Check if the correct number of arguments is provided
if [ "$#" -ne 5 ]; then
echo "Usage: $0 <niftyreg_executable> <fixed_image> <moving_image> <transformed_image> <transformation_matrix>"
if [ "$#" -ne 6 ]; then
echo "Usage: $0 <niftyreg_executable> <fixed_image> <moving_image> <transformed_image> <transformation_matrix> <padding_value>"
exit 1
fi

Expand All @@ -21,6 +21,7 @@ fixed_image="$2"
moving_image="$3"
transformed_image="$4"
transformation_matrix="$5"
padding_value="$6"

# Validate the existence of input files
if ! file_exists "$fixed_image"; then
Expand All @@ -41,6 +42,7 @@ registration_options=(
"-flo" "$moving_image"
"-res" "$transformed_image"
"-aff" "$transformation_matrix"
"-pad" "$padding_value"
)

# Perform rigid-only registration with NiftyReg
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ file_exists() {
}

# Check if the correct number of arguments is provided
if [ "$#" -ne 6 ]; then
echo "Usage: $0 <niftyreg_executable> <fixed_image> <moving_image> <transformed_image> <transformation_matrix> <interpolation_method>"
if [ "$#" -ne 7 ]; then
echo "Usage: $0 <niftyreg_executable> <fixed_image> <moving_image> <transformed_image> <transformation_matrix> <interpolation_method> <padding_value>"
exit 1
fi

Expand All @@ -22,6 +22,7 @@ moving_image="$3"
transformed_image="$4"
transformation_matrix="$5"
interpolation_method="$6"
padding_value="$7"

# Validate the existence of input files
if ! file_exists "$fixed_image"; then
Expand Down Expand Up @@ -51,6 +52,7 @@ resample_command=(
-trans "$transformation_matrix"
-res "$transformed_image"
-inter "$interpolation_method"
-pad "$padding_value"
)

if file_exists "$niftyreg_path"; then
Expand Down