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
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkLinearInterpolateImageFunctionTest.cxx,v $
Language: C++
Date: $Date: 2008-01-07 00:56:16 $
Version: $Revision: 1.2 $
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 "itkImage.h"
#include "itkTranslationTransform.h"
#include "itkLinearInterpolateImageFunction.h"
int itkLinearInterpolateImageFunctionTest( int , char*[] )
{
const unsigned int Dimension = 2;
typedef float PixelType;
typedef itk::Image< PixelType, Dimension > ImageType;
typedef ImageType::RegionType RegionType;
typedef RegionType::SizeType SizeType;
typedef ImageType::IndexType IndexType;
typedef itk::ContinuousIndex<float, 2> ContinuousIndexType;
typedef itk::Point<float,2> PointType;
typedef itk::LinearInterpolateImageFunction< ImageType > InterpolatorType;
ImageType::Pointer image = ImageType::New();
IndexType start;
start.Fill( 0 );
SizeType size;
size.Fill( 3 );
RegionType region;
region.SetSize( size );
region.SetIndex( start );
image->SetRegions( region );
image->Allocate();
ImageType::PointType origin;
ImageType::SpacingType spacing;
origin.Fill( 0.0 );
spacing.Fill( 1.0 );
image->SetOrigin( origin );
image->SetSpacing( spacing );
image->Print( std::cout );
unsigned int maxx = 3;
unsigned int maxy = 3;
//
// Fill up the image values with the function
//
// Intensity = f(x,y) = x + 3 * y
//
//
for (unsigned int y = 0; y < maxy; y++)
{
for (unsigned int x = 0; x < maxx; x++)
{
IndexType index;
index[0] = x;
index[1] = y;
PixelType value = x + y * maxx;
image->SetPixel( index, value );
std::cout << value << " ";
}
std::cout << std::endl;
}
InterpolatorType::Pointer interpolator = InterpolatorType::New();
interpolator->SetInputImage( image );
const double incr = 0.1;
const double tolerance = 1e-6;
PointType point;
for (double yy = 0; yy < static_cast<double>(maxy-1); yy++)
{
for (double xx = 0; xx < static_cast<double>(maxx-1); xx++)
{
for (double yyy = yy; yyy < yy + 1.01; yyy += incr)
{
for (double xxx = xx; xxx < xx + 1.01; xxx += incr)
{
point[0] = xxx;
point[1] = yyy;
if( interpolator->IsInsideBuffer( point ) )
{
const double expectedValue = xxx + 3.0 * yyy;
const double computedValue = interpolator->Evaluate( point );
const double difference = expectedValue - computedValue;
if( vcl_fabs( difference ) > tolerance )
{
std::cerr << "Error found while computing interpolation " << std::endl;
std::cerr << "Point = " << point << std::endl;
std::cerr << "Expected value = " << expectedValue << std::endl;
std::cerr << "Computed value = " << computedValue << std::endl;
std::cerr << "Difference = " << difference << std::endl;
return EXIT_FAILURE;
}
}
}
}
}
}
return EXIT_SUCCESS;
}
|