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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkVectorGradientMagnitudeImageFilter.txx,v $
Language: C++
Date: $Date: 2006-03-19 04:36:58 $
Version: $Revision: 1.12 $
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 _itkVectorGradientMagnitudeImageFilter_txx
#define _itkVectorGradientMagnitudeImageFilter_txx
#include "itkVectorGradientMagnitudeImageFilter.h"
#include "itkNeighborhoodAlgorithm.h"
#include "itkImageRegionIterator.h"
#include "itkZeroFluxNeumannBoundaryCondition.h"
#include "itkProgressReporter.h"
#include "itkVectorCastImageFilter.h"
#include "vnl/vnl_math.h"
namespace itk
{
template <typename TInputImage, typename TRealType, typename TOutputImage>
void
VectorGradientMagnitudeImageFilter<TInputImage, TRealType, TOutputImage>
::PrintSelf(std::ostream& os, Indent indent) const
{
unsigned i;
Superclass::PrintSelf(os,indent);
os << indent << "m_UseImageSpacing = " << m_UseImageSpacing
<< std::endl;
os << indent << "m_UsePrincipleComponents = " << m_UseImageSpacing
<< std::endl;
os << indent << "m_RequestedNumberOfThreads = " << m_RequestedNumberOfThreads
<< std::endl;
os << indent << "m_DerivativeWeights = ";
for (i = 0; i < ImageDimension; i++)
{ os << m_DerivativeWeights[i] << " "; }
os << std::endl;
os << indent << "m_ComponentWeights = ";
for (i = 0; i < VectorDimension; i++)
{ os << m_ComponentWeights[i] << " "; }
os << std::endl;
os << indent << "m_NeighborhoodRadius = " << m_NeighborhoodRadius
<< std::endl;
os << indent << "m_RealValuedInputImage = " << m_RealValuedInputImage.GetPointer()
<< std::endl;
}
template <typename TInputImage, typename TRealType, typename TOutputImage>
VectorGradientMagnitudeImageFilter<TInputImage, TRealType, TOutputImage>
::VectorGradientMagnitudeImageFilter()
{
unsigned int i;
m_UseImageSpacing = false;
m_UsePrincipleComponents = true;
m_RequestedNumberOfThreads = this->GetNumberOfThreads();
for (i = 0; i < ImageDimension; i++)
{
m_NeighborhoodRadius[i] = 1; // radius of neighborhood we will use
m_DerivativeWeights[i] = static_cast<TRealType>(1.0);
}
for (i = 0; i < VectorDimension; i++)
{
m_ComponentWeights[i] = static_cast<TRealType>(1.0);
m_SqrtComponentWeights[i] = static_cast<TRealType>(1.0);
}
}
template <typename TInputImage, typename TRealType, typename TOutputImage>
void
VectorGradientMagnitudeImageFilter<TInputImage, TRealType, TOutputImage>
::SetDerivativeWeights(TRealType data[])
{
m_UseImageSpacing = false;
for (unsigned i = 0; i < ImageDimension; ++i)
{
if (m_DerivativeWeights[i] != data[i])
{
this->Modified();
m_DerivativeWeights[i] = data[i];
}
}
}
template <typename TInputImage, typename TRealType, typename TOutputImage>
void
VectorGradientMagnitudeImageFilter<TInputImage, TRealType, TOutputImage>
::SetUseImageSpacing(bool f)
{
if (m_UseImageSpacing == f) { return; }
// Only reset the weights if they were previously set to the image spacing,
// otherwise, the user may have provided their own weightings.
if (f == false && m_UseImageSpacing == true)
{
for (unsigned i = 0; i < ImageDimension; ++i)
{
m_DerivativeWeights[i] = static_cast<TRealType>(1.0);
}
}
m_UseImageSpacing = f;
}
template <typename TInputImage, typename TRealType, typename TOutputImage>
void
VectorGradientMagnitudeImageFilter<TInputImage, TRealType, TOutputImage>
::GenerateInputRequestedRegion() throw(InvalidRequestedRegionError)
{
// call the superclass' implementation of this method
Superclass::GenerateInputRequestedRegion();
// get pointers to the input and output
InputImagePointer inputPtr =
const_cast< InputImageType * >( this->GetInput());
OutputImagePointer outputPtr = this->GetOutput();
if ( !inputPtr || !outputPtr )
{
return;
}
// get a copy of the input requested region (should equal the output
// requested region)
typename TInputImage::RegionType inputRequestedRegion;
inputRequestedRegion = inputPtr->GetRequestedRegion();
// pad the input requested region by the operator radius
inputRequestedRegion.PadByRadius( m_NeighborhoodRadius );
// crop the input requested region at the input's largest possible region
if ( inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()) )
{
inputPtr->SetRequestedRegion( inputRequestedRegion );
return;
}
else
{
// Couldn't crop the region (requested region is outside the largest
// possible region). Throw an exception.
// store what we tried to request (prior to trying to crop)
inputPtr->SetRequestedRegion( inputRequestedRegion );
// build an exception
InvalidRequestedRegionError e(__FILE__, __LINE__);
e.SetLocation(ITK_LOCATION);
e.SetDescription("Requested region is (at least partially) outside the largest possible region.");
e.SetDataObject(inputPtr);
throw e;
}
}
template< typename TInputImage, typename TRealType, typename TOutputImage >
void
VectorGradientMagnitudeImageFilter<TInputImage, TRealType, TOutputImage>
::BeforeThreadedGenerateData()
{
Superclass::BeforeThreadedGenerateData();
// Calculate the square-roots of the component weights.
for (unsigned i = 0; i < VectorDimension; ++i)
{
if (m_ComponentWeights[i] < 0 )
{
itkExceptionMacro( << "Component weights must be positive numbers" );
}
m_SqrtComponentWeights[i] = ::sqrt(m_ComponentWeights[i]);
}
// Set the weights on the derivatives.
// Are we using image spacing in the calculations? If so we must update now
// in case our input image has changed.
if (m_UseImageSpacing == true)
{
for (unsigned i = 0; i < ImageDimension; i++)
{
if (static_cast<TRealType>(this->GetInput()->GetSpacing()[i]) == 0.0)
{
itkExceptionMacro(<< "Image spacing in dimension " << i << " is zero.");
}
m_DerivativeWeights[i]
= static_cast<TRealType>( 1.0 /
static_cast<TRealType>(this->GetInput()->GetSpacing()[i]) );
}
}
// If using the principle components method, then force this filter to use a
// single thread because vnl eigensystem objects are not thread-safe. 3D
// data is ok because we have a special solver.
if (m_UsePrincipleComponents == true && ImageDimension != 3)
{
m_RequestedNumberOfThreads = this->GetNumberOfThreads();
this->SetNumberOfThreads(1);
}
else
{
this->SetNumberOfThreads(m_RequestedNumberOfThreads);
}
/** If the input needs casting to a real-valued vector type, create the
appropriate image and set the m_RealValuedInputImage pointer to this
image. Otherwise just point to the input image. */
if ( typeid( typename InputImageType::PixelType ) != typeid( RealVectorType ) )
{
typename VectorCastImageFilter<TInputImage, RealVectorImageType>::Pointer
caster = VectorCastImageFilter<TInputImage, RealVectorImageType>::New();
caster->SetInput(this->GetInput());
caster->Update();
m_RealValuedInputImage = caster->GetOutput();
}
else
{
m_RealValuedInputImage
= dynamic_cast<const ImageBase<ImageDimension> *>(this->GetInput());
}
}
template< typename TInputImage, typename TRealType, typename TOutputImage >
void
VectorGradientMagnitudeImageFilter< TInputImage, TRealType, TOutputImage >
::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread,
int threadId)
{
ZeroFluxNeumannBoundaryCondition<RealVectorImageType> nbc;
ConstNeighborhoodIteratorType bit;
ImageRegionIterator<TOutputImage> it;
// Find the data-set boundary "faces"
typename NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<RealVectorImageType>::
FaceListType faceList;
NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<RealVectorImageType> bC;
faceList = bC(dynamic_cast<const RealVectorImageType *>(m_RealValuedInputImage.GetPointer()),
outputRegionForThread, m_NeighborhoodRadius);
typename NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<RealVectorImageType>::
FaceListType::iterator fit;
fit = faceList.begin();
// Support progress methods/callbacks
ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
// Process each of the data set faces. The iterator is reinitialized on each
// face so that it can determine whether or not to check for boundary
// conditions.
for (fit=faceList.begin(); fit != faceList.end(); ++fit)
{
bit = ConstNeighborhoodIteratorType(m_NeighborhoodRadius,
dynamic_cast<const RealVectorImageType *>(m_RealValuedInputImage.GetPointer()),
*fit);
it = ImageRegionIterator<TOutputImage>(this->GetOutput(), *fit);
bit.OverrideBoundaryCondition(&nbc);
bit.GoToBegin();
if (m_UsePrincipleComponents == true)
{
if (ImageDimension == 3)
{ // Use the specialized eigensolve which can be threaded
while ( ! bit.IsAtEnd() )
{
it.Set( this->EvaluateAtNeighborhood3D(bit) );
++bit;
++it;
progress.CompletedPixel();
}
}
else
{
while ( ! bit.IsAtEnd() )
{
it.Set( this->EvaluateAtNeighborhood(bit) );
++bit;
++it;
progress.CompletedPixel();
}
}
}
else
{
while ( ! bit.IsAtEnd() )
{
it.Set( this->NonPCEvaluateAtNeighborhood(bit) );
++bit;
++it;
progress.CompletedPixel();
}
}
}
}
template <typename TInputImage, typename TRealType, typename TOutputImage>
int
VectorGradientMagnitudeImageFilter<TInputImage, TRealType, TOutputImage>
::CubicSolver(double *c, double *s)
{
// IMPORTANT
// This code is specialized for particular case of positive symmetric
// matrix. It also assumes that x^3 coefficient is 1. c contains the
// coefficients of the polynomial: x^3 + c[2]x^2 + c[1]x^1 + c[0]. The roots
// s are not necessarily sorted, and int is the number of distinct roots
// found in s.
int num;
const double dpi = 3.14159265358979323846;
const double epsilon = 1.0e-11;
// Substitution of x = y - c[2]/3 eliminate the quadric term x^3 +px + q = 0
double sq_c2 = c[2] * c[2];
double p = 1.0/3 * (- 1.0/3.0 * sq_c2 + c[1]);
double q = 1.0/2 * (2.0/27.0 * c[2] * sq_c2 - 1.0/3.0 * c[2] * c[1] + c[0]);
// Cardano's formula
double cb_p = p * p * p;
double D = q * q + cb_p;
if (D < -epsilon) // D < 0, three real solutions, by far the common case.
{
double phi = 1.0/3.0 * vcl_acos(-q / vcl_sqrt(-cb_p));
double t = 2.0 * vcl_sqrt(-p);
s[0] = t * vcl_cos(phi);
s[1] = - t * vcl_cos(phi + dpi / 3);
s[2] = - t * vcl_cos(phi - dpi / 3);
num = 3;
}
else if (D < epsilon) // D == 0
{
if (q > -epsilon && q < epsilon)
{
s[0] = 0.0;
num = 1;
}
else
{
double u = vnl_math_cuberoot(-q);
s[0] = 2 * u;
s[1] = - u;
num = 2;
}
}
else // Only one real solution. This case misses a double root on rare
// occasions with very large char eqn coefficients.
{
double sqrt_D = vcl_sqrt(D);
double u = vnl_math_cuberoot(sqrt_D - q);
double v = - vnl_math_cuberoot(sqrt_D + q);
s[0] = u + v;
num = 1;
}
// Resubstitute
double sub = 1.0/3.0 * c[2];
for (int i = 0; i < num; ++i)
{ s[i] -= sub; }
return num;
}
} // end namespace itk
#endif
|