📄 itkregiongrow2dtest.cxx
字号:
++labelIt2;
++k;
} //end while iterator loop
std::cout << "Test PASSED" << std::endl;
KLMFilter->ResetPipeline();
// THIRD TEST:
// degenerate case, all 0 image, all constant lambda values -
// issues related to speed of algorithm
std::cout << std::endl << "Third test, all 0 image" << std::endl;
inIt.GoToBegin();
pixelData[0] = 0;
pixelData[1] = 0;
while ( inIt != inItEnd ) {
inIt.Set( pixelData );
++inIt;
}
gridSize[0] = 1;
gridSize[1] = 2;
KLMFilter->SetInput( image );
KLMFilter->SetMaximumNumberOfRegions( 25 );
KLMFilter->SetGridSize( gridSize );
KLMFilter->SetMaximumLambda( 1e45 );
// Kick off the Region grow function
LOCAL_TEST_EXCEPTION_MACRO( KLMFilter );
// KLMFilter->Print(std::cout);
if( KLMFilter->GetMaximumNumberOfRegions() !=
KLMFilter->GetNumberOfRegions() )
{
std::cout << KLMFilter->GetMaximumNumberOfRegions()
<< KLMFilter->GetNumberOfRegions() << std::endl;
std::cout << "Test FAILED" << std::endl;
return EXIT_FAILURE;
}
// Test the functions useful to test the region and border statistics
// as the regions are merged. Primarily useful for debug operations and are
// called several times, so prudent usage is advisable.
// KLMFilter->PrintAlgorithmRegionStats();
// KLMFilter->PrintAlgorithmBorderStats();
std::cout << "Extracting and checking approximation image" << std::endl;
OutputImageType::Pointer outImage3 = KLMFilter->GetOutput();
OutputImageIterator outIt3( outImage3, outImage3->GetBufferedRegion() );
OutputImageIterator outItEnd3 = outIt3.End();
while ( outIt3 != outItEnd3 )
{
pixelOut = outIt3.Get();
if ( pixelOut[0] != 0 || pixelOut[1] != 0 )
{
std::cout << "Test FAILED" << std::endl;
if ( pixelOut[0] != 0 )
{
std::cout << "pixelOut[0]: " << pixelOut[0]
<< " != "
<< "0"
<< std::endl;
}
if ( pixelOut[1] != 0 )
{
std::cout << "pixelOut[1]: " << pixelOut[1]
<< " != "
<< "0"
<< std::endl;
}
return EXIT_FAILURE;
}
++outIt3;
} //end while iterator loop
std::cout << "Extracting and checking label image" << std::endl;
typedef itk::Statistics::ScalarImageToHistogramGenerator<LabelledImageType>
HistogramGeneratorType;
HistogramGeneratorType::Pointer histogramGenerator =
HistogramGeneratorType::New();
histogramGenerator->SetInput( KLMFilter->GetLabelledImage() );
histogramGenerator->SetNumberOfBins( KLMFilter->GetNumberOfRegions() );
histogramGenerator->SetMarginalScale( 1.0 );
histogramGenerator->Compute();
typedef HistogramGeneratorType::HistogramType HistogramType;
const HistogramType * histogram = histogramGenerator->GetOutput();
const unsigned int histogramSize = histogram->Size();
if ( histogramSize != KLMFilter->GetNumberOfRegions() )
{
std::cout << "Test FAILED" << std::endl;
return EXIT_FAILURE;
}
HistogramType::ConstIterator histIt = histogram->Begin();
HistogramType::ConstIterator histItEnd = histogram->End();
double Sum = histogram->GetTotalFrequency();
double labelEntropy = 0.0;
while( histIt != histItEnd )
{
double probability = histIt.GetFrequency() / Sum;
if( probability == 0 )
{
std::cout << "Test FAILED" << std::endl;
return EXIT_FAILURE;
}
labelEntropy -= probability * log( probability );
++histIt;
}
labelEntropy /= log( 2.0 );
double idealEntropy = - log( 8.0 / numPixels ) / log( 2.0 );
std::cout << "Label entropy = " << labelEntropy << " bits " << std::endl;
std::cout << "Ideal entropy = " << idealEntropy << " bits " << std::endl;
if ( vnl_math_abs( idealEntropy - labelEntropy ) > 0.2 )
{
std::cout << "Test FAILED" << std::endl;
return EXIT_FAILURE;
}
std::cout << "Test PASSED" << std::endl;
std::cout << std::endl << "Done testing of two-dimension images" << std::endl;
return EXIT_SUCCESS;
} // End test_regiongrow2D()
unsigned int test_regiongrowKLM3D()
{
itk::OutputWindow::SetInstance(itk::TextOutput::New().GetPointer());
std::cout << std::endl << "Begin testing three-dimension images" << std::endl;
// Manually create an image
typedef itk::Image<itk::Vector<short int,NUMBANDS2>,NUMDIM3D> ImageType;
typedef itk::Image<itk::Vector<float,NUMBANDS2>,NUMDIM3D> OutputImageType;
ImageType::Pointer image = ImageType::New();
ImageType::SizeType imageSize;
imageSize[0] = 10;
imageSize[1] = 20;
imageSize[2] = 3;
unsigned int numPixels = 10 * 20 * 3;
ImageType::IndexType index;
index.Fill(0);
ImageType::RegionType region;
region.SetSize( imageSize );
region.SetIndex( index );
image->SetLargestPossibleRegion( region );
image->SetBufferedRegion( region );
image->Allocate();
typedef ImageType::PixelType ImagePixelType;
typedef itk::ImageRegionIterator< ImageType > ImageIterator;
ImageIterator inIt( image, image->GetBufferedRegion() );
ImageIterator inItEnd = inIt.End();
/* the key test for merging with duplicate borders will
come from an image that looks like this:
x x x x x x x x x x x x x x x x x x x x
x y y y y y y y y y y y y y y y y y y x
x y q q q q q q y y y y q q q q q q y x
x y q q q q q q y y y y q q q q q q y x
x y q z q q z q y y y y q z q q z q y x
x y m z m m z m y y y y m z m m z m y x
x y m m m m m m y y y y m m m m m m y x
x y m m m m m m y y y y m m m m m m y x
x y y y y y y y y y y y y y y y y y y x
x x x x x x x x x x x x x x x x x x x x
Merge this to 8 regions
1 with value x
1 with value y
2 each with value qm
4 each with value z
*/
int inImageVals[] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1,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1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
// Set up the filter
typedef itk::KLMRegionGrowImageFilter<ImageType,OutputImageType>
KLMRegionGrowImageFilterType;
KLMRegionGrowImageFilterType::Pointer KLMFilter =
KLMRegionGrowImageFilterType::New();
KLMRegionGrowImageFilterType::GridSizeType gridSize;
gridSize.Fill(1);
KLMFilter->SetInput( image );
KLMFilter->SetMaximumNumberOfRegions( 2 );
KLMFilter->SetGridSize(gridSize);
typedef KLMRegionGrowImageFilterType::RegionLabelType LabelType;
LabelType labelVals[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1,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