I am trying to write a Filter which labels an input image in a specific manner. To do this I extended ImageToImageFilter
Class like so :
template< typename TInputImage, typename TOutputImage=TInputImage>
class ITK_TEMPLATE_EXPORT LabellingFilter:
public ImageToImageFilter<TInputImage, TOutputImage>{
ITK_DISALLOW_COPY_AND_ASSIGN(LabellingFilter);
/** Standard class type aliases. */
using Self = LabellingFilter;
using Superclass = ImageToImageFilter<TInputImage, TOutputImage>;
using Pointer = SmartPointer<Self>;
using ConstPointer = SmartPointer<const Self>;
/** Method for creation through the object factory. */
itkNewMacro(Self);
itkTypeMacro( LabellingFilter, ImageToImageFilter );
....
};
With GenerateData() defined as follows…
GenerateData(){
typename InputImageType::ConstPointer input = this->GetInput();
typename OutputImageType::Pointer output = this->GetOutput();
output->Allocate();// I don't think this should be required.
//This part causes Segmentation Fault if allocate not called...
InputIndexType index;
index.Fill(1);
std::cout<< output->GetPixel(index)<< std::endl;
using InputIteratorType = itk::ImageRegionConstIteratorWithIndex<InputImageType >;
InputIteratorType inputIt = InputIteratorType(input, input->GetLargestPossibleRegion());
std::cout << "Initialized input iterator\n";
std::cout << "Size of output = " << output->GetLargestPossibleRegion();
using OutputIteratorType = itk::ImageRegionIterator<OutputImageType>;
//This throws error with and without allocate..
OutputIteratorType outputIt = OutputIteratorType(output, input->GetLargestPossibleRegion());
// Changing this to input->GetBufferedRegion() still causes an error
std::cout<< "Initialized output iterator\n";
inputIt.GoToBegin();
outputIt.GoToBegin();
while(!inputIt.IsAtEnd()){
InputIndexType currentIndex = inputIt.GetIndex();
unsigned lbl = this->Label(currentIndex);
outputIt.Set(lbl);
++outputIt;
++inputIt;
}
std::cout<<"Fin"<< std::endl;
}
Following is a sample output…
Initialized input iterator
Size of output = ImageRegion (0x55a8f1f5ba88)
Dimension: 3
Index: [0, 0, 0]
Size: [5, 5, 5]
seg: /usr/local/include/ITK-5.0/itkImageConstIterator.h:210: void itk::ImageConstIterator<TImage>::SetRegion(const RegionType&) [with TImage = itk::Image<unsigned char, 3>; itk::ImageConstIterator<TImage>::RegionType = itk::ImageRegion<3>]: Assertion `Region ImageRegion (0x7ffd531dec00)
Dimension: 3
Index: [0, 0, 0]
Size: [5, 5, 5]
is outside of buffered region ImageRegion (0x55a8f1f5baf8)
Dimension: 3
Index: [0, 0, 0]
Size: [0, 0, 0]
' failed.
How do I fix this error?
Edit:
If I change GenerateData() -> DynamicThreadedGenerateData(…) the code works fine!