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
|
/*=========================================================================
Program: ITK-SNAP
Module: $RCSfile: SNAPLevelSetDriver.txx,v $
Language: C++
Date: $Date: 2010/06/28 18:45:08 $
Version: $Revision: 1.6 $
Copyright (c) 2007 Paul A. Yushkevich
This file is part of ITK-SNAP
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-----
Copyright (c) 2003 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 __SNAPLevelSetDriver_txx_
#define __SNAPLevelSetDriver_txx_
// Borland compiler is very lazy so we need to instantiate the template
// by hand
//#if defined(__BORLANDC__)
//#include <../../../SNAPBorlandDummyTypes.h>
//#endif
#include "SNAPLevelSetDriver.h"
#include "itkCommand.h"
#include "itkNarrowBandLevelSetImageFilter.h"
#include "itkDenseFiniteDifferenceImageFilter.h"
#include "LevelSetExtensionFilter.h"
#include "itkParallelSparseFieldLevelSetImageFilter.h"
// Disable some windows debug length messages
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#pragma warning ( disable : 4503 )
#endif
/**
* THIS IS A WORKAROUND FOR A BUG THAT I FOUND IN THE PARALLEL SPARSE LEVEL SET
* FILTER. For small seeds, the way the filter splits up the image into regions
* for different threads, some threads end up with empty regions (no nodes). The
* function that computes the timestep from the per-region timesteps then sets
* the timestep to 0, and the filter stops. The work-around changes the step size
* for empty regions to 1 and fixes the problem.
*/
template< class TInputImage, class TOutputImage >
class ParallelSparseFieldLevelSetImageFilterBugFix
: public itk::ParallelSparseFieldLevelSetImageFilter< TInputImage, TOutputImage >
{
public:
typedef ParallelSparseFieldLevelSetImageFilterBugFix Self;
typedef itk::ParallelSparseFieldLevelSetImageFilter< TInputImage, TOutputImage > Superclass;
typedef itk::SmartPointer< Self > Pointer;
typedef itk::SmartPointer< const Self > ConstPointer;
/** Method for creation through the object factory. */
itkNewMacro(Self)
/** Run-time type information (and related methods). */
itkTypeMacro(ParallelSparseFieldLevelSetImageFilterBugFix,
itk::ParallelSparseFieldLevelSetImageFilter)
virtual typename Superclass::TimeStepType ThreadedCalculateChange(itk::ThreadIdType ThreadId)
{
typename Superclass::TimeStepType ts = Superclass::ThreadedCalculateChange(ThreadId);
if(ThreadId > 0 && this->m_Data[ThreadId].m_Count == 0)
return 1.0;
else
return ts;
}
itk::SimpleFastMutexLock locky;
};
// Create an inverting functor
class InvertFunctor {
public:
unsigned char operator()(unsigned char input) {
return input == 0 ? 1 : 0;
}
};
template<unsigned int VDimension>
SNAPLevelSetDriver<VDimension>
::SNAPLevelSetDriver(FloatImageType *init, ShortImageType *speed,
const SnakeParameters &sparms,
VectorImageType *externalAdvection)
{
// Create the level set function
m_LevelSetFunction = LevelSetFunctionType::New();
// Pass the speed image to the function
m_LevelSetFunction->SetSpeedImage(speed);
// Scale the speed function to range -1 to 1
m_LevelSetFunction->SetSpeedScaleFactor(1.0 / 0x7fff);
// Set the external advection if any
if(externalAdvection)
m_LevelSetFunction->SetAdvectionField(externalAdvection);
// Remember the input and output images for later initialization
m_InitializationImage = init;
// Pass the parameters to the level set function
AssignParametersToPhi(sparms,true);
// Create the filter
DoCreateLevelSetFilter();
}
template<unsigned int VDimension>
void
SNAPLevelSetDriver<VDimension>
::AssignParametersToPhi(const SnakeParameters &p, bool itkNotUsed(firstTime))
{
// Set up the level set function
// The sign of the advection term is flipped in our equation
m_LevelSetFunction->SetAdvectionWeight(- p.GetAdvectionWeight());
m_LevelSetFunction->SetAdvectionSpeedExponent(p.GetAdvectionSpeedExponent());
// The curvature exponent for traditional/legacy reasons has a +1 value.
m_LevelSetFunction->SetCurvatureSpeedExponent(p.GetCurvatureSpeedExponent()+1);
m_LevelSetFunction->SetCurvatureWeight(p.GetCurvatureWeight());
m_LevelSetFunction->SetPropagationWeight(p.GetPropagationWeight());
m_LevelSetFunction->SetPropagationSpeedExponent(p.GetPropagationSpeedExponent());
m_LevelSetFunction->SetLaplacianSmoothingWeight(p.GetLaplacianWeight());
m_LevelSetFunction->SetLaplacianSmoothingSpeedExponent(p.GetLaplacianSpeedExponent());
// We only need to recompute the internal images if the exponents to those
// images have changed
m_LevelSetFunction->CalculateInternalImages();
// Call the initialize method
typename LevelSetFunctionType::RadiusType radius;
radius.Fill(1);
m_LevelSetFunction->Initialize(radius);
// Set the time step
m_LevelSetFunction->SetTimeStepFactor(
p.GetAutomaticTimeStep() ? 1.0 : p.GetTimeStepFactor());
// Remember the parameters
m_Parameters = p;
}
template<unsigned int VDimension>
void
SNAPLevelSetDriver<VDimension>
::DoCreateLevelSetFilter()
{
// In this method we have the flexibility to create a level set filter
// of any ITK solver type. This way, we can plug in different solvers:
// NarrowBand, ParallelSparseField, even Dense.
if(m_Parameters.GetSolver() == SnakeParameters::PARALLEL_SPARSE_FIELD_SOLVER)
{
// Define an extension to the appropriate filter class
typedef ParallelSparseFieldLevelSetImageFilterBugFix<
FloatImageType, FloatImageType> LevelSetFilterType;
typedef typename LevelSetFilterType::Pointer LevelSetFilterPointer;
LevelSetFilterPointer filter = LevelSetFilterType::New();
// Cast this specific filter down to the lowest common denominator that is
// a filter
m_LevelSetFilter = filter.GetPointer();
// Perform the special configuration tasks on the filter
filter->SetInput(m_InitializationImage);
filter->SetNumberOfLayers(3);
filter->SetIsoSurfaceValue(0.0f);
filter->SetDifferenceFunction(m_LevelSetFunction);
filter->InPlaceOn();
}
/*
else if(m_Parameters.GetSolver() == SnakeParameters::NARROW_BAND_SOLVER)
{
// Define an extension to the appropriate filter class
typedef itk::NarrowBandLevelSetImageFilter<
FloatImageType,SpeedAdaptorType,float,FloatImageType> LevelSetFilterType;
typedef LevelSetExtensionFilter<LevelSetFilterType> ExtensionFilter;
typename ExtensionFilter::Pointer filter = ExtensionFilter::New();
// Cast this specific filter down to the lowest common denominator that is
// a filter
m_LevelSetFilter = filter.GetPointer();
// Perform the special configuration tasks on the filter
filter->SetSegmentationFunction(m_LevelSetFunction);
filter->SetInput(m_InitializationImage);
filter->SetNarrowBandTotalRadius(5);
filter->SetNarrowBandInnerRadius(3);
filter->SetFeatureImage(m_LevelSetFunction->GetSpeedImage());
}
*/
else if(m_Parameters.GetSolver() == SnakeParameters::DENSE_SOLVER)
{
// Define an extension to the appropriate filter class
typedef itk::DenseFiniteDifferenceImageFilter<
FloatImageType,FloatImageType> LevelSetFilterType;
typedef LevelSetExtensionFilter<LevelSetFilterType> ExtensionFilter;
typename ExtensionFilter::Pointer filter = ExtensionFilter::New();
// Cast this specific filter down to the lowest common denominator that is
// a filter
m_LevelSetFilter = filter.GetPointer();
// Perform the special configuration tasks on the filter
filter->SetInput(m_InitializationImage);
filter->SetDifferenceFunction(m_LevelSetFunction);
filter->InPlaceOn();
}
else
{
throw itk::ExceptionObject(__FILE__,__LINE__,"Unknown level set solver requested");
}
// This code is common to all filters. It causes the filter to initialize
// the necessary memory and sets the iteration counter to 0
m_LevelSetFilter->SetManualReinitialization(true);
m_LevelSetFilter->SetNumberOfIterations(0);
// Update the largest possible region. The slicer may be changing the
// requested region on this image, so it's important that we always
// update the entire image
m_LevelSetFilter->UpdateLargestPossibleRegion();
}
template<unsigned int VDimension>
void
SNAPLevelSetDriver<VDimension>
::Restart()
{
// Tell the filter to reinitialize next time that an update will
// be performed, and set the number of iterations to 0
m_LevelSetFilter->SetStateToUninitialized();
m_LevelSetFilter->SetNumberOfIterations(0);
// Update the largest possible region. The slicer may be changing the
// requested region on this image, so it's important that we always
// update the entire image
m_LevelSetFilter->UpdateLargestPossibleRegion();
}
template<unsigned int VDimension>
void
SNAPLevelSetDriver<VDimension>
::Run(unsigned int nIterations)
{
// Increment the number of iterations
unsigned int nElapsed = m_LevelSetFilter->GetElapsedIterations();
m_LevelSetFilter->SetNumberOfIterations(nElapsed + nIterations);
// Update the largest possible region. The slicer may be changing the
// requested region on this image, so it's important that we always
// update the entire image
m_LevelSetFilter->UpdateLargestPossibleRegion();
}
template<unsigned int VDimension>
bool
SNAPLevelSetDriver<VDimension>
::IsEvolutionConverged()
{
if(m_LevelSetFilter->GetElapsedIterations() == 0)
return false;
// For now, require absolute convergence
std::cout << m_LevelSetFilter->GetRMSChange() << std::endl;
return (m_LevelSetFilter->GetRMSChange() == 0.0);
}
template<unsigned int VDimension>
typename SNAPLevelSetDriver<VDimension>::FloatImageType *
SNAPLevelSetDriver<VDimension>
::GetCurrentState()
{
// Fix the spacing of the level set filter's output (huh?)
m_LevelSetFilter->GetOutput()->SetDirection(m_InitializationImage->GetDirection());
m_LevelSetFilter->GetOutput()->SetSpacing(m_InitializationImage->GetSpacing());
m_LevelSetFilter->GetOutput()->SetOrigin(m_InitializationImage->GetOrigin());
// Return the filter's output
return m_LevelSetFilter->GetOutput();
}
template<unsigned int VDimension>
unsigned int
SNAPLevelSetDriver<VDimension>
::GetElapsedIterations() const
{
return m_LevelSetFilter->GetElapsedIterations();
}
template<unsigned int VDimension>
void
SNAPLevelSetDriver<VDimension>
::CleanUp()
{
// Basically, the filter is finished, and we can finally return
// from running the filter. Let's clear the level set and the
// function to free memory
m_LevelSetFilter = NULL;
m_LevelSetFunction = NULL;
}
template<unsigned int VDimension>
void
SNAPLevelSetDriver<VDimension>
::SetSnakeParameters(const SnakeParameters &sparms)
{
// Parameter setting can be destructive or passive. If the solver has
// has changed, then it's destructive, otherwise it's passive
bool destructive = sparms.GetSolver() != m_Parameters.GetSolver();
// First of all, pass the parameters to the phi function, which may or
// may not cause it to recompute it's images
AssignParametersToPhi(sparms,false);
// Create a new level set filter
if(destructive)
{
DoCreateLevelSetFilter();
}
}
#endif
|