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
587 stars 161 forks source link

The 2D registration results are dark #409

Open LexTran opened 1 year ago

LexTran commented 1 year ago

Describe the bug Hi, I was using ANTsPy to register 2D images, but I got 2 dark image as warped result and inverse warped result, I don't understand what's going on.

To Reproduce My code to register the images is as follow:

def img_to_single_component(name):
    img = Image.open(name)
    # img = img.resize((1080, 1080))
    img = img.convert('P')
    img1 = ants.from_numpy(np.array(img))
    return img1
def antsRegistration(fi_path, mv_path, method, random_seed):
    fi = img_to_single_component(fi_path)
    mi = img_to_single_component(mv_path)
    mytx = ants.registration(fixed=fi, moving=mi, type_of_transform=method, randome_seed=random_seed)
    my_warped_image = ants.apply_transforms(fixed=fi, moving=mi, transformlist=mytx['fwdtransforms'],interpolator='nearestNeighbor')
    my_inversed_warped_image = ants.apply_transforms(fixed=mi, moving=fi, transformlist=mytx['invtransforms'],interpolator='nearestNeighbor')
    imageio.imwrite(f'{method}_Warped.png', my_warped_image.numpy())
    imageio.imwrite(f'{method}_InverseWarped.png', my_inversed_warped_image.numpy())

Expected behavior A clear and concise description of what you expected to happen.

Screenshots Affine_Warped This is the warped image I get, the method is "Affine" and the interpolation is set to be "nearestNeighbor"

Desktop (please complete the following information):

ntustison commented 1 year ago

For your inverse warped image, you forgot to specify the whichtoinvert parameter. Also, given your conversions to/from numpy arrays, we'd actually need a reproducible example (including data) to diagnose the problem.

LexTran commented 1 year ago

For your inverse warped image, you forgot to specify the whichtoinvert parameter. Also, given your conversions to/from numpy arrays, we'd actually need a reproducible example (including data) to diagnose the problem.

Thanks for replying me, I'll give my example as bellow:

import ants
import imageio
from PIL import Image
import numpy as np
import os

def img_to_single_component(name):
    img = Image.open(name)
    # img = img.resize((1080, 1080))
    img = img.convert('P')
    img1 = ants.from_numpy(np.array(img))
    return img1

def antsRegistration(fi_path, mv_path, method, random_seed):
    fi = img_to_single_component(fi_path)
    mi = img_to_single_component(mv_path)

    mytx = ants.registration(fixed=fi, moving=mi, type_of_transform=method, randome_seed=random_seed)
    my_warped_image = ants.apply_transforms(fixed=fi, moving=mi, transformlist=mytx['fwdtransforms'],interpolator='nearestNeighbor')
    my_inversed_warped_image = ants.apply_transforms(fixed=mi, moving=fi, transformlist=mytx['invtransforms'],interpolator='nearestNeighbor')
    imageio.imwrite(f'{method}_Warped.png', my_warped_image.numpy())
    imageio.imwrite(f'{method}_InverseWarped.png', my_inversed_warped_image.numpy())

if __name__ =='__main__':

    mi_path = "./mi.png"
    fi_path = "./fi.png"

    antsRegistration(fi_path, mi_path, 'Affine', 42)

And here are my images: Moving image: mi

Fixed image: fi

cookpa commented 1 year ago

If I do

fi = img_to_single_component(fi_path)
imageio.imwrite('fi.png', fi.numpy())

I see that the constrast is much compressed, and the image appears dark.

Try replacing your image conversion with this

img = img.convert('L')

The imageio docs suggest this as the way to convert an RGB image to grayscale.

LexTran commented 1 year ago

If I do

fi = img_to_single_component(fi_path)
imageio.imwrite('fi.png', fi.numpy())

I see that the constrast is much compressed, and the image appears dark.

Try replacing your image conversion with this

img = img.convert('L')

The imageio docs suggest this as the way to convert an RGB image to grayscale.

Thanks a lot, it actually worked! But I got another question, now the results look like exactly the same as the origin image This is when I use Affine method: Affine_Warped

And this is when I use SyN method: SyN_Warped

From the SyN method's result, you can see ants is working, but when it comes to the Affine method, it seems nothing happened, I'm a little confused.

And by working I mean it is working, but the result is not good, I don't understand why

cookpa commented 1 year ago

You can try searching for a better affine solution with ants.affine_initializer. Unfortunately the Python version only supports rotations, the command line antsAI will also let you search translations.

Beyond that, you're trying to align two different 2D projections of 3D information. The registration only has the 2D grayscale information and local gradients to go on. It may need help (eg, anatomical landmarks).

LexTran commented 1 year ago

I understood, thanks again for helping me! I'll try the commind line version of ants.

---Original--- From: "Philip @.> Date: Thu, Nov 10, 2022 01:02 AM To: @.>; Cc: @.**@.>; Subject: Re: [ANTsX/ANTsPy] The 2D registration results are dark (Issue #409)

You can try searching for a better affine solution with ants.affine_initializer. Unfortunately the Python version only supports rotations, the command line antsAI will also let you search translations.

Beyond that, you're trying to align two different 2D projections of 3D information. The registration only has the 2D grayscale information and local gradients to go on. It may need help (eg, anatomical landmarks).

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>

LexTran commented 1 year ago

You can try searching for a better affine solution with ants.affine_initializer. Unfortunately the Python version only supports rotations, the command line antsAI will also let you search translations.

Beyond that, you're trying to align two different 2D projections of 3D information. The registration only has the 2D grayscale information and local gradients to go on. It may need help (eg, anatomical landmarks).

Hi, it' me again, when I tried to use the 'ants.affine_initializer', the program seems stuck, it is too slow, I'm not sure is it still working, I've been waiting about 30mins, but it seems still running, is this normal?

I used it like this:

def antsAffine(fi_path, mi_path):
    fi = img_to_single_component(fi_path)
    mi = img_to_single_component(mi_path)
    print("Finding affine initial transform...")
    txfn = ants.affine_initializer(fi, mi)
    my_warped_image = ants.apply_transforms(fixed=fi, moving=mi, transformlist=txfn, interpolator='nearestNeighbor')
    print("Affine Done")
    imageio.imwrite(f'Affine_Warped.png', my_warped_image.numpy())

if __name__ =='__main__':

    mi_path = "./mi.png"
    fi_path = "./fi.png"

    antsAffine(fi_path, mi_path)