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
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: itkLinearInterpolateImageFunctionTest.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 "itkImage.h"
#include "itkTranslationTransform.h"
#include "itkLinearInterpolateImageFunction.h"
int itkLinearInterpolateImageFunctionTest( int , char*[] )
{
const unsigned int Dimension = 2;
typedef float PixelType;
const unsigned int VectorDimension = 4;
typedef itk::Vector< PixelType, VectorDimension > VectorPixelType;
typedef itk::Image< PixelType, Dimension > ImageType;
typedef itk::Image< VectorPixelType, Dimension > VectorImageType;
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 float CoordRepType;
typedef itk::LinearInterpolateImageFunction<
ImageType, CoordRepType > InterpolatorType;
typedef itk::LinearInterpolateImageFunction<
VectorImageType, CoordRepType > VectorInterpolatorType;
typedef VectorInterpolatorType::OutputType InterpolatedVectorType;
ImageType::Pointer image = ImageType::New();
VectorImageType::Pointer vectorimage = VectorImageType::New();
IndexType start;
start.Fill( 0 );
SizeType size;
size.Fill( 3 );
RegionType region;
region.SetSize( size );
region.SetIndex( start );
image->SetRegions( region );
image->Allocate();
vectorimage->SetRegions( region );
vectorimage->Allocate();
ImageType::PointType origin;
ImageType::SpacingType spacing;
origin.Fill( 0.0 );
spacing.Fill( 1.0 );
image->SetOrigin( origin );
image->SetSpacing( spacing );
vectorimage->SetOrigin( origin );
vectorimage->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;
const PixelType value = x + y * maxx;
image->SetPixel( index, value );
VectorPixelType & vectorpixel = vectorimage->GetPixel( index );
vectorpixel.Fill( value );
std::cout << value << " ";
}
std::cout << std::endl;
}
InterpolatorType::Pointer interpolator = InterpolatorType::New();
interpolator->SetInputImage( image );
VectorInterpolatorType::Pointer vectorinterpolator = VectorInterpolatorType::New();
vectorinterpolator->SetInputImage( vectorimage );
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;
}
const InterpolatedVectorType vectorpixel = vectorinterpolator->Evaluate( point );
const InterpolatedVectorType expectedvector(expectedValue);
const double errornorm = (expectedvector - vectorpixel).GetNorm();
if( errornorm > tolerance )
{
std::cerr << "Error found while computing vector interpolation " << std::endl;
std::cerr << "Point = " << point << std::endl;
std::cerr << "Expected vector = " << expectedvector << std::endl;
std::cerr << "Computed vector = " << vectorpixel << std::endl;
std::cerr << "Difference = " << (expectedvector - vectorpixel) << std::endl;
return EXIT_FAILURE;
}
}
}
}
}
}
return EXIT_SUCCESS;
}
|