# Pipeline confusion?

**URL:** <https://discourse.itk.org/t/pipeline-confusion/4594>\
**Category:** Beginner Questions\
**Tags:** python\
**Created:** [November 19, 2021, 12:32pm UTC](https://discourse.itk.org/t/pipeline-confusion/4594 "2021-11-19T12:32:10Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Theoretialperson](https://discourse.itk.org/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@Theoretialperson](https://discourse.itk.org/u/Theoretialperson)\
**Post date:** [November 19, 2021, 12:32pm UTC](https://discourse.itk.org/t/pipeline-confusion/4594/1 "2021-11-19T12:32:10Z")

</div>

I’m having facing a problem that possibly stems for a misunderstanding of how pipelines work and get updated. I have the following piece of code,

```python
labelfile = labelfilelist[0]                                                         
labelpath = os.path.join(label_path, labelfile)                                      
MeshType = itk.Mesh[itk.SS,3]                                                        
meshReader = itk.MeshFileReader[MeshType].New()                                      
meshReader.SetFileName(labelpath)                                                    
                                                                                     
pad_filter = itk.ConstantPadImageFilter[ImageType, ImageType].New()                  
pad_filter.SetInput(reader.GetOutput())                                              
pad_filter.SetPadLowerBound((1,1,1))                                                 
pad_filter.SetPadUpperBound((1,1,1))                                                 
                                                                                     
mesh_to_image_filter = itk.TriangleMeshToBinaryImageFilter[MeshType, ImageType].New()
mesh_to_image_filter.SetInput(meshReader.GetOutput())                                
mesh_to_image_filter.SetInfoImage(pad_filter.GetOutput())                            
                                                                                     
crop_filter = itk.CropImageFilter[ImageType, ImageType].New()                        
crop_filter.SetInput(mesh_to_image_filter.GetOutput())                               
crop_filter.SetUpperBoundaryCropSize((1,1,1))                                        
crop_filter.SetLowerBoundaryCropSize((1,1,1))                                        
crop_filter.Update()                                                                 
                                                                                     
cropped_image = crop_filter.GetOutput()                                              
image = itk.GetArrayViewFromImage(cropped_image).astype(np.short)                    
itk_image = itk.GetImageFromArray(image)                                             
itk_image.SetMetaDataDictionary(reader.GetMetaDataDictionary())                      
                                                                                     
header = itk.ChangeInformationImageFilter[ImageType].New()                           
header.SetInput(itk_image)                                                           
header.SetOutputSpacing(reader.GetOutput().GetSpacing())                             
header.ChangeSpacingOn()                                                             
header.SetOutputOrigin(reader.GetOutput().GetOrigin())                               
header.ChangeOriginOn()                                                              
header.SetOutputDirection(reader.GetOutput().GetDirection())                         
header.ChangeDirectionOn()                                                           
header.UpdateOutputInformation()                                                     
                                                                                     
writer = itk.ImageFileWriter[ImageType].New()                                        
writer.SetInput(header.GetOutput())                                                  
segmentation_name = os.path.join(output_label, series_name + '_' +ann+ '.nii.gz')    
writer.SetFileName(segmentation_name)                                                
writer.Update()                                                                      

```

which gives me an error:

```auto
Traceback (most recent call last):                                                                                                
  File "dicom_to_nifti.py", line 276, in <module>                                                                                 
    separate_labels(spec_data_dar, spec_annotation_directory, output, case_number, phase)                                         
  File "dicom_to_nifti.py", line 127, in separate_labels                                                                          
    crop_filter.Update()                                                                                                          
RuntimeError: /work/ITK-source/ITK/Modules/Filtering/ImageGrid/include/itkCropImageFilter.hxx:76:                                 
itk::ERROR: itk::ERROR: CropImageFilter(0x55577c544990): The input image's size [0, 0, 0] is less than the total of the crop size!

```

If I `Update()` the `mesh_to_image_filter` before passing its output to the `crop_filter`, it does not complain anymore, but I don’t understand why I have to in the first place.

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [November 19, 2021, 2:32pm UTC](https://discourse.itk.org/t/pipeline-confusion/4594/2 "2021-11-19T14:32:42Z")

</div>

`pad_filter` needs to be updated, in order to have the output image’s metadata (size, start index etc) properly computed. As `mesh_to_image_filter` depends on it, it calls the update in its update method.

I guess that `crop_filter` tries to calculate image metadata before ensuring its input is up to date, thus causing the problem.

---

<div class="post-metadata">

**Author:** ![Theoretialperson](https://discourse.itk.org/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@Theoretialperson](https://discourse.itk.org/u/Theoretialperson)\
**Post date:** [November 19, 2021, 2:45pm UTC](https://discourse.itk.org/t/pipeline-confusion/4594/3 "2021-11-19T14:45:58Z")

</div>

I guess this is where the error originates from:

```auto
template <typename TInputImage, typename TOutputImage>
void
CropImageFilter<TInputImage, TOutputImage>::VerifyInputInformation() ITKv5_CONST
{
  Superclass::VerifyInputInformation();

  const TInputImage * inputPtr = this->GetInput();

  InputImageSizeType input_sz = inputPtr->GetLargestPossibleRegion().GetSize();

  for (unsigned int i = 0; i < InputImageDimension; ++i)
  {
    if (input_sz[i] < (m_UpperBoundaryCropSize[i] + m_LowerBoundaryCropSize[i]))
    {
      itkExceptionMacro("The input image's size " << input_sz << " is less than the total of the crop size!");
    }
  }
}

```

Wouldn’t it be a bug if I have to explicitly call `Update`?

Also, as you see, I’m padding and then cropping by 1 pixel. This is so that I can bypass a [bug](https://github.com/InsightSoftwareConsortium/ITK/issues/2416) in `TriangleMeshToBinaryImageFilter`. Again, I would expect that in reality the padding and cropping would not affect the result of `TriangleMeshToBinaryImageFilter`, but as you can see in the picture, the output seems shifted (red being the one where I cropped and padded, and green without cropping and padding).

 ![image](https://discourse.itk.org/uploads/default/original/2X/2/2fbbf874d2bcf2d0fbfdf4fd6f1508012664cf2f.png)

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [November 19, 2021, 2:51pm UTC](https://discourse.itk.org/t/pipeline-confusion/4594/4 "2021-11-19T14:51:23Z")

</div>

`VerifyInputInformation` isn’t supposed to call Update. It might/should call `inputPtr->UpdateOutputInformation();`. Does this fix it?

`TriangleMeshToBinaryImageFilter` might assume that LargestPossibleRegion starts with index 0. In this case it is wrong. Can you check that this is the problem?

---

<div class="post-metadata">

**Author:** ![Theoretialperson](https://discourse.itk.org/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@Theoretialperson](https://discourse.itk.org/u/Theoretialperson)\
**Post date:** [November 19, 2021, 2:57pm UTC](https://discourse.itk.org/t/pipeline-confusion/4594/5 "2021-11-19T14:57:59Z")

</div>

Sorry, I’m not compiling ITK, I’m just using the Python version. I was looking at the code on Github.

It does not seem to ignore the starting index:

```auto
/** Update */
template <typename TInputMesh, typename TOutputImage>
void
TriangleMeshToBinaryImageFilter<TInputMesh, TOutputImage>::GenerateData()
{
  itkDebugMacro(<< "TriangleMeshToBinaryImageFilter::Update() called");

  // Get the input and output pointers
  OutputImagePointer OutputImage = this->GetOutput();
  if (m_InfoImage == nullptr)
  {
    if (m_Size[0] == 0 || m_Size[1] == 0 || m_Size[2] == 0)
    {
      itkExceptionMacro(<< "Must Set Image Size");
    }

    typename OutputImageType::RegionType region;

    region.SetSize(m_Size);
    region.SetIndex(m_Index);

    OutputImage->SetLargestPossibleRegion(region); //
    OutputImage->SetBufferedRegion(region); // set the region
    OutputImage->SetRequestedRegion(region); //
    OutputImage->SetSpacing(m_Spacing); // set spacing
    OutputImage->SetOrigin(m_Origin); // and origin
    OutputImage->SetDirection(m_Direction); // direction cosines
  }
  else
  {
    itkDebugMacro(<< "Using info image");
    m_InfoImage->Update();
    OutputImage->CopyInformation(m_InfoImage);
    OutputImage->SetRegions(m_InfoImage->GetLargestPossibleRegion());
    m_Size = m_InfoImage->GetLargestPossibleRegion().GetSize();
    m_Index = m_InfoImage->GetLargestPossibleRegion().GetIndex();
    m_Spacing = m_InfoImage->GetSpacing();
    m_Origin = m_InfoImage->GetOrigin();
    m_Direction = m_InfoImage->GetDirection();
  }

```

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [November 19, 2021, 3:41pm UTC](https://discourse.itk.org/t/pipeline-confusion/4594/6 "2021-11-19T15:41:15Z")

</div>

Can you submit these as two separate bugs on the [issue tracker](https://github.com/InsightSoftwareConsortium/ITK/issues)?

[ITKCuberille](https://github.com/InsightSoftwareConsortium/ITKCuberille) is kind of a higher-quality `TriangleMeshToBinaryImageFilter`. That might alleviate your immediate issues.

---

<div class="post-metadata">

**Author:** ![Theoretialperson](https://discourse.itk.org/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@Theoretialperson](https://discourse.itk.org/u/Theoretialperson)\
**Post date:** [November 19, 2021, 4:15pm UTC](https://discourse.itk.org/t/pipeline-confusion/4594/7 "2021-11-19T16:15:00Z")

</div>

Thanks I’ll have a look.

Here are the issues I created:

> <https://github.com/InsightSoftwareConsortium/ITK/issues/2884>
>
> \### Description
> 
> Due to a \[bug\](https://github.com/InsightSoftwareConsortium/I…TK/issues/2416) with \`TriangleMeshToBinaryImageFilter\` where it can get corrupt near the edges, I tried padding the image and then cropping it by 1 pixel border. Unfortunately, that shifts the final results as you can see here:
> \<img width="410" alt="image" src="https://user-images.githubusercontent.com/91547393/142653607-b15aa58e-bda1-48a3-8c6f-14b59e998296.png"\>
> Green is with padding/cropping, and white is without.
> 
> \### Steps to Reproduce
> 
> Run the following code
> \`\`\`python
> t\_mesh\_filter.py                                                                        
> 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))                                            
> pad\_filter.Update()                                                             
>                                                                                         
> 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")                  
> \`\`\`
> 
> \### Expected behavior
> 
> Cropping and padding should not affect the output.
> 
> \### Versions
> 
> Python ITK '5.2.1'
> 
> \### Environment
> 
> Linux, Python 3.8.10

> <https://github.com/InsightSoftwareConsortium/ITK/issues/2885>
>
> \### 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:
> \`\`\`python
> 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

---

<div class="post-metadata">

**Author:** ![Theoretialperson](https://discourse.itk.org/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@Theoretialperson](https://discourse.itk.org/u/Theoretialperson)\
**Post date:** [December 1, 2021, 9:30am UTC](https://discourse.itk.org/t/pipeline-confusion/4594/8 "2021-12-01T09:30:41Z")

</div>

@dzenanz From my understanding, [ITKCuberille](https://github.com/InsightSoftwareConsortium/ITKCuberille) does the inverse of what I want to do, i.e. it is a binary image / implicit surface to mesh filter.

Are there any alternatives or am is e.g. VTK my best alternative?

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [December 1, 2021, 3:38pm UTC](https://discourse.itk.org/t/pipeline-confusion/4594/9 "2021-12-01T15:38:28Z")

</div>

> [@dzenanz](#):
>
> TriangleMeshToBinaryImageFilter

This is the only filter in ITK. VTK has [vtkPolyDataToImageStencil](https://vtk.org/doc/nightly/html/classvtkPolyDataToImageStencil.html). There might be some other implementations, but I think the one in VTK is quite mature and heavily used.
