1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
/*=========================================================================
*
* Copyright Insight Software Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#include "itkShapeDetectionLevelSetImageFilter.h"
#include "itkCastImageFilter.h"
#include "itkGradientMagnitudeRecursiveGaussianImageFilter.h"
#include "itkSigmoidImageFilter.h"
#include "itkFastMarchingImageFilter.h"
#include "itkBinaryThresholdImageFilter.h"
#include "itkSimilarityIndexImageFilter.h"
/* Uncomment to write out image files */
/*
*/
int itkShapeDetectionLevelSetImageFilterTest(int, char* [] )
{
const unsigned int ImageDimension = 2;
typedef unsigned char PixelType;
typedef float InternalPixelType;
typedef itk::Image<PixelType,ImageDimension> ImageType;
typedef itk::Image<InternalPixelType,ImageDimension> InternalImageType;
ImageType::SizeType imageSize;
imageSize[0] = 128;
imageSize[1] = 128;
ImageType::RegionType imageRegion;
imageRegion.SetSize( imageSize );
/**
* Create an input image.
* A light square on a dark background.
*/
PixelType background = 0;
PixelType foreground = 190;
ImageType::Pointer inputImage = ImageType::New();
inputImage->SetRegions( imageRegion );
inputImage->Allocate();
inputImage->FillBuffer( background );
ImageType::IndexType squareStart;
squareStart.Fill( 20 );
ImageType::SizeType squareSize;
squareSize.Fill( 60 );
ImageType::RegionType squareRegion;
squareRegion.SetIndex( squareStart );
squareRegion.SetSize( squareSize );
typedef itk::ImageRegionIterator<ImageType> Iterator;
Iterator it( inputImage, squareRegion );
it.GoToBegin();
while( !it.IsAtEnd() )
{
it.Set( foreground );
++it;
}
try
{
/**
* Create an edge potential map.
* First compute the image gradient magnitude using a derivative of gaussian filter.
* Then apply a sigmoid function to the gradient magnitude.
*/
typedef itk::CastImageFilter< ImageType, InternalImageType > CastFilterType;
CastFilterType::Pointer caster = CastFilterType::New();
caster->SetInput( inputImage );
typedef itk::GradientMagnitudeRecursiveGaussianImageFilter< InternalImageType,
InternalImageType > GradientImageType;
GradientImageType::Pointer gradMagnitude = GradientImageType::New();
gradMagnitude->SetInput( caster->GetOutput() );
gradMagnitude->SetSigma( 1.0 );
typedef itk::SigmoidImageFilter< InternalImageType, InternalImageType >
SigmoidFilterType;
SigmoidFilterType::Pointer sigmoid = SigmoidFilterType::New();
sigmoid->SetOutputMinimum( 0.0 );
sigmoid->SetOutputMaximum( 1.0 );
sigmoid->SetAlpha( -0.4 );
sigmoid->SetBeta( 2.5 );
sigmoid->SetInput( gradMagnitude->GetOutput() );
/**
* Create an initial level.
* Use fast marching to create an signed distance from a seed point.
*/
typedef itk::FastMarchingImageFilter<InternalImageType> FastMarchingFilterType;
FastMarchingFilterType::Pointer fastMarching = FastMarchingFilterType::New();
typedef FastMarchingFilterType::NodeContainer NodeContainer;
typedef FastMarchingFilterType::NodeType NodeType;
NodeContainer::Pointer seeds = NodeContainer::New();
// Choose an initial contour that is wholly within the square to be segmented.
InternalImageType::IndexType seedPosition;
seedPosition[0] = 47;
seedPosition[1] = 47;
NodeType node;
node.SetValue( -5.0 );
node.SetIndex( seedPosition );
seeds->Initialize();
seeds->InsertElement( 0, node );
fastMarching->SetTrialPoints( seeds );
fastMarching->SetSpeedConstant( 1.0 );
fastMarching->SetOutputSize( imageSize );
/**
* Set up and run the shape detection filter
*/
typedef itk::ShapeDetectionLevelSetImageFilter<
InternalImageType, InternalImageType > ShapeDetectionFilterType;
ShapeDetectionFilterType::Pointer shapeDetection = ShapeDetectionFilterType::New();
// set the initial level set
shapeDetection->SetInput( fastMarching->GetOutput() );
// set the edge potential image
shapeDetection->SetFeatureImage( sigmoid->GetOutput() );
// set the weights between the propagation and curvature terms
shapeDetection->SetPropagationScaling( 1.0 );
shapeDetection->SetCurvatureScaling( 0.1 );
// set the convergence criteria
shapeDetection->SetMaximumRMSError( 0.02 );
shapeDetection->SetNumberOfIterations( 200 );
/**
* Threshold the output level set to display the final contour.
*/
typedef itk::BinaryThresholdImageFilter< InternalImageType, ImageType >
ThresholdFilterType;
ThresholdFilterType::Pointer thresholder = ThresholdFilterType::New();
thresholder->SetInput( shapeDetection->GetOutput() );
thresholder->SetLowerThreshold( -1e+10 );
thresholder->SetUpperThreshold( 0.0 );
thresholder->SetOutsideValue( 0 );
thresholder->SetInsideValue( 255 );
/**
* Compute overlap between the true shape and the segmented shape
*/
typedef itk::SimilarityIndexImageFilter< ImageType, ImageType >
OverlapCalculatorType;
OverlapCalculatorType::Pointer overlap = OverlapCalculatorType::New();
overlap->SetInput1( inputImage );
overlap->SetInput2( thresholder->GetOutput() );
overlap->Update();
/** Printout useful information from the shape detection filter. */
std::cout << "Max. no. iterations: " << shapeDetection->GetNumberOfIterations() << std::endl;
std::cout << "Max. RMS error: " << shapeDetection->GetMaximumRMSError() << std::endl;
std::cout << "No. elpased iterations: " << shapeDetection->GetElapsedIterations() << std::endl;
std::cout << "RMS change: " << shapeDetection->GetRMSChange() << std::endl;
std::cout << "Overlap: " << overlap->GetSimilarityIndex() << std::endl;
/**
* Uncomment to write out image files.
*/
/*
typedef itk::ImageFileWriter< ImageType > WriterType;
WriterType::Pointer writer = WriterType::New();
typedef itk::RescaleIntensityImageFilter< InternalImageType,
ImageType > RescaleFilterType;
RescaleFilterType::Pointer rescaler = RescaleFilterType::New();
writer->SetFileName( "inputImage.png" );
writer->SetInput( inputImage );
writer->Update();
rescaler->SetInput( gradMagnitude->GetOutput() );
rescaler->SetOutputMinimum( 0 );
rescaler->SetOutputMaximum( 255 );
writer->SetFileName( "gradMagnitude.png" );
writer->SetInput( rescaler->GetOutput() );
writer->Update();
rescaler->SetInput( sigmoid->GetOutput() );
writer->SetFileName( "edgePotential.png" );
writer->Update();
writer->SetInput( thresholder->GetOutput() );
writer->SetFileName( "outputLevelSet.png" );
writer->Update();
thresholder->SetInput( fastMarching->GetOutput() );
writer->SetInput( thresholder->GetOutput() );
writer->SetFileName( "initialLevelSet.png" );
writer->Update();
*/
// Check of overlap is above threshold
if ( overlap->GetSimilarityIndex() > 0.90 )
{
std::cout << "Overlap exceed threshold." << std::endl;
}
else
{
std::cout << "Overlap below threshold." << std::endl;
std::cout << "Test failed." << std::endl;
return EXIT_FAILURE;
}
// Test case when PropagationScaling is zero
shapeDetection->SetPropagationScaling( 0.0 );
shapeDetection->SetCurvatureScaling( 1.0 );
shapeDetection->Update();
std::cout << "Test Passed. " << std::endl;
return EXIT_SUCCESS;
}
catch( itk::ExceptionObject& err )
{
std::cout << err << std::endl;
return EXIT_FAILURE;
}
catch( ... )
{
std::cout << "Caught unknown exception" << std::endl;
return EXIT_FAILURE;
}
}
|