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
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkFastMarchingDirectionalFreezeImageFilter.txx,v $
Language: C++
Date: $Date: 2007/02/09 15:06:19 $
Version: $Revision: 1.7 $
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 _itkFastMarchingDirectionalFreezeImageFilter_txx
#define _itkFastMarchingDirectionalFreezeImageFilter_txx
#include "itkFastMarchingDirectionalFreezeImageFilter.h"
#include "itkGradientImageFilter.h"
namespace itk
{
/*
*
*/
template <class TLevelSet, class TSpeedImage>
FastMarchingDirectionalFreezeImageFilter<TLevelSet,TSpeedImage>
::FastMarchingDirectionalFreezeImageFilter()
{
m_SpeedGradientImage = SpeedGradientImageType::New();
}
/*
*
*/
template <class TLevelSet, class TSpeedImage>
void
FastMarchingDirectionalFreezeImageFilter<TLevelSet,TSpeedImage>
::PrintSelf(std::ostream& os, Indent indent) const
{
Superclass::PrintSelf(os,indent);
os << indent << "Speed gradient image: " << m_SpeedGradientImage.GetPointer() << std::endl;
}
/*
*
*/
template <class TLevelSet, class TSpeedImage>
void
FastMarchingDirectionalFreezeImageFilter<TLevelSet,TSpeedImage>
::AllocateSpeedGradientImage()
{
SpeedImageConstPointer speedImage = this->GetInput();
m_SpeedGradientImage->SetRequestedRegion(speedImage->GetRequestedRegion());
m_SpeedGradientImage->SetBufferedRegion(speedImage->GetBufferedRegion());
m_SpeedGradientImage->SetLargestPossibleRegion(speedImage->GetLargestPossibleRegion());
m_SpeedGradientImage->Allocate();
}
/*
*
*/
template <class TLevelSet, class TSpeedImage>
void
FastMarchingDirectionalFreezeImageFilter<TLevelSet,TSpeedImage>
::ComputeSpeedGradientImage()
{
if (this->GetInput() == NULL)
{
ExceptionObject err(__FILE__, __LINE__);
err.SetLocation( ITK_LOCATION );
err.SetDescription("Input image must be set");
throw err;
}
SpeedImageConstPointer speedImage = this->GetInput();
typedef GradientImageFilter<SpeedImageType> DerivativeFilterType;
typename DerivativeFilterType::Pointer derivative = DerivativeFilterType::New();
derivative->SetInput(speedImage);
derivative->SetUseImageSpacingOn();
derivative->Update();
typedef typename DerivativeFilterType::OutputImageType DerivativeOutputImageType;
ImageRegionIterator<SpeedGradientImageType>
dit(derivative->GetOutput(),speedImage->GetRequestedRegion());
ImageRegionIterator<SpeedGradientImageType>
sgit(m_SpeedGradientImage,speedImage->GetRequestedRegion());
for(dit.GoToBegin(),sgit.GoToBegin(); !dit.IsAtEnd(); ++dit,++sgit)
{
//TODO: cast
sgit.Set(dit.Get());
}
}
/*
*
*/
template <class TLevelSet, class TSpeedImage>
void
FastMarchingDirectionalFreezeImageFilter<TLevelSet,TSpeedImage>
::GenerateData()
{
this->AllocateSpeedGradientImage();
this->ComputeSpeedGradientImage();
// run the GenerateData() method of the superclass
try
{
Superclass::GenerateData();
}
catch (ProcessAborted &exc)
{
// process was aborted, clean up the state of the filter
// (most of the cleanup will have already been done by the
// superclass)
throw exc;
}
}
template <class TLevelSet, class TSpeedImage>
void
FastMarchingDirectionalFreezeImageFilter<TLevelSet,TSpeedImage>
::UpdateNeighbors(
const IndexType& index,
const SpeedImageType * speedImage,
LevelSetImageType * output )
{
GradientPixelType gradientValue = this->ComputeGradientValue(index, output);
SpeedGradientPixelType speedGradientValue = m_SpeedGradientImage->GetPixel(index);
//TODO: set threshold to speedGradientNorm
// normalizing is probably not an excellent idea, but it's the only way to threshold by angle
gradientValue.Normalize();
speedGradientValue.Normalize();
//TODO: cast here
if (gradientValue * speedGradientValue < -0.6)
{
return;
}
Superclass::UpdateNeighbors(index,speedImage,output);
}
/*
*
*/
template <class TLevelSet, class TSpeedImage>
typename FastMarchingDirectionalFreezeImageFilter<TLevelSet,TSpeedImage>::GradientPixelType
FastMarchingDirectionalFreezeImageFilter<TLevelSet,TSpeedImage>
::ComputeGradientValue( const IndexType& index,
const LevelSetImageType * output)
{
IndexType neighIndex = index;
typedef typename TLevelSet::PixelType LevelSetPixelType;
LevelSetPixelType centerPixel;
LevelSetPixelType dx_forward;
LevelSetPixelType dx_backward;
GradientPixelType gradientPixel;
const LevelSetIndexType & lastIndex = this->GetLastIndex();
const LevelSetIndexType & startIndex = this->GetStartIndex();
const LevelSetPixelType ZERO =
NumericTraits< LevelSetPixelType >::Zero;
OutputSpacingType spacing = this->GetOutput()->GetSpacing();
unsigned int xStride[itkGetStaticConstMacro(SetDimension)];
for ( unsigned int j = 0; j < SetDimension; j++ )
{
centerPixel = output->GetPixel(index);
neighIndex = index;
// Set stride of one in each direction
xStride[j] = 1;
// Compute one-sided finite differences with alive neighbors
// (the front can only come from there)
dx_backward = 0.0;
neighIndex[j] = index[j] - xStride[j];
if(! (neighIndex[j] > lastIndex[j] ||
neighIndex[j] < startIndex[j]) )
{
if ( this->GetLabelImage()->GetPixel( neighIndex ) == Superclass::AlivePoint )
{
dx_backward = centerPixel - output->GetPixel( neighIndex );
}
}
dx_forward = 0.0;
neighIndex[j] = index[j] + xStride[j];
if(! (neighIndex[j] > lastIndex[j] ||
neighIndex[j] < startIndex[j]) )
{
if ( this->GetLabelImage()->GetPixel( neighIndex ) == Superclass::AlivePoint )
{
dx_forward = output->GetPixel( neighIndex ) - centerPixel;
}
}
// Compute upwind finite differences
if (vnl_math_max(dx_backward,-dx_forward) < ZERO)
{
gradientPixel[j] = ZERO;
}
else if (dx_backward > -dx_forward)
{
gradientPixel[j] = dx_backward;
}
else
{
gradientPixel[j] = dx_forward;
}
gradientPixel[j] /= spacing[j];
}
return gradientPixel;
}
} // namespace itk
#endif
|