# Region outside of buffered region

**URL:** https://discourse.itk.org/t/region-outside-of-buffered-region/2677
**Category:** Engineering
**Created:** [February 3, 2020, 5:19pm UTC](https://discourse.itk.org/t/region-outside-of-buffered-region/2677 "2020-02-03T17:19:54Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![spic](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/ea5d25/32.png) [@spic](https://discourse.itk.org/u/spic)
#### Post date: [February 3, 2020, 5:19pm UTC](https://discourse.itk.org/t/region-outside-of-buffered-region/2677/1 "2020-02-03T17:19:55Z")

</div>

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!
