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

Confusion regarding the use of create_jacobian_determinant_image #491

Closed YasmineMH1997 closed 1 year ago

YasmineMH1997 commented 1 year ago

I need to understand what is tx. Can the TX be the MNI152_T1_1mm_brain.nii.gz file? I aligned the T1w together by setting one T1w image as the reference and aligned the rest on it. The ref T1w was transformed to MNI152 first before using it. So what's the correct way to use the below function to transform the T1w images to jacobian?

def create_jacobian_determinant_image(domain_image, tx, do_log=False, geom=False):
    """
    Compute the jacobian determinant from a transformation file

    ANTsR function: `createJacobianDeterminantImage`

    Arguments
    ---------
    domain_image : ANTsImage
        image that defines transformation domain

    tx : string
        deformation transformation file name

    do_log : boolean
        return the log jacobian

    geom : bolean
        use the geometric jacobian calculation (boolean)

    Returns
    -------
    ANTsImage

    Example
    -------
    >>> import ants
    >>> fi = ants.image_read( ants.get_ants_data('r16'))
    >>> mi = ants.image_read( ants.get_ants_data('r64'))
    >>> fi = ants.resample_image(fi,(128,128),1,0)
cookpa commented 1 year ago

I'm transferring this to ANTsPy, please open issues about ANTsPy there

cookpa commented 1 year ago

tx is the transform from antsRegistration, as shown in the rest of the example you mentioned

import ants fi = ants.image_read( ants.get_ants_data('r16')) mi = ants.image_read( ants.get_ants_data('r64')) fi = ants.resample_image(fi,(128,128),1,0) mi = ants.resample_image(mi,(128,128),1,0) mytx = ants.registration(fixed=fi , moving=mi, type_of_transform = ('SyN') ) jac = ants.create_jacobian_determinant_image(fi,mytx['fwdtransforms'][0],1)

The jacobian is derived from the warp field mytx['fwdtransforms'][0], which represents the nonlinear deformation in the registration of the moving to the fixed image. It is defined in the domain of the fixed image.

This example might be useful

https://github.com/cookpa/antsJacobianExample

YasmineMH1997 commented 1 year ago

Why I keep getting this error: Registeration computation completed successfully for sub-OAS30901_sess-d0155_CT_contrastStretching_MNI152.nii.gz_brain.nii.gz Error occurred for sub-OAS30901_sess-d0155_CT_contrastStretching_MNI152.nii.gz_brain.nii.gz: /project/itksource/Modules/IO/ImageBase/include/itkImageFileReader.hxx:132: Could not create IO object for reading file /tmp/tmp3n56ok430GenericAffine.mat Tried to create one of the following: BMPImageIO BioRadImageIO Bruker2dseqImageIO GDCMImageIO GE4ImageIO GE5ImageIO GiplImageIO HDF5ImageIO JPEGImageIO JPEG2000ImageIO LSMImageIO MGHImageIO MINCImageIO MRCImageIO MetaImageIO NiftiImageIO NrrdImageIO PNGImageIO StimulateImageIO TIFFImageIO VTKImageIO You probably failed to set a file suffix, or set the suffix to an unsupported type.

I am trying to apply a rigid transformation on each CT scan to align it with its corresponding T1w (same subject). Then I create the Jacobian determinant.
` ct_image = ants.image_read(ct_file) mr_image = ants.image_read(mr_file)

            # Perform rigid registration
            transform = ants.registration(fixed=mr_image, moving=ct_image, type_of_transform="Rigid")

            # Apply the transformation to the CT scan image
            registered_ct_image = ants.apply_transforms(fixed=mr_image, moving=ct_image, transformlist=transform['fwdtransforms'])

            # Save the registered CT scan image
            output_filename = f"{filename}_registered_CT.nii.gz"
            output_file = os.path.join(output_registered , output_filename)
            ants.image_write(registered_ct_image, output_file)
            print(f"Registeration computation completed successfully for {filename}")

            # Compute the Jacobian determinant image
            jac_image = ants.create_jacobian_determinant_image(registered_ct_image, transform['fwdtransforms'][0])

            # Save the Jacobian determinant image
            jac_output_filename = f"{filename}_jacobian.nii.gz"
            jac_output_file = os.path.join(output_folder, jac_output_filename)
            ants.image_write(jac_image, jac_output_file)
            print(f"Jacobian computation completed successfully for {filename}.")`
cookpa commented 1 year ago

ants.create_jacobian_determinant_image requires a deformation field as input. A rigid transform does not change the volume of any part of the image, so the Jacobian determinant is 1 in every voxel.

YasmineMH1997 commented 1 year ago

image How can the image be transformed to the Jacobian domain by quantifying voxel level volumetric transitions? The above paper utilized ANTs to do so. But I don't get what transformation is used here.

Abbas, S. Q., Chi, L., & Chen, Y. P. P. (2023). Transformed domain convolutional neural network for alzheimer's disease diagnosis using structural MRI. Pattern Recognition, 133, 109031.

cookpa commented 1 year ago

I don't understand what this paper did. It sounds like they are describing a nonlinear transform, starting from a preprocessed image that has been affinely registered to the template space.

It doesn't look like the authors shared code, you will have to ask them for more details.

YasmineMH1997 commented 1 year ago

Your comments did help though. Thank you so much for your time ^__^

domadaaaa commented 3 months ago

ants.create_jacobian_determinant_image requires a deformation field as input. A rigid transform does not change the volume of any part of the image, so the Jacobian determinant is 1 in every voxel.

hello, how can i got the number of non-positive Jacobi determinants in the deformation field? I execute follow code:

jac_det = ants.create_jacobian_determinant_image(fix_img, outs['fwdtransforms'][0])
jac_det = jac_det.numpy()
non_positive_count = np.count_nonzero(jac_det <= 0)
total_count = np.prod(jac_det.shape)
Jdet = non_positive_count / total_count

but result is 0

domadaaaa commented 3 months ago

ants.create_jacobian_determinant_image requires a deformation field as input. A rigid transform does not change the volume of any part of the image, so the Jacobian determinant is 1 in every voxel.

hello, how can i got the number of non-positive Jacobi determinants in the deformation field? I execute follow code:

jac_det = ants.create_jacobian_determinant_image(fix_img, outs['fwdtransforms'][0])
jac_det = jac_det.numpy()
non_positive_count = np.count_nonzero(jac_det <= 0)
total_count = np.prod(jac_det.shape)
Jdet = non_positive_count / total_count

but result is 0

is that data problem? but i got non_zero value if using learning-based registration model such as voxelmorph or transmorph