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
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkEigenAnalysis2DImageFilter.txx,v $
Language: C++
Date: $Date: 2008-01-24 00:15:02 $
Version: $Revision: 1.21 $
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.
=========================================================================*/
#ifndef _itkEigenAnalysis2DImageFilter_txx
#define _itkEigenAnalysis2DImageFilter_txx
#include "itkEigenAnalysis2DImageFilter.h"
#include "itkImageRegionIteratorWithIndex.h"
#include "itkImageRegionConstIteratorWithIndex.h"
#include "itkProgressReporter.h"
namespace itk
{
/**
* Constructor
*/
template <class TInputImage, class TEigenValueImage, class TEigenVectorImage>
EigenAnalysis2DImageFilter<TInputImage,TEigenValueImage,TEigenVectorImage>
::EigenAnalysis2DImageFilter()
{
this->SetNumberOfRequiredInputs( 3 );
this->SetNumberOfRequiredOutputs( 3 );
this->SetNthOutput( 0, this->MakeOutput( 0 ) );
this->SetNthOutput( 1, this->MakeOutput( 1 ) );
this->SetNthOutput( 2, this->MakeOutput( 2 ) );
}
/**
* Connect one the image containing the [0,0] elements of the input matrix
*/
template <class TInputImage, class TEigenValueImage, class TEigenVectorImage>
void
EigenAnalysis2DImageFilter<TInputImage,TEigenValueImage,TEigenVectorImage>
::SetInput1( TInputImage * image )
{
this->SetNthInput(0, image);
}
/**
* Connect one the image containing the [0,1] elements of the input matrix
* this element is the same [1,0] because this filter assumes symmetric
* matrices
*/
template <class TInputImage, class TEigenValueImage, class TEigenVectorImage>
void
EigenAnalysis2DImageFilter<TInputImage,TEigenValueImage,TEigenVectorImage>
::SetInput2( TInputImage * image )
{
this->SetNthInput(1, image);
}
/**
* Connect one the image containing the [1,1] elements of the input matrix
*/
template <class TInputImage, class TEigenValueImage, class TEigenVectorImage>
void
EigenAnalysis2DImageFilter<TInputImage,TEigenValueImage,TEigenVectorImage>
::SetInput3( TInputImage * image )
{
this->SetNthInput(2, image);
}
/**
* Get the greatest eigenvalue considering the sign
*/
template <class TInputImage, class TEigenValueImage, class TEigenVectorImage>
typename EigenAnalysis2DImageFilter<TInputImage,TEigenValueImage,TEigenVectorImage>::EigenValueImageType *
EigenAnalysis2DImageFilter<TInputImage,TEigenValueImage,TEigenVectorImage>
::GetMaxEigenValue( void )
{
return dynamic_cast<EigenValueImageType *>(
this->ProcessObject::GetOutput( 0 ) );
}
/**
* Get the smallest eigenvalue considering the sign
*/
template <class TInputImage, class TEigenValueImage, class TEigenVectorImage>
typename EigenAnalysis2DImageFilter<TInputImage,TEigenValueImage,TEigenVectorImage>::EigenValueImageType *
EigenAnalysis2DImageFilter<TInputImage,TEigenValueImage,TEigenVectorImage>
::GetMinEigenValue( void )
{
return dynamic_cast<EigenValueImageType *>(
this->ProcessObject::GetOutput( 1 ) );
}
/**
* Get the smallest eigenvalue considering the sign
*/
template <class TInputImage, class TEigenValueImage, class TEigenVectorImage>
typename EigenAnalysis2DImageFilter<TInputImage,TEigenValueImage,TEigenVectorImage>::EigenVectorImageType *
EigenAnalysis2DImageFilter<TInputImage,TEigenValueImage,TEigenVectorImage>
::GetMaxEigenVector( void )
{
EigenVectorImageType *eigenVector = dynamic_cast<EigenVectorImageType *>(
this->ProcessObject::GetOutput( 2 ) );
if (eigenVector)
{
return eigenVector;
}
else
{
itkWarningMacro(<<"EigenAnalysis2DImageFilter::GetMaxEigenVector(): dynamic_cast has failed. A reinterpret_cast is being attempted." << std::endl << "Type name is: " << typeid( *this->GetOutput( 2 )).name());
return reinterpret_cast<EigenVectorImageType *>(
this->ProcessObject::GetOutput( 2 ) );
}
}
/**
* Make Ouput
* \todo Verify that MakeOutput is createing the right type of objects
* this could be the cause of the reinterpret_cast bug in this class
*/
template <class TInputImage, class TEigenValueImage, class TEigenVectorImage>
DataObject::Pointer
EigenAnalysis2DImageFilter<TInputImage,TEigenValueImage,TEigenVectorImage>
::MakeOutput(unsigned int idx)
{
DataObject::Pointer output;
switch( idx )
{
case 0:
output = (EigenValueImageType::New()).GetPointer();
break;
case 1:
output = (EigenValueImageType::New()).GetPointer();
break;
case 2:
output = (EigenVectorImageType::New()).GetPointer();
break;
}
return output.GetPointer();
}
template <class TInputImage, class TEigenValueImage, class TEigenVectorImage>
void
EigenAnalysis2DImageFilter<TInputImage,TEigenValueImage,TEigenVectorImage>
::GenerateData()
{
typename TInputImage::ConstPointer inputPtr1(
dynamic_cast<const TInputImage *>(
(ProcessObject::GetInput(0))));
typename TInputImage::ConstPointer inputPtr2(
dynamic_cast<const TInputImage *>(
(ProcessObject::GetInput(1))));
typename TInputImage::ConstPointer inputPtr3(
dynamic_cast<const TInputImage *>(
(ProcessObject::GetInput(2))));
EigenValueImagePointer outputPtr1 = this->GetMaxEigenValue();
EigenValueImagePointer outputPtr2 = this->GetMinEigenValue();
EigenVectorImagePointer outputPtr3 = this->GetMaxEigenVector();
outputPtr1->SetBufferedRegion( inputPtr1->GetBufferedRegion() );
outputPtr2->SetBufferedRegion( inputPtr1->GetBufferedRegion() );
outputPtr3->SetBufferedRegion( inputPtr1->GetBufferedRegion() );
outputPtr1->Allocate();
outputPtr2->Allocate();
outputPtr3->Allocate();
EigenValueImageRegionType region = outputPtr1->GetRequestedRegion();
ImageRegionConstIteratorWithIndex<TInputImage> inputIt1( inputPtr1, region );
ImageRegionConstIteratorWithIndex<TInputImage> inputIt2( inputPtr2, region );
ImageRegionConstIteratorWithIndex<TInputImage> inputIt3( inputPtr3, region );
ImageRegionIteratorWithIndex<EigenValueImageType> outputIt1(outputPtr1, region );
ImageRegionIteratorWithIndex<EigenValueImageType> outputIt2(outputPtr2, region );
ImageRegionIteratorWithIndex<EigenVectorImageType> outputIt3(outputPtr3, region );
EigenVectorType nullVector;
nullVector.Fill( 0.0 );
// support progress methods/callbacks
ProgressReporter progress(this, 0, region.GetNumberOfPixels());
inputIt1.GoToBegin();
inputIt2.GoToBegin();
inputIt3.GoToBegin();
outputIt1.GoToBegin();
outputIt2.GoToBegin();
outputIt3.GoToBegin();
EigenVectorType eigenVector;
while( !inputIt1.IsAtEnd() )
{
const double xx = static_cast<double>( inputIt1.Get() );
const double xy = static_cast<double>( inputIt2.Get() );
const double yy = static_cast<double>( inputIt3.Get() );
const double dxy = xx - yy;
const double sxy = xx + yy;
const double S = vcl_sqrt(dxy * dxy + 4.0 * xy * xy);
const double pp = ( sxy + S ) / 2.0;
const double qq = ( sxy - S ) / 2.0;
outputIt1.Set( pp );
outputIt2.Set( qq );
eigenVector[0] = static_cast<VectorComponentType>(( -dxy - S ) / 2.0 );
eigenVector[1] = static_cast<VectorComponentType>( -xy );
const VectorComponentType norm = eigenVector.GetNorm();
if( norm > 1e-30 )
{
outputIt3.Set( eigenVector / norm );
}
else
{
outputIt3.Set( nullVector );
}
++inputIt1;
++inputIt2;
++inputIt3;
++outputIt1;
++outputIt2;
++outputIt3;
progress.CompletedPixel();
}
}
} // end namespace itk
#endif
|