StatisticsImageFilter does not work with sub region

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);
        }
    }
}

Is my way of using the filter wrong?

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.

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

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 or the [RegionOfInterestImageFilter](ITK: itk::RegionOfInterestImageFilter< TInputImage, TOutputImage > Class Template Reference.

1 Like