# StatisticsImageFilter does not work with sub region

**URL:** https://discourse.itk.org/t/statisticsimagefilter-does-not-work-with-sub-region/3752
**Category:** Community
**Created:** [January 10, 2021, 6:39am UTC](https://discourse.itk.org/t/statisticsimagefilter-does-not-work-with-sub-region/3752 "2021-01-10T06:39:00Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Xinbo\_Li](https://discourse.itk.org/user_avatar/discourse.itk.org/xinbo_li/32/1852_2.png) [@Xinbo\_Li](https://discourse.itk.org/u/Xinbo_Li)
#### Post date: [January 10, 2021, 6:39am UTC](https://discourse.itk.org/t/statisticsimagefilter-does-not-work-with-sub-region/3752/1 "2021-01-10T06:39:00Z")

</div>

I tried to get mean intensity of a sub region in an image by using the **StatisticsImageFilter**.  
Turns out that the filter does not work with sub region.  
I used the 3 set region methods of the class and tried all possible combinations of any one, two or all of the methods with parameters of whole image or the sub region.  
All I can get are either 0 (which is nonsense) or mean intensity of the whole image instead of the region I specified.  
Below is a test case written with gTest.

```
TEST_F(ImageEvaluate_Test, VerifiySubregionMean)
{
    int width = 3;
    int height = 3;
    IndexType index = { 0, 0 };
    SizeType size = { width, height };
    RegionType region = { index, size };

    ImagePointerType image = ImageType::New();
    image->SetRegions(region);
    image->Allocate();

    for (int yp = 0; yp < height; yp++)
    {
        index.at(1) = yp;
        for (int xp = 0; xp < width; xp++)
        {
            index.at(0) = xp;
            unsigned short pix = xp + yp * 10;
            image->SetPixel(index, pix);
        }
    }

    itk::StatisticsImageFilter<ImageType>::Pointer statFilter = itk::StatisticsImageFilter<ImageType>::New();

    SizeType subSize= { 1, 1};
    IndexType subIndex;
    for (int yp = 1; yp < height; yp++)
    {
        subIndex.at(1) = yp;
        for (int xp = 1; xp < width; xp++)
        {
            subIndex.at(0) = xp;
            unsigned short expectMean = xp + yp * 10;

            subSize = { 1, 1};
            RegionType subRegion = { subIndex, subSize };

            //<<< set region for processing, all possible combinations have been tried >>>>>>//
            image->SetLargestPossibleRegion(region);
            image->SetBufferedRegion(region);
            image->SetRequestedRegion(subRegion);

            statFilter->SetInput(image);
            statFilter->Update();

            unsigned short pix = image->GetPixel(subIndex);
            std::cout << "At pixel: (" << xp << ", " << yp << "), intensity is: " << pix << std::endl;

            double meanIntensity = statFilter->GetMean();
            EXPECT_EQ(expectMean, meanIntensity);
        }
    }
}
```

---

<div class="post-metadata">

### Author: ![Xinbo\_Li](https://discourse.itk.org/user_avatar/discourse.itk.org/xinbo_li/32/1852_2.png) [@Xinbo\_Li](https://discourse.itk.org/u/Xinbo_Li)
#### Post date: [January 10, 2021, 6:41am UTC](https://discourse.itk.org/t/statisticsimagefilter-does-not-work-with-sub-region/3752/2 "2021-01-10T06:41:04Z")

</div>

Is my way of using the filter wrong?

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [January 11, 2021, 2:08pm UTC](https://discourse.itk.org/t/statisticsimagefilter-does-not-work-with-sub-region/3752/3 "2021-01-11T14:08:39Z")

</div>

It sound like you are trying to get the filter to do two things:

- Extract a region
- Compute statistics on an Image

The StatisticsImageFilter only performs statistics on the whole or largest possible region of the input image.

> [@Xinbo\_Li](#):
>
> ```auto
> //<<< set region for processing, all possible combinations have been tried >>>>>>//
> image->SetLargestPossibleRegion(region);
> image->SetBufferedRegion(region);
> image->SetRequestedRegion(subRegion);
> 
> statFilter->SetInput(image);
> 
> ```

It appears here that the `image` is the input image to the statistics filter. For filters that support “streaming” or updating a sub-region ( the statistics filters do **not** support this ), on the **output** of the filter the `RequestedRegion` can be set. This is an advanced topic covered in the software guide: [8 How To Write A Filter](https://itk.org/ITKSoftwareGuide/html/Book1/ITKSoftwareGuide-Book1ch8.html#x54-1320008.3)

To compute the image statistics on a sub-region you should extract a sub-region from the original input image with a filter such as the [ExtractImageFilter](https://itk.org/Doxygen/html/classitk_1_1ExtractImageFilter.html) or the [RegionOfInterestImageFilter]([ITK: itk::RegionOfInterestImageFilter\< TInputImage, TOutputImage \> Class Template Reference](https://itk.org/Doxygen/html/classitk_1_1RegionOfInterestImageFilter.html).
