Skip to content

Commit 1d6f546

Browse files
authored
Merge pull request #158 from BrainLesion/153-bug-defaultvalue-for-padding-when-registering-without-skull-stripping-seems-to-be-off
153 bug defaultvalue for padding when registering without skull stripping seems to be off
2 parents e495aa4 + 829fc32 commit 1d6f546

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

brainles_preprocessing/registration/ANTs/ANTs.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,24 +101,30 @@ def register(
101101

102102
fixed_image = ants.image_read(str(fixed_image_path))
103103
moving_image = ants.image_read(str(moving_image_path))
104+
104105
registration_result = ants.registration(
105106
fixed=fixed_image,
106107
moving=moving_image,
107108
**registration_kwargs,
108109
)
109-
transformed_image = registration_result["warpedmovout"]
110110

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

115-
ants.image_write(transformed_image, str(transformed_image_path))
116-
117115
shutil.copyfile(
118116
src=registration_result["fwdtransforms"][0],
119117
dst=str(matrix_path),
120118
)
121119

120+
self.transform(
121+
fixed_image_path=fixed_image_path,
122+
moving_image_path=moving_image_path,
123+
transformed_image_path=transformed_image_path,
124+
matrix_path=matrix_path,
125+
log_file_path=log_file_path,
126+
)
127+
122128
end_time = datetime.datetime.now()
123129

124130
# TODO nicer logging
@@ -147,6 +153,7 @@ def transform(
147153
) -> None:
148154
"""
149155
Apply a transformation using ANTs.
156+
By default the padding value corresponds to the minimum of the moving image. Can be adjusted using the defaultvalue parameter.
150157
151158
Args:
152159
fixed_image_path (str or Path): Path to the fixed image.
@@ -195,6 +202,11 @@ def transform(
195202
fixed_image = ants.image_read(str(fixed_image_path))
196203
moving_image = ants.image_read(str(moving_image_path))
197204

205+
if "defaultvalue" not in transform_kwargs:
206+
transform_kwargs["defaultvalue"] = (
207+
moving_image.min()
208+
) # set out of view voxels by default to min value, i.e. air/ bg
209+
198210
# Ensure output directory exist
199211
transformed_image_path.parent.mkdir(parents=True, exist_ok=True)
200212

brainles_preprocessing/registration/niftyreg/niftyreg.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import numpy as np
99
from auxiliary.runscript import ScriptRunner
1010
from auxiliary.turbopath import turbopath
11+
from auxiliary.io import read_image
1112

1213
from brainles_preprocessing.registration.registrator import Registrator
1314

@@ -79,12 +80,16 @@ def register(
7980

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

83+
# read moving image to get padding value
84+
padding_value = float(read_image(moving_image_path).min())
85+
8286
input_params = [
8387
turbopath(niftyreg_executable),
8488
turbopath(fixed_image_path),
8589
turbopath(moving_image_path),
8690
turbopath(transformed_image_path),
8791
str(matrix_path),
92+
str(padding_value),
8893
]
8994

9095
# Call the run method to execute the script and capture the output in the log file
@@ -155,6 +160,7 @@ def transform(
155160
) -> None:
156161
"""
157162
Apply a transformation using NiftyReg.
163+
By default the padding value corresponds to the minimum of the moving image.
158164
159165
Args:
160166
fixed_image_path (str): Path to the fixed image.
@@ -199,13 +205,17 @@ def transform(
199205
else:
200206
transform_path = Path(matrix_path).with_suffix(".txt")
201207

208+
# read moving image to get padding value
209+
padding_value = float(read_image(moving_image_path).min())
210+
202211
input_params = [
203212
turbopath(niftyreg_executable),
204213
turbopath(fixed_image_path),
205214
turbopath(moving_image_path),
206215
turbopath(transformed_image_path),
207216
str(transform_path),
208-
interpolator, # interpolation method, 3 is Cubic
217+
str(interpolator), # interpolation method, 3 is Cubic
218+
str(padding_value),
209219
]
210220

211221
# Call the run method to execute the script and capture the output in the log file

brainles_preprocessing/registration/niftyreg/niftyreg_scripts/rigid_reg.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ file_exists() {
1010
}
1111

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

@@ -21,6 +21,7 @@ fixed_image="$2"
2121
moving_image="$3"
2222
transformed_image="$4"
2323
transformation_matrix="$5"
24+
padding_value="$6"
2425

2526
# Validate the existence of input files
2627
if ! file_exists "$fixed_image"; then
@@ -41,6 +42,7 @@ registration_options=(
4142
"-flo" "$moving_image"
4243
"-res" "$transformed_image"
4344
"-aff" "$transformation_matrix"
45+
"-pad" "$padding_value"
4446
)
4547

4648
# Perform rigid-only registration with NiftyReg

brainles_preprocessing/registration/niftyreg/niftyreg_scripts/transform.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ file_exists() {
1010
}
1111

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

@@ -22,6 +22,7 @@ moving_image="$3"
2222
transformed_image="$4"
2323
transformation_matrix="$5"
2424
interpolation_method="$6"
25+
padding_value="$7"
2526

2627
# Validate the existence of input files
2728
if ! file_exists "$fixed_image"; then
@@ -51,6 +52,7 @@ resample_command=(
5152
-trans "$transformation_matrix"
5253
-res "$transformed_image"
5354
-inter "$interpolation_method"
55+
-pad "$padding_value"
5456
)
5557

5658
if file_exists "$niftyreg_path"; then

0 commit comments

Comments
 (0)