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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: itkExpandImageFilterTest.cxx
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
#include <iostream>
#include "itkIndex.h"
#include "itkImage.h"
#include "itkImageRegionIteratorWithIndex.h"
#ifndef ITK_USE_CENTERED_PIXEL_COORDINATES_CONSISTENTLY
#include "itkLinearInterpolateImageFunction.h"
#else
#include "itkNearestNeighborInterpolateImageFunction.h"
#endif
#include "itkExpandImageFilter.h"
#include "itkCastImageFilter.h"
#include "itkStreamingImageFilter.h"
#include "itkCommand.h"
#include "vnl/vnl_math.h"
// class to produce a linear image pattern
template <int VDimension>
class ImagePattern
{
public:
typedef itk::Index<VDimension> IndexType;
ImagePattern()
{
offset = 0.0;
for( int j = 0; j < VDimension; j++ )
{
coeff[j] = 0.0;
}
}
double Evaluate( const IndexType& index )
{
double accum = offset;
for( int j = 0; j < VDimension; j++ )
{
accum += coeff[j] * (double) index[j];
}
return accum;
}
double coeff[VDimension];
double offset;
};
// The following three classes are used to support callbacks
// on the filter in the pipeline that follows later
class ShowProgressObject
{
public:
ShowProgressObject(itk::ProcessObject* o)
{m_Process = o;}
void ShowProgress()
{std::cout << "Progress " << m_Process->GetProgress() << std::endl;}
itk::ProcessObject::Pointer m_Process;
};
int itkExpandImageFilterTest(int, char* [] )
{
typedef float PixelType;
enum { ImageDimension = 2 };
typedef itk::Image<PixelType,ImageDimension> ImageType;
bool testPassed = true;
//=============================================================
std::cout << "Create the input image pattern." << std::endl;
ImageType::RegionType region;
ImageType::SizeType size = {{64, 64}};
region.SetSize( size );
ImageType::Pointer input = ImageType::New();
input->SetLargestPossibleRegion( region );
input->SetBufferedRegion( region );
input->Allocate();
int j;
ImagePattern<ImageDimension> pattern;
pattern.offset = 64;
for( j = 0; j < ImageDimension; j++ )
{
pattern.coeff[j] = 1.0;
}
typedef itk::ImageRegionIteratorWithIndex<ImageType> Iterator;
Iterator inIter( input, region );
while( !inIter.IsAtEnd() )
{
inIter.Set( pattern.Evaluate( inIter.GetIndex() ) );
++inIter;
}
//=============================================================
std::cout << "Run ExpandImageFilter in standalone mode with progress.";
std::cout << std::endl;
typedef itk::ExpandImageFilter<ImageType,ImageType> ExpanderType;
ExpanderType::Pointer expander = ExpanderType::New();
#ifndef ITK_USE_CENTERED_PIXEL_COORDINATES_CONSISTENTLY
typedef itk::LinearInterpolateImageFunction<ImageType,double> InterpolatorType;
#else
typedef itk::NearestNeighborInterpolateImageFunction<ImageType,double> InterpolatorType;
#endif
InterpolatorType::Pointer interpolator = InterpolatorType::New();
expander->SetInterpolator( interpolator );
std::cout << "Interpolator: " << expander->GetInterpolator() << std::endl;
expander->SetExpandFactors( 5 );
unsigned int factors[ImageDimension] = {2,3};
ImageType::PixelType padValue = 4.0;
expander->SetInput( input );
expander->SetExpandFactors( factors );
expander->SetEdgePaddingValue( padValue );
ShowProgressObject progressWatch(expander);
itk::SimpleMemberCommand<ShowProgressObject>::Pointer command;
command = itk::SimpleMemberCommand<ShowProgressObject>::New();
command->SetCallbackFunction(&progressWatch,
&ShowProgressObject::ShowProgress);
expander->AddObserver(itk::ProgressEvent(), command);
expander->Print( std::cout );
expander->Update();
//=============================================================
std::cout << "Checking the output against expected." << std::endl;
Iterator outIter( expander->GetOutput(),
expander->GetOutput()->GetBufferedRegion() );
// compute non-padded output region
ImageType::RegionType validRegion =
expander->GetOutput()->GetLargestPossibleRegion();
ImageType::SizeType validSize = validRegion.GetSize();
#ifndef ITK_USE_CENTERED_PIXEL_COORDINATES_CONSISTENTLY
for( j = 0; j < ImageDimension; j++ )
{
validSize[j] -= (factors[j] - 1);
}
#endif
validRegion.SetSize( validSize );
#ifndef ITK_USE_CENTERED_PIXEL_COORDINATES_CONSISTENTLY
// adjust the pattern coefficients to match
for( j = 0; j < ImageDimension; j++ )
{
pattern.coeff[j] /= (double) factors[j];
}
#endif
while( !outIter.IsAtEnd() )
{
ImageType::IndexType index = outIter.GetIndex();
double value = outIter.Get();
if( validRegion.IsInside( index ) )
{
#ifndef ITK_USE_CENTERED_PIXEL_COORDINATES_CONSISTENTLY
double trueValue = pattern.Evaluate( outIter.GetIndex() );
#else
ImageType::PointType point;
ImageType::IndexType inputIndex;
expander->GetOutput()->TransformIndexToPhysicalPoint( outIter.GetIndex(), point );
input->TransformPhysicalPointToIndex(point, inputIndex );
double trueValue = pattern.Evaluate( inputIndex );
#endif
if( vnl_math_abs( trueValue - value ) > 1e-4 )
{
testPassed = false;
std::cout << "Error at Index: " << index << " ";
std::cout << "Expected: " << trueValue << " ";
std::cout << "Actual: " << value << std::endl;
}
}
else
{
if( value != padValue )
{
testPassed = false;
std::cout << "Error at Index: " << index << " ";
std::cout << "Expected: " << padValue << " ";
std::cout << "Actual: " << value << std::endl;
}
}
++outIter;
}
//=============================================================
std::cout << "Run ExpandImageFilter with streamer";
std::cout << std::endl;
typedef itk::CastImageFilter<ImageType,ImageType> CasterType;
CasterType::Pointer caster = CasterType::New();
caster->SetInput( expander->GetInput() );
ExpanderType::Pointer expander2 = ExpanderType::New();
expander2->SetInput( caster->GetOutput() );
expander2->SetExpandFactors( expander->GetExpandFactors() );
expander2->SetEdgePaddingValue( expander->GetEdgePaddingValue() );
expander2->SetInterpolator( expander->GetInterpolator() );
typedef itk::StreamingImageFilter<ImageType,ImageType> StreamerType;
StreamerType::Pointer streamer = StreamerType::New();
streamer->SetInput( expander2->GetOutput() );
streamer->SetNumberOfStreamDivisions( 3 );
streamer->Update();
//=============================================================
std::cout << "Compare standalone and streamed outputs" << std::endl;
Iterator streamIter( streamer->GetOutput(),
streamer->GetOutput()->GetBufferedRegion() );
outIter.GoToBegin();
streamIter.GoToBegin();
while( !outIter.IsAtEnd() )
{
if( outIter.Get() != streamIter.Get() )
{
testPassed = false;
}
++outIter;
++streamIter;
}
if ( !testPassed )
{
std::cout << "Test failed." << std::endl;
return EXIT_FAILURE;
}
// Test error handling
try
{
testPassed = false;
std::cout << "Setting Input to NULL" << std::endl;
expander->SetInput( NULL );
expander->Update();
}
catch( itk::ExceptionObject& err )
{
std::cout << err << std::endl;
expander->ResetPipeline();
expander->SetInput( input );
testPassed = true;
}
if ( !testPassed )
{
std::cout << "Test failed." << std::endl;
return EXIT_FAILURE;
}
try
{
testPassed = false;
std::cout << "Setting Interpolator to NULL" << std::endl;
expander->SetInterpolator( NULL );
expander->Update();
}
catch( itk::ExceptionObject& err )
{
std::cout << err << std::endl;
expander->ResetPipeline();
expander->SetInterpolator( interpolator );
testPassed = true;
}
if ( !testPassed )
{
std::cout << "Test failed." << std::endl;
return EXIT_FAILURE;
}
std::cout << "Test passed." << std::endl;
return EXIT_SUCCESS;
}
|