Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build template normalize weights #730

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion ants/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
make_image,
from_numpy,
from_numpy_like,
new_image_like)
new_image_like,
ones_like)
from .ants_image import (ANTsImage,
copy_image_info,
set_origin,
Expand Down
23 changes: 22 additions & 1 deletion ants/core/ants_image_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,4 +527,25 @@ def new_image_like(image, data):
has_components=image.has_components)

def from_numpy_like(data, image):
return new_image_like(image, data)
return new_image_like(image, data)


def ones_like(image):
"""
Return an image of ones with the same shape and info as a given image.

Arguments
---------
image : ANTsImage
Image to get image shape and info from.

Returns
-------
ANTsImage
Image of ones with reference header information
"""
ones = image.clone()
ones.view().fill(1)
return ones


23 changes: 23 additions & 0 deletions ants/registration/build_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def build_template(
blending_weight=0.75,
weights=None,
useNoRigid=True,
normalize=False,
**kwargs
):
"""
Expand Down Expand Up @@ -44,6 +45,11 @@ def build_template(
useNoRigid : boolean
equivalent of -y in the script. Template update
step will not use the rigid component if this is True.

normalize : boolean
if this is True, the intensity contribution from each input
image is renormalized on a per-pixel level.

kwargs : keyword args
extra arguments passed to ants registration

Expand Down Expand Up @@ -74,6 +80,13 @@ def build_template(
temp = image_list[i] * weights[i]
temp = ants.resample_image_to_target(temp, initial_template)
initial_template = initial_template + temp
if normalize:
wimg = initial_template.clone("float")
for i in range(len(image_list)):
wtemp = ants.resample_image_to_target(ants.ones_like(image_list[i]), wimg)
wimg = wimg + wtemp * weights[i]
nonzero = wimg.view() != 0
initial_template.view()[nonzero] = initial_template.view()[nonzero] / wimg.view()[nonzero]

xavg = initial_template.clone()
for i in range(iterations):
Expand All @@ -90,10 +103,20 @@ def build_template(
if L == 2:
wavg = ants.image_read(w1["fwdtransforms"][0]) * weights[k]
xavgNew = w1["warpedmovout"] * weights[k]
if normalize:
wimg = ants.apply_transforms(xavg, ants.ones_like(image_list[k]), transformlist=w1["fwdtransforms"]) * weights[k]
else:
if L == 2:
wavg = wavg + ants.image_read(w1["fwdtransforms"][0]) * weights[k]
xavgNew = xavgNew + w1["warpedmovout"] * weights[k]
if normalize:
wimg = wimg + ants.apply_transforms(xavg, ants.ones_like(image_list[k]), transformlist=w1["fwdtransforms"]) * weights[k]

if normalize:
nonzero = wimg.view() != 0
xavgNew.view()[nonzero] = xavgNew.view()[nonzero] / wimg.view()[nonzero]
if L == 2:
wavg.view()[nonzero] = wavg.view()[nonzero] / wimg.view()[nonzero]

if useNoRigid:
avgaffine = ants.average_affine_transform_no_rigid(affinelist)
Expand Down