1
/*=========================================================================
3
Program: Insight Segmentation & Registration Toolkit
4
Module: $RCSfile: itkOrientedImage2DTest.cxx,v $
6
Date: $Date: 2007-12-20 18:37:03 $
7
Version: $Revision: 1.5 $
9
Copyright (c) Insight Software Consortium. All rights reserved.
10
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
12
This software is distributed WITHOUT ANY WARRANTY; without even
13
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14
PURPOSE. See the above copyright notices for more information.
16
=========================================================================*/
18
#pragma warning ( disable : 4786 )
21
#include "itkImageFileReader.h"
22
#include "itkOrientedImage.h"
23
#include "itkCentralDifferenceImageFunction.h"
25
int itkOrientedImage2DTest( int ac, char * av[] )
30
std::cerr << "Usage: " << av[0]
32
<< "corner1x corner1y "
33
<< "corner2x corner2y "
34
<< "corner3x corner3y "
35
<< "derivative1x derivative1y "
36
<< "derivative2x derivative2y "
41
const unsigned int Dimension = 2;
42
typedef unsigned char PixelType;
44
typedef itk::OrientedImage<PixelType, Dimension> ImageType;
45
typedef itk::ImageFileReader< ImageType > ReaderType;
47
typedef ImageType::IndexType IndexType;
48
typedef ImageType::PointType PointType;
49
typedef IndexType::IndexValueType IndexValueType;
51
ReaderType::Pointer reader = ReaderType::New();
53
reader->SetFileName( av[1] );
59
catch (itk::ExceptionObject & e)
61
std::cerr << e << std::endl;
65
ImageType::ConstPointer image = reader->GetOutput();
67
ImageType::DirectionType directionCosines = image->GetDirection();
69
std::cout << directionCosines << std::endl;
71
unsigned int element = 2;
72
const double tolerance = 1e-3;
74
ImageType::RegionType region = image->GetLargestPossibleRegion();
75
ImageType::SizeType size = region.GetSize();
77
const int numberOfPointsToTest = 3;
79
IndexType index[numberOfPointsToTest];
80
PointType physicalPoint;
85
index[1][0] = size[0];
89
index[2][1] = size[1];
91
image->Print( std::cout );
92
std::cout << std::endl;
93
std::cout << std::endl;
95
for( int pointId=0; pointId < numberOfPointsToTest; ++pointId )
98
image->TransformIndexToPhysicalPoint( index[pointId], physicalPoint );
100
std::cout << index[pointId] << " : " << physicalPoint << std::endl;
102
for( unsigned int dim=0; dim < Dimension; ++dim )
104
const double expectedValue = atof( av[ element++ ] );
105
const double currentValue = physicalPoint[dim];
106
const double difference = currentValue - expectedValue;
107
if( vnl_math_abs( difference ) > tolerance )
109
std::cerr << "Error: " << std::endl;
110
std::cerr << "in Point # " << pointId << std::endl;
111
std::cerr << "Expected = " << expectedValue << std::endl;
112
std::cerr << "Read = " << currentValue << std::endl;
113
std::cerr << "Index = " << index[pointId] << std::endl;
114
std::cerr << "PhysicalPoint = " << physicalPoint << std::endl;
121
// Select a point in the middle of the image and compute its
122
// derivative using the image orientation.
124
IndexType centralIndex;
125
centralIndex[0] = static_cast< IndexValueType >( size[0] / 2.0 );
126
centralIndex[1] = static_cast< IndexValueType >( size[1] / 2.0 );
128
typedef itk::CentralDifferenceImageFunction< ImageType, double > CentralDifferenceImageFunctionType;
130
CentralDifferenceImageFunctionType::Pointer gradientHelper1 = CentralDifferenceImageFunctionType::New();
131
gradientHelper1->SetInputImage( image );
133
std::cout << std::endl;
134
std::cout << std::endl;
135
std::cout << "Image Direction" << std::endl;
136
std::cout << image->GetDirection() << std::endl;
138
{ // Compute gradient value without taking image direction into account
139
gradientHelper1->UseImageDirectionOff();
140
CentralDifferenceImageFunctionType::OutputType gradient1a = gradientHelper1->EvaluateAtIndex( centralIndex );
142
std::cout << "Gradient without Direction" << std::endl;
143
std::cout << gradient1a << std::endl;
145
for( unsigned int dim=0; dim < Dimension; ++dim )
147
const double expectedValue = atof( av[ element++ ] );
148
const double currentValue = gradient1a[dim];
149
const double difference = currentValue - expectedValue;
150
if( vnl_math_abs( difference ) > tolerance )
152
std::cerr << "Error: " << std::endl;
154
std::cerr << "Expected = " << expectedValue << std::endl;
155
std::cerr << "Read = " << currentValue << std::endl;
162
{ // Compute gradient value taking image direction into account
163
gradientHelper1->UseImageDirectionOn();
164
CentralDifferenceImageFunctionType::OutputType gradient1b = gradientHelper1->EvaluateAtIndex( centralIndex );
166
std::cout << std::endl;
167
std::cout << "Gradient with Direction" << std::endl;
168
std::cout << gradient1b << std::endl;
170
for( unsigned int dim=0; dim < Dimension; ++dim )
172
const double expectedValue = atof( av[ element++ ] );
173
const double currentValue = gradient1b[dim];
174
const double difference = currentValue - expectedValue;
175
if( vnl_math_abs( difference ) > tolerance )
177
std::cerr << "Error: " << std::endl;
178
std::cerr << "Expected = " << expectedValue << std::endl;
179
std::cerr << "Read = " << currentValue << std::endl;