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