InsightSoftwareConsortium / ITK

Insight Toolkit (ITK) -- Official Repository. ITK builds on a proven, spatially-oriented architecture for processing, segmentation, and registration of scientific images in two, three, or more dimensions.
https://itk.org
Apache License 2.0
1.4k stars 662 forks source link

CropImageFilter complaints about 0 image size #2885

Open nicktasios opened 2 years ago

nicktasios commented 2 years ago

Description

When passing a filter output to CropImageFilter I get the following error if I don't update the filter first.

Traceback (most recent call last):                                                                                    
  File "test_mesh_filter.py", line 52, in <module>                                                                    

RuntimeError: /work/ITK-source/ITK/Modules/Filtering/ImageGrid/include/itkCropImageFilter.hxx:76:                     
ITK ERROR: CropImageFilter(0x55a7ea50ac80): The input image's size [0, 0, 0] is less than the total of the crop size! 

Steps to Reproduce

Run the following:

import itk                                                                              

if __name__ == '__main__':                                                              
    should_pad_and_crop = True                                                          

    TCoordinate = itk.D                                                                 
    Dimension = 3                                                                       
    TMesh = itk.Mesh[TCoordinate, Dimension].New()                                      
    sphere = itk.RegularSphereMeshSource[TMesh].New()                                   
    sphere.SetResolution(4)                                                             
    sphere_mesh = sphere.GetOutput()                                                    

    mesh_writer = itk.MeshFileWriter[TMesh].New()                                       
    mesh_writer.SetFileName( "sphere.vtk" )                                             
    mesh_writer.SetInput(sphere_mesh)                                                   
    mesh_writer.Update()                                                                

    TMesh = itk.Mesh[itk.SS, Dimension].New()                                           
    mesh_reader = itk.MeshFileReader[TMesh].New()                                       
    mesh_reader.SetFileName("sphere.vtk")                                               
    mesh_reader.Update()                                                                
    sphere_mesh = mesh_reader.GetOutput()                                               

    TPixel = itk.SS                                                                     
    TImage = itk.Image[TPixel, Dimension]                                               

    image = itk.Image[TPixel, Dimension].New()                                          
    region = itk.ImageRegion[Dimension]()                                               
    region.SetSize([128, 128, 128])                                                     
    region.SetIndex([0, 0, 0])                                                          
    image.SetRegions(region)                                                            
    image.Allocate()                                                                    
    image.SetOrigin([-1.28, -1.28, -1.28])                                              
    image.SetSpacing([0.02, 0.02, 0.02])                                                

    if should_pad_and_crop:                                                             
        pad_filter = itk.ConstantPadImageFilter[TImage, TImage].New()                   
        pad_filter.SetInput(image)                                                      
        pad_filter.SetPadLowerBound((1,1,1))                                            
        pad_filter.SetPadUpperBound((1,1,1))                                            

        mesh_to_image_filter = itk.TriangleMeshToBinaryImageFilter[TMesh, TImage].New() 
        mesh_to_image_filter.SetInput(sphere_mesh)                                      
        mesh_to_image_filter.SetInfoImage(pad_filter.GetOutput())                       
        #mesh_to_image_filter.Update()                                                  

        crop_filter = itk.CropImageFilter[TImage, TImage].New()                         
        crop_filter.SetInput(mesh_to_image_filter.GetOutput())                          
        crop_filter.SetUpperBoundaryCropSize((1,1,1))                                   
        crop_filter.SetLowerBoundaryCropSize((1,1,1))                                   
        crop_filter.Update()                                                            

        itk.imwrite(crop_filter.GetOutput(), "sphere.nii.gz")                           
    else:                                                                               
        mesh_to_image_filter = itk.TriangleMeshToBinaryImageFilter[TMesh, TImage].New() 
        mesh_to_image_filter.SetInput(sphere_mesh)                                      
        mesh_to_image_filter.SetInfoImage(image)                                        
        mesh_to_image_filter.Update()                                                   

        itk.imwrite(mesh_to_image_filter.GetOutput(), "sphere.nii.gz")                  

If the Update is uncommented it works fine.

Expected behavior

I should not need to call Update on the filter before passing it as input to CropImageFilter.

Versions

Python ITK '5.2.1'

Environment

Linux Python 3.8.10

blowekamp commented 2 years ago

My expectation is that this is a bug with TriangleMeshToBinaryImageFilter. VerifyOutputInformation should be called after UpdateOutputInformation. The TriangleMeshToBinaryImageFilter should update it's output's size in UpdateOutputInformation.These error messages seem to indicate that TriangleMeshToBinaryImageFilter did not update its output size and other information.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. Thank you for your contributions.