📄 itkregiongrow2dtest.cxx
字号:
++labelIt5;
++inIt;
}
pixelOut5in /= gridWidth;
pixelOut5in /= spacing[0];
for ( int idx = 0; idx < gridWidth; idx++ )
{
pixelOut5out = outIt5.Get();
std::cout << "idx: " << idx << " pixelOut5out: " << pixelOut5out << std::endl;
if ( pixelOut5in != pixelOut5out )
{
std::cout << "Test FAILED" << std::endl;
std::cout << "pixelOut5in: " << pixelOut5in
<< " != "
<< "pixelOut5out: " << pixelOut5out
<< std::endl;
return EXIT_FAILURE;
}
++outIt5;
}
++k;
} //end while iterator loop
std::cout << "Test PASSED" << std::endl;
std::cout << std::endl << "Done testing of one-dimension images" << std::endl;
return EXIT_SUCCESS;
}
unsigned int test_regiongrowKLM2D()
{
itk::OutputWindow::SetInstance(itk::TextOutput::New().GetPointer());
std::cout << std::endl << "Begin testing two-dimension images" << std::endl;
// Manually create an image
typedef itk::Image<itk::Vector<int,NUMBANDS2>,NUMDIM2D> ImageType;
typedef itk::Image<itk::Vector<double,NUMBANDS2>,NUMDIM2D> OutputImageType;
ImageType::Pointer image = ImageType::New();
ImageType::SizeType imageSize;
imageSize[0] = 10;
imageSize[1] = 20;
unsigned int numPixels = 200;
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, 9, 9, 9, 9, 9, 9, 9, 9, 1,
1, 9, 7, 7, 7, 6, 6, 6, 9, 1,
1, 9, 7, 7, 7, 6, 6, 6, 9, 1,
1, 9, 7, 7, 3, 3, 6, 6, 9, 1,
1, 9, 7, 7, 7, 6, 6, 6, 9, 1,
1, 9, 7, 7, 3, 3, 6, 6, 9, 1,
1, 9, 7, 7, 7, 6, 6, 6, 9, 1,
1, 9, 9, 9, 9, 9, 9, 9, 9, 1,
1, 9, 9, 9, 9, 9, 9, 9, 9, 1,
1, 9, 9, 9, 9, 9, 9, 9, 9, 1,
1, 9, 9, 9, 9, 9, 9, 9, 9, 1,
1, 9, 7, 7, 7, 6, 6, 6, 9, 1,
1, 9, 7, 7, 3, 3, 6, 6, 9, 1,
1, 9, 7, 7, 7, 6, 6, 6, 9, 1,
1, 9, 7, 7, 7, 6, 6, 6, 9, 1,
1, 9, 7, 7, 3, 3, 6, 6, 9, 1,
1, 9, 7, 7, 7, 6, 6, 6, 9, 1,
1, 9, 9, 9, 9, 9, 9, 9, 9, 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, 2, 2, 2, 2, 2, 2, 2, 2, 1,
1, 2, 3, 3, 3, 3, 3, 3, 2, 1,
1, 2, 3, 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.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 1.0,
1.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 1.0,
1.0, 9.0, 6.5, 6.5, 6.5, 6.5, 6.5, 6.5, 9.0, 1.0,
1.0, 9.0, 6.5, 6.5, 3.0, 3.0, 6.5, 6.5, 9.0, 1.0,
1.0, 9.0, 6.5, 6.5, 6.5, 6.5, 6.5, 6.5, 9.0, 1.0,
1.0, 9.0, 6.5, 6.5, 6.5, 6.5, 6.5, 6.5, 9.0, 1.0,
1.0, 9.0, 6.5, 6.5, 3.0, 3.0, 6.5, 6.5, 9.0, 1.0,
1.0, 9.0, 6.5, 6.5, 6.5, 6.5, 6.5, 6.5, 9.0, 1.0,
1.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1 };
typedef ImageType::PixelType::VectorType ImageData;
ImageData pixelData;
int k = 0;
while ( inIt != inItEnd ) {
pixelData[0] = inImageVals[k];
pixelData[1] = 100 - pixelData[0];
inIt.Set( pixelData );
++inIt;
++k;
}
// FIRST TEST:
// If lambda is negative, the number of final regions should equal
// initial number of regions, the region labels should be consecutive
std::cout << std::endl << "First test, lambda = -1" << std::endl;
KLMFilter->SetMaximumLambda( -1 );
// Kick off the Region grow function
LOCAL_TEST_EXCEPTION_MACRO( KLMFilter );
KLMFilter->Print(std::cout);
// This should return unique integer labels of the segmented regions.
// The region labels should be consecutive integers beginning with 1.
if( numPixels != KLMFilter->GetNumberOfRegions() )
{
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 outImage = KLMFilter->GetOutput();
typedef itk::ImageRegionIterator< OutputImageType > OutputImageIterator;
inIt.GoToBegin();
OutputImageIterator outIt( outImage, outImage->GetBufferedRegion() );
typedef OutputImageType::PixelType::VectorType OutputImageData;
ImageData pixelIn;
OutputImageData pixelOut;
while ( inIt != inItEnd )
{
pixelOut = outIt.Get();
pixelIn = inIt.Get();
if ( pixelOut[0] != pixelIn[0] || pixelOut[1] != pixelIn[1] )
{
std::cout << "Test FAILED" << std::endl;
if ( pixelOut[0] != pixelIn[0] )
{
std::cout << "pixelOut[0]: " << pixelOut[0]
<< " != "
<< "pixelIn[0]: " << pixelIn[0]
<< std::endl;
}
if ( pixelOut[1] != pixelIn[1] )
{
std::cout << "pixelOut[1]: " << pixelOut[1]
<< " != "
<< "pixelIn[1]: " << pixelIn[1]
<< std::endl;
}
return EXIT_FAILURE;
}
++outIt;
++inIt;
} //end while iterator loop
// Make sure that the labelled image type is set to unsigned integer
// as labels associated with different regions are always integers
std::cout << "Extracting and checking label image" << std::endl;
typedef itk::Image<LabelType, NUMDIM2D> LabelledImageType;
LabelledImageType::Pointer labelledImage = KLMFilter->GetLabelledImage();
// Loop through the approximation image and check if they match the
// input image
// setup the iterators
typedef LabelledImageType::PixelType LabelledImagePixelType;
typedef itk::ImageRegionIterator< LabelledImageType > LabelImageIterator;
LabelImageIterator
labelIt( labelledImage, labelledImage->GetBufferedRegion() );
LabelType pixelLabel;
LabelType m = 1;
while(!labelIt.IsAtEnd())
{
pixelLabel = labelIt.Get();
if(pixelLabel != m)
{
std::cout << "Test FAILED" << std::endl;
std::cout << "pixelLabel: " << pixelLabel
<< " != "
<< "m: " << m
<< std::endl;
return EXIT_FAILURE;
}
++labelIt;
++m;
} //end while iterator loop
std::cout << "Test PASSED" << std::endl;
KLMFilter->ResetPipeline();
// SECOND TEST:
// the key test for union borders
std::cout << std::endl <<
"Second test, key merging test containing duplicate borders" << std::endl;
KLMFilter->SetMaximumLambda( 1e45 );
unsigned int nregions = 8;
KLMFilter->SetMaximumNumberOfRegions( nregions );
// Kick off the Region grow function
LOCAL_TEST_EXCEPTION_MACRO( KLMFilter );
KLMFilter->Print(std::cout);
// This should return unique integer labels of the segmented regions.
// The region labels should be consecutive integers beginning with 1.
if( nregions != KLMFilter->GetNumberOfRegions() )
{
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 outImage2 = KLMFilter->GetOutput();
OutputImageIterator outIt2( outImage2, outImage2->GetBufferedRegion() );
OutputImageIterator outItEnd2 = outIt2.End();
k = 0;
while ( outIt2 != outItEnd2 )
{
pixelOut = outIt2.Get();
if ( pixelOut[0] != outImageVals[k] ||
pixelOut[1] != 100 - pixelOut[0] )
{
std::cout << "Test FAILED" << std::endl;
if ( pixelOut[0] != outImageVals[k] )
{
std::cout << "pixelOut[0]: " << pixelOut[0]
<< " != "
<< "outImageVals[k]: " << outImageVals[k]
<< std::endl;
}
if ( pixelOut[1] != (100 - pixelOut[0]) )
{
std::cout << "pixelOut[1]: " << pixelOut[1]
<< " != "
<< "100 - pixelOut[0]: " << 100 - pixelOut[0]
<< std::endl;
}
return EXIT_FAILURE;
}
++outIt2;
++k;
} //end while iterator loop
std::cout << "Extracting and checking label image" << std::endl;
LabelledImageType::Pointer labelledImage2 = KLMFilter->GetLabelledImage();
LabelImageIterator
labelIt2( labelledImage2, labelledImage2->GetBufferedRegion() );
k = 0;
while(!labelIt2.IsAtEnd())
{
pixelLabel = labelIt2.Get();
if(pixelLabel != labelVals[k])
{
std::cout << "Test FAILED" << std::endl;
std::cout << "pixelLabel: " << pixelLabel
<< " != "
<< "labelVals[k]: " << labelVals[k]
<< std::endl;
return EXIT_FAILURE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -