ANTsX / ANTsPy

A fast medical imaging analysis library in Python with algorithms for registration, segmentation, and more.
https://antspyx.readthedocs.io
Apache License 2.0
648 stars 164 forks source link

build_template is darker in regions that are not always fully in field of view. #729

Open dyollb opened 1 week ago

dyollb commented 1 week ago

Is your feature request related to a problem? Please describe.

build_template produces an atlas that is blurry in regions that are not always fully in the field-of-view. See the attached screenshot.

I believe the reason is partly because the mean image is not normalized correctly in these regions.

Describe the solution you'd like I would propose to calculate the sum of the transformed weight images. The mean image can then be normalized using this weight image.

the inner for loop could could be modified like this

        for k in range(len(image_list)):
            w1 = ants.registration(
                xavg, image_list[k], type_of_transform=type_of_transform, **kwargs
            )
            L = len(w1["fwdtransforms"])
            # affine is the last one
            affinelist.append(w1["fwdtransforms"][L-1])

            if k == 0:
                if L == 2:
                    wavg = ants.image_read(w1["fwdtransforms"][0]) * weights[k]
                xavgNew = w1["warpedmovout"] * weights[k]
                wimgNew = ants.apply_transforms(xavg, ants.ones_like(image_list[k]) * weights[k], transformlist=w1["fwdtransforms"])
            else:
                if L == 2:
                    wavg = wavg + ants.image_read(w1["fwdtransforms"][0]) * weights[k]
                xavgNew = xavgNew + w1["warpedmovout"] * weights[k]
                wimgNew = wimgNew + ants.apply_transforms(xavg, ants.ones_like(image_list[k]) * weights[k], transformlist=w1["fwdtransforms"])

        # normalize
        nonzero = wimgNew.view() != 0
        xavgNew.view()[nonzero] = xavgNew.view()[nonzero] / wimgNew.view()[nonzero]

Note I introduced a ones_like helper, to make this modification more readable.

Describe alternatives you've considered Re-implement the build_template function in my own code.

Additional context using the current implementation: image

with re-normalization using the code above. While the image is still blurry (because registration is less accurate near the boundary), the brightness stays more homogeneous: image