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

The 2D registration results are dark #409

Open
LexTran opened this issue Nov 9, 2022 · 7 comments
Open

The 2D registration results are dark #409

LexTran opened this issue Nov 9, 2022 · 7 comments

Comments

@LexTran
Copy link

LexTran commented Nov 9, 2022

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):

  • OS: Windows 10 Pro 64
@ntustison
Copy link
Member

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
Copy link
Author

LexTran commented Nov 9, 2022

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
Copy link
Member

cookpa commented Nov 9, 2022

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
Copy link
Author

LexTran commented Nov 9, 2022

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
Copy link
Member

cookpa commented Nov 9, 2022

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
Copy link
Author

LexTran commented Nov 9, 2022 via email

@LexTran
Copy link
Author

LexTran commented Nov 10, 2022

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants