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
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: itkOrientedImage3DTest.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 "itkImageFileReader.h"
#include "itkOrientedImage.h"
#include "itkCentralDifferenceImageFunction.h"
int itkOrientedImage3DTest( int ac, char * av[] )
{
if( ac < 20 )
{
std::cerr << "Usage: " << av[0]
<< " InputImage "
<< "corner1x corner1y corner1z "
<< "corner2x corner2y corner2z "
<< "corner3x corner3y corner3z "
<< "corner4x corner4y corner4z "
<< "derivative1x derivative1y derivative1z "
<< "derivative2x derivative2y derivative2z"
<< std::endl;
return EXIT_FAILURE;
}
const unsigned int Dimension = 3;
typedef unsigned char PixelType;
typedef itk::OrientedImage<PixelType, Dimension> ImageType;
typedef itk::ImageFileReader< ImageType > ReaderType;
typedef ImageType::IndexType IndexType;
typedef ImageType::PointType PointType;
typedef IndexType::IndexValueType IndexValueType;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName( av[1] );
try
{
reader->Update();
}
catch (itk::ExceptionObject & e)
{
std::cerr << e << std::endl;
return EXIT_FAILURE;
}
ImageType::ConstPointer image = reader->GetOutput();
ImageType::DirectionType directionCosines = image->GetDirection();
std::cout << directionCosines << std::endl;
unsigned int element = 2;
const double tolerance = 1e-3;
ImageType::RegionType region = image->GetLargestPossibleRegion();
ImageType::SizeType size = region.GetSize();
const int numberOfPointsToTest = 4;
ImageType::IndexType index[numberOfPointsToTest];
ImageType::PointType physicalPoint;
index[0][0] = 0;
index[0][1] = 0;
index[0][2] = 0;
index[1][0] = size[0];
index[1][1] = 0;
index[1][2] = 0;
index[2][0] = 0;
index[2][1] = size[1];
index[2][2] = 0;
index[3][0] = 0;
index[3][1] = 0;
index[3][2] = size[2];
image->Print( std::cout );
std::cout << std::endl;
std::cout << std::endl;
for( int pointId=0; pointId < numberOfPointsToTest; ++pointId )
{
image->TransformIndexToPhysicalPoint( index[pointId], physicalPoint );
std::cout << index[pointId] << " : " << physicalPoint << std::endl;
for( unsigned int dim=0; dim < Dimension; ++dim )
{
const double expectedValue = atof( av[ element++ ] );
const double currentValue = physicalPoint[dim];
const double difference = currentValue - expectedValue;
if( vnl_math_abs( difference ) > tolerance )
{
std::cerr << "Error: " << std::endl;
std::cerr << "in Point # " << pointId << std::endl;
std::cerr << "Expected = " << expectedValue << std::endl;
std::cerr << "Read = " << currentValue << std::endl;
std::cerr << "Index = " << index[pointId] << std::endl;
std::cerr << "PhysicalPoint = " << physicalPoint << std::endl;
}
}
}
//
// Select a point in the middle of the image and compute its
// derivative using the image orientation.
//
IndexType centralIndex;
centralIndex[0] = static_cast< IndexValueType >( size[0] / 2.0 );
centralIndex[1] = static_cast< IndexValueType >( size[1] / 2.0 );
centralIndex[2] = static_cast< IndexValueType >( size[2] / 2.0 );
typedef itk::CentralDifferenceImageFunction< ImageType, double > CentralDifferenceImageFunctionType;
CentralDifferenceImageFunctionType::Pointer gradientHelper1 = CentralDifferenceImageFunctionType::New();
gradientHelper1->SetInputImage( image );
std::cout << std::endl;
std::cout << std::endl;
std::cout << "Image Direction" << std::endl;
std::cout << image->GetDirection() << std::endl;
{ // Compute gradient value without taking image direction into account
gradientHelper1->UseImageDirectionOff();
CentralDifferenceImageFunctionType::OutputType gradient1a = gradientHelper1->EvaluateAtIndex( centralIndex );
std::cout << "Gradient without Direction" << std::endl;
std::cout << gradient1a << std::endl;
for( unsigned int dim=0; dim < Dimension; ++dim )
{
const double expectedValue = atof( av[ element++ ] );
const double currentValue = gradient1a[dim];
const double difference = currentValue - expectedValue;
if( vnl_math_abs( difference ) > tolerance )
{
std::cerr << "Error: " << std::endl;
std::cerr << "Expected = " << expectedValue << std::endl;
std::cerr << "Read = " << currentValue << std::endl;
return EXIT_FAILURE;
}
}
}
{ // Compute gradient value taking image direction into account
gradientHelper1->UseImageDirectionOn();
CentralDifferenceImageFunctionType::OutputType gradient1b = gradientHelper1->EvaluateAtIndex( centralIndex );
std::cout << std::endl;
std::cout << "Gradient with Direction" << std::endl;
std::cout << gradient1b << std::endl;
for( unsigned int dim=0; dim < Dimension; ++dim )
{
const double expectedValue = atof( av[ element++ ] );
const double currentValue = gradient1b[dim];
const double difference = currentValue - expectedValue;
if( vnl_math_abs( difference ) > tolerance )
{
std::cerr << "Error: " << std::endl;
std::cerr << "Expected = " << expectedValue << std::endl;
std::cerr << "Read = " << currentValue << std::endl;
return EXIT_FAILURE;
}
}
}
return EXIT_SUCCESS;
}
|