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
|
/*=========================================================================
*
* Copyright UMC Utrecht and contributors
*
* 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.
*
*=========================================================================*/
/** \file
\brief Compare the advanced linear interpolator with the linear and 1st order B-spline.
*/
#include "itkLinearInterpolateImageFunction.h"
#include "itkAdvancedLinearInterpolateImageFunction.h"
#include "itkBSplineInterpolateImageFunction.h"
#include "itkImage.h"
#include "itkImageRegionIterator.h"
#include "itkMersenneTwisterRandomVariateGenerator.h"
#include "itkImageFileWriter.h"
#include "itkTimeProbe.h"
#include <cmath> // For abs.
//-------------------------------------------------------------------------------------
// Test function templated over the dimension
template< unsigned int Dimension >
bool
TestInterpolators( void )
{
typedef itk::Image< short, Dimension > InputImageType;
typedef typename InputImageType::SizeType SizeType;
typedef typename InputImageType::SpacingType SpacingType;
typedef typename InputImageType::PointType OriginType;
typedef typename InputImageType::RegionType RegionType;
//typedef typename RegionType::IndexType IndexType;
typedef typename InputImageType::DirectionType DirectionType;
typedef double CoordRepType;
typedef double CoefficientType;
typedef itk::LinearInterpolateImageFunction<
InputImageType, CoordRepType > LinearInterpolatorType;
typedef itk::AdvancedLinearInterpolateImageFunction<
InputImageType, CoordRepType > AdvancedLinearInterpolatorType;
typedef itk::BSplineInterpolateImageFunction<
InputImageType, CoordRepType, CoefficientType > BSplineInterpolatorType;
typedef typename LinearInterpolatorType::ContinuousIndexType ContinuousIndexType;
typedef typename AdvancedLinearInterpolatorType::CovariantVectorType CovariantVectorType;
typedef typename AdvancedLinearInterpolatorType::OutputType OutputType; // double scalar
typedef itk::ImageRegionIterator< InputImageType > IteratorType;
typedef itk::Statistics::MersenneTwisterRandomVariateGenerator RandomNumberGeneratorType;
//typedef itk::ImageFileWriter< InputImageType > WriterType;
RandomNumberGeneratorType::Pointer randomNum = RandomNumberGeneratorType::GetInstance();
/** Create random input image. */
SizeType size; SpacingType spacing; OriginType origin;
for( unsigned int i = 0; i < Dimension; ++i )
{
size[ i ] = 10;
spacing[ i ] = randomNum->GetUniformVariate( 0.5, 2.0 );
origin[ i ] = randomNum->GetUniformVariate( -1, 0 );
}
RegionType region; region.SetSize( size );
/** Make sure to test for non-identity direction cosines. */
DirectionType direction; direction.Fill( 0.0 );
if( Dimension == 2 )
{
direction[ 0 ][ 1 ] = -1.0;
direction[ 1 ][ 0 ] = 1.0;
}
else if( Dimension == 3 )
{
direction[ 0 ][ 2 ] = -1.0;
direction[ 1 ][ 1 ] = 1.0;
direction[ 2 ][ 0 ] = 1.0;
}
typename InputImageType::Pointer image = InputImageType::New();
image->SetRegions( region );
image->SetOrigin( origin );
image->SetSpacing( spacing );
image->SetDirection( direction );
image->Allocate();
// loop over image and fill with random values
IteratorType it( image, image->GetLargestPossibleRegion() );
it.GoToBegin();
while( !it.IsAtEnd() )
{
it.Set( randomNum->GetUniformVariate( 0, 255 ) );
++it;
}
/** Write the image. */
//WriterType::Pointer writer = WriterType::New();
//writer->SetInput( image );
//writer->SetFileName( "image.mhd" );
//writer->Update();
/** Create and setup interpolators. */
typename LinearInterpolatorType::Pointer linear = LinearInterpolatorType::New();
typename AdvancedLinearInterpolatorType::Pointer linearA = AdvancedLinearInterpolatorType::New();
typename BSplineInterpolatorType::Pointer bspline = BSplineInterpolatorType::New();
linear->SetInputImage( image );
linearA->SetInputImage( image );
bspline->SetSplineOrder( 1 ); // prior to SetInputImage()
bspline->SetInputImage( image );
/** Test some points. */
const unsigned int count = 12;
double darray1[ 12 ][ Dimension ];
if( Dimension == 2 )
{
double darray2[ 12 ][ 2 ] =
{ { 0.1, 0.2 }, { 3.4, 5.8 }, { 4.0, 6.0 }, { 2.1, 8.0 },
{ -0.1, -0.1 }, { 0.0, 0.0 }, { 1.3, 1.0 }, { 2.0, 5.7 },
{ 9.5, 9.1 }, { 2.0, -0.1 }, { -0.1, 2.0 }, { 12.7, 15.3 } };
for( unsigned int i = 0; i < 12; i++ )
{
for( unsigned int j = 0; j < Dimension; j++ )
{
darray1[ i ][ j ] = darray2[ i ][ j ];
}
}
}
else if( Dimension == 3 )
{
//double darray2[count][3] =
//{ { 0.0, 0.0, 0.0}, { 0.1, 0.0, 0.0}, { 0.2, 0.0, 0.0} }; // x, y=z=0, works
//{ { 0.0, 0.5, 0.0}, { 0.1, 0.5, 0.0}, { 0.2, 0.5, 0.0} }; // x, z=0, works
//{ { 0.0, 0.0, 0.5}, { 0.1, 0.0, 0.5}, { 0.2, 0.0, 0.5} }; // x, y=0, works
//{ { 0.0, 0.2, 0.2}, { 0.0, 0.4, 0.4}, { 0.0, 0.5, 0.5} }; // x=0, y=z, works
//{ { 0.0, 0.0, 0.0}, { 0.0, 0.1, 0.0}, { 0.0, 0.2, 0.0} }; // y, works
//{ { 0.0, 0.0, 0.0}, { 0.0, 0.0, 0.1}, { 0.0, 0.0, 0.2} }; // z, works
//{ { 0.0, 0.0, 0.0}, { 0.2, 0.1, 0.0}, { 0.5, 0.2, 0.0} }; // xy, works
//{ { 0.0, 0.0, 0.0}, { 0.3, 0.0, 0.1}, { 0.5, 0.0, 0.2} }; // xz, works
//{ { 0.0, 0.0, 0.0}, { 0.0, 0.1, 0.1}, { 0.0, 0.4, 0.2} }; // yz, works
double darray2[ 12 ][ 3 ] =
{ { 0.1, 0.2, 0.1 }, { 3.4, 5.8, 4.7 }, { 4.0, 6.0, 5.0 }, { 2.1, 8.0, 3.4 },
{ -0.1, -0.1, -0.1 }, { 0.0, 0.0, 0.0 }, { 1.3, 1.0, 1.4 }, { 2.0, 5.7, 7.5 },
{ 9.5, 9.1, 9.3 }, { 2.0, -0.1, 5.3 }, { -0.1, 2.0, 4.0 }, { 12.7, 15.3, 14.1 } };
for( unsigned int i = 0; i < count; i++ )
{
for( unsigned int j = 0; j < Dimension; j++ )
{
darray1[ i ][ j ] = darray2[ i ][ j ];
}
}
}
/** Compare results. */
OutputType valueLinA, valueBSpline, valueBSpline2;
CovariantVectorType derivLinA, derivBSpline, derivBSpline2;
for( unsigned int i = 0; i < count; i++ )
{
ContinuousIndexType cindex( &darray1[ i ][ 0 ] );
linearA->EvaluateValueAndDerivativeAtContinuousIndex( cindex, valueLinA, derivLinA );
valueBSpline = bspline->EvaluateAtContinuousIndex( cindex );
derivBSpline = bspline->EvaluateDerivativeAtContinuousIndex( cindex );
bspline->EvaluateValueAndDerivativeAtContinuousIndex( cindex, valueBSpline2, derivBSpline2 );
std::cout << "cindex: " << cindex << std::endl;
if (linear->IsInsideBuffer(cindex))
{
std::cout << "linear: " << linear->EvaluateAtContinuousIndex(cindex) << " ---" << std::endl;
}
else
{
std::cout << "linear: --- ---" << std::endl;
}
std::cout << "linearA: " << valueLinA << " " << derivLinA << std::endl;
std::cout << "B-spline: " << valueBSpline << " " << derivBSpline << std::endl;
std::cout << "B-spline: " << valueBSpline2 << " " << derivBSpline2 << "\n" << std::endl;
if( std::abs( valueLinA - valueBSpline ) > 1.0e-3 )
{
std::cerr << "ERROR: there is a difference in the interpolated value, "
<< "between the linear and the 1st-order B-spline interpolator." << std::endl;
return false;
}
if( std::abs( valueBSpline - valueBSpline2 ) > 1.0e-3 )
{
std::cerr << "ERROR: there is a difference in the interpolated value, "
<< "within the 1st-order B-spline interpolator (inconsistency)." << std::endl;
return false;
}
if( ( derivLinA - derivBSpline ).GetVnlVector().magnitude() > 1.0e-3 )
{
std::cerr << "ERROR: there is a difference in the interpolated gradient, "
<< "between the linear and the 1st-order B-spline interpolator." << std::endl;
return false;
}
if( ( derivBSpline - derivBSpline2 ).GetVnlVector().magnitude() > 1.0e-3 )
{
std::cerr << "ERROR: there is a difference in the interpolated gradient, "
<< "within the 1st-order B-spline interpolator (inconsistency)." << std::endl;
return false;
}
}
/** Measure the run times, but only in release mode. */
#ifdef NDEBUG
std::cout << std::endl;
ContinuousIndexType cindex( &darray1[ 1 ][ 0 ] );
std::cout << "cindex: " << cindex << std::endl;
OutputType value; CovariantVectorType deriv;
const unsigned int runs = 1e5;
itk::TimeProbe timer;
timer.Start();
for( unsigned int i = 0; i < runs; ++i )
{
value = linear->EvaluateAtContinuousIndex( cindex );
}
timer.Stop();
std::cout << "linear (value) : "
<< 1.0e3 * timer.GetMean() / static_cast< double >( runs )
<< " ms" << std::endl;
timer.Reset(); timer.Start();
for( unsigned int i = 0; i < runs; ++i )
{
linearA->EvaluateValueAndDerivativeAtContinuousIndex( cindex, value, deriv );
}
timer.Stop();
std::cout << "linearA (v&d) : "
<< 1.0e3 * timer.GetMean() / static_cast< double >( runs )
<< " ms" << std::endl;
timer.Reset(); timer.Start();
for( unsigned int i = 0; i < runs; ++i )
{
value = bspline->EvaluateAtContinuousIndex( cindex );
}
timer.Stop();
std::cout << "B-spline (value): "
<< 1.0e3 * timer.GetMean() / static_cast< double >( runs )
<< " ms" << std::endl;
timer.Reset(); timer.Start();
for( unsigned int i = 0; i < runs; ++i )
{
value = bspline->EvaluateAtContinuousIndex( cindex );
deriv = bspline->EvaluateDerivativeAtContinuousIndex( cindex );
}
timer.Stop();
std::cout << "B-spline (v+d) : "
<< 1.0e3 * timer.GetMean() / static_cast< double >( runs )
<< " ms" << std::endl;
timer.Reset(); timer.Start();
for( unsigned int i = 0; i < runs; ++i )
{
bspline->EvaluateValueAndDerivativeAtContinuousIndex( cindex, value, deriv );
}
timer.Stop();
std::cout << "B-spline (v&d) : "
<< 1.0e3 * timer.GetMean() / static_cast< double >( runs )
<< " ms" << std::endl;
#endif
return true;
} // end TestInterpolator()
int
main( int argc, char ** argv )
{
// 2D tests
bool success = TestInterpolators< 2 >();
if( !success ) { return EXIT_FAILURE; }
std::cerr << "\n\n\n-----------------------------------\n\n\n";
// 3D tests
success = TestInterpolators< 3 >();
if( !success ) { return EXIT_FAILURE; }
return EXIT_SUCCESS;
} // end main
|