Skip to content

CropImageFilter complaints about 0 image size #2885

@nicktasios

Description

@nicktasios

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:FilteringIssues affecting the Filtering moduletype:BugInconsistencies or issues which will cause an incorrect result under some or all circumstances

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions