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 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
|
/*=========================================================================
*
* Copyright NumFOCUS
*
* 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
*
* https://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.
*
*=========================================================================*/
#ifndef itkSegmentationLevelSetImageFilter_h
#define itkSegmentationLevelSetImageFilter_h
#include "itkSparseFieldLevelSetImageFilter.h"
#include "itkSegmentationLevelSetFunction.h"
#include "itkMath.h"
namespace itk
{
/**
*
* \class SegmentationLevelSetImageFilter
*
* \brief A base class which defines the API for implementing a special class of
* image segmentation filters using level set methods.
*
* \par OVERVIEW
* This object defines the framework for a class of segmentation filters which
* use level set methods. These filters work by constructing a "feature image"
* onto which the evolving level set locks as it moves. In the feature image,
* values that are close to zero are associated with object boundaries. An
* original (or preprocessed) image is given to the filter as the feature image
* and a seed for the level set is given as the input of the filter. The seed is
* converted into a level set embedding which propagates according to the features
* calculated from the original image.
*
* \par TEMPLATE PARAMETERS
* There are two required and one optional template parameter for these
* filters.
*
* TInputImage is the image type of the initial model you will input to the
* filter using SetInput() or SetInitialImage().
*
* TFeatureImage is the image type of the image from which the filter will
* calculate the speed term for segmentation (see INPUTS).
*
* TOutputPixelType is the data type used for the output image phi, the implicit
* level set image. This should really only ever be set as float (default) or
* double.
*
* \par INPUTS
* The input to any subclass of this filter is the seed image for the initial
* level set embedding. As with other subclasses of the
* SparseLevelSetImageFilter, the type of the input image is is not important.
* The (RequestedRegion) size of the seed image must, however, match the
* (RequestedRegion) size of the feature image.
*
* You must identify the initial front (surface) in the input image. You do
* this by specifying its isovalue through the method SetIsoSurfaceValue(float
* f). The algorithm will then initialize its solution using the front represented by
* value f. Note that the front is always represented by isosurface zero in
* the output and not the isosurface you specified for the input. This is
* because, for simplicity, the filter will shift your input image so that the
* active front has zero values.
*
* \par
* Depending on the particular application and filter that you are using, the
* feature image should be preprocessed with some type of noise reduction
* filtering. The feature image input can be of any type, but it will be cast
* to floating point before calculations are done.
*
* \par OUTPUTS
* The output of any subclass of this filter is a level set embedding as
* described in SparseFieldLevelSetImageFilter. The zero crossings of the output
* image give the pixels closest to the level set boundary. By ITK convention,
* NEGATIVE values are pixels INSIDE the segmented region and POSITIVE values are
* pixels OUTSIDE the segmented region.
*
* \par PARAMETERS
* The MaximumRMSChange parameter is used to determine when the solution has
* converged. A lower value will result in a tighter-fitting solution, but
* will require more computations. Too low a value could put the solver into
* an infinite loop unless a reasonable NumberOfIterations parameter is set.
* Values should always be greater than 0.0 and less than 1.0.
*
* \par
* The NumberOfIterations parameter can be used to halt the solution after a
* specified number of iterations, overriding the MaximumRMSChange halting
* criteria.
*
* \par
* The standard convention for ITK level-set segmentation filters is that
* POSITIVE propagation (speed) and advection terms cause the surface to EXPAND
* while negative terms cause the surface to CONTRACT. When the
* ReverseExpansionDirection parameter is set to TRUE (on), it tells the
* function object to reverse the standard ITK convention so that NEGATIVE
* terms cause EXPANSION and positive terms cause CONTRACTION.
*
* This parameter can be safely changed as appropriate for a particular
* application or data set to achieve the desired behavior.
*
* \par
* The FeatureScaling parameter controls the magnitude of the features calculated
* for use in the level set propagation and advection speeds. This value simply
* sets both parameters to equal values at once. Some filters may only use on of
* these two terms and this method is a generic way to set either or both without
* having to know which is in use.
*
* \par
* The CurvatureScaling parameter controls the magnitude of the curvature values
* which are calculated on the evolving isophote. This is important in
* controlling the relative effect of curvature in the calculation. Default
* value is 1.0. Higher values relative to the other level set equation terms
* (propagation and advection) will give a smoother result.
*
* \par
* The PropagationScaling parameter controls the scaling of the scalar
* propagation (speed) term relative to other terms in the level set
* equation. Setting this value will override any value already set by
* FeatureScaling.
*
* \par
* The AdvectionScaling parameter controls the scaling of the vector
* advection field term relative to other terms in the level set
* equation. Setting this value will override any value already set by
* FeatureScaling.
*
* \warning This is an abstract class. It is not intended to be instantiated
* by itself. Instead, you should use the derived classes. This is the reason
* why the New() operator (itkNewMacro) is missing from the class API.
*
* \par
* See LevelSetFunction for more information.
* \ingroup ITKLevelSets
*/
template <typename TInputImage, typename TFeatureImage, typename TOutputPixelType = float>
class ITK_TEMPLATE_EXPORT SegmentationLevelSetImageFilter
: public SparseFieldLevelSetImageFilter<TInputImage, Image<TOutputPixelType, TInputImage::ImageDimension>>
{
public:
ITK_DISALLOW_COPY_AND_MOVE(SegmentationLevelSetImageFilter);
/** Inherited type alias from the superclass. Needs to be placed before the next macro. */
using Self = SegmentationLevelSetImageFilter;
// static constexpr unsigned int InputImageDimension = TInputImage::ImageDimension;
/** Output image type alias */
using OutputImageType = Image<TOutputPixelType, Self::InputImageDimension>;
/** Standard class type aliases */
using Superclass = SparseFieldLevelSetImageFilter<TInputImage, OutputImageType>;
using Pointer = SmartPointer<Self>;
using ConstPointer = SmartPointer<const Self>;
/** Inherited type alias from the superclass. */
using typename Superclass::ValueType;
using typename Superclass::IndexType;
using typename Superclass::TimeStepType;
using typename Superclass::InputImageType;
/** Local image type alias */
using FeatureImageType = TFeatureImage;
/** The generic level set function type */
using SegmentationFunctionType = SegmentationLevelSetFunction<OutputImageType, FeatureImageType>;
/** The type used for the advection field */
using VectorImageType = typename SegmentationFunctionType::VectorImageType;
using SpeedImageType = typename SegmentationFunctionType::ImageType;
/** \see LightObject::GetNameOfClass() */
itkOverrideGetNameOfClassMacro(SegmentationLevelSetImageFilter);
/** Set/Get the maximum number of iterations allowed for the solver. This
* prevents infinite loops if a solution "bounces". */
void
SetMaximumIterations(unsigned int i)
{
itkWarningMacro("SetMaximumIterations is deprecated. Please use SetNumberOfIterations instead.");
this->SetNumberOfIterations(i);
}
unsigned int
GetMaximumIterations()
{
itkWarningMacro("GetMaximumIterations is deprecated. Please use GetNumberOfIterations instead.");
return this->GetNumberOfIterations();
}
/** Set/Get the feature image to be used for speed function of the level set
* equation. Equivalent to calling Set/GetInput(1, ..) */
virtual void
SetFeatureImage(const FeatureImageType * f)
{
itkDebugMacro("setting input FeatureImage to " << f);
if (f != itkDynamicCastInDebugMode<FeatureImageType *>(this->ProcessObject::GetInput("FeatureImage")))
{
this->ProcessObject::SetInput("FeatureImage", const_cast<FeatureImageType *>(f));
m_SegmentationFunction->SetFeatureImage(f);
this->Modified();
}
}
itkGetInputMacro(FeatureImage, FeatureImageType);
/** Set/Get the initial level set model. Equivalent to calling SetInput(..)
*/
itkSetInputMacro(InitialImage, InputImageType);
itkGetInputMacro(InitialImage, InputImageType);
/** Set the feature image */
void
SetInput2(const FeatureImageType * input)
{
this->SetFeatureImage(input);
}
/** This function is for advanced applications. Set the image sampled as the
* speed term of this segmentation method. In general, the speed image is
* generated automatically by a subclass of this filter. */
void
SetSpeedImage(SpeedImageType * s)
{
m_SegmentationFunction->SetSpeedImage(s);
}
/** This function is for advanced applications. Set the image sampled as the
* advection term of this segmentation method. In general, the advection image
* is generated automatically by a subclass of this filter. */
void
SetAdvectionImage(VectorImageType * v)
{
m_SegmentationFunction->SetAdvectionImage(v);
}
/** Return a pointer to the image sampled as the speed term of the
* segmentation algorithm. */
virtual const SpeedImageType *
GetSpeedImage() const
{
return m_SegmentationFunction->GetSpeedImage();
}
/** Return a pointer to the image sampled as the advection term of the
* segmentation algorithm. */
virtual const VectorImageType *
GetAdvectionImage() const
{
return m_SegmentationFunction->GetAdvectionImage();
}
/** THIS METHOD IS DEPRECATED AND SHOULD NOT BE USED. This method reverses
* the speed function direction, effectively changing inside feature values to
* outside feature values and vice versa. */
void
SetUseNegativeFeaturesOn()
{
itkWarningMacro(
<< "SetUseNegativeFeaturesOn has been deprecated. Please use ReverseExpansionDirectionOn() instead");
this->ReverseExpansionDirectionOn();
}
void
SetUseNegativeFeaturesOff()
{
itkWarningMacro(
<< "SetUseNegativeFeaturesOff has been deprecated. Please use ReverseExpansionDirectionOff() instead");
this->ReverseExpansionDirectionOff();
}
/** THIS METHOD IS DEPRECATED AND SHOULD NOT BE USED. Set/Get the value of the UseNegativeFeatures flag. This method
* is deprecated. Use Set/Get ReverseExpansionDirection instead. */
void
SetUseNegativeFeatures(bool u)
{
itkWarningMacro("SetUseNegativeFeatures has been deprecated. Please use SetReverseExpansionDirection instead");
if (u)
{
this->SetReverseExpansionDirection(false);
}
else
{
this->SetReverseExpansionDirection(true);
}
}
bool
GetUseNegativeFeatures() const
{
itkWarningMacro(
<< "GetUseNegativeFeatures has been deprecated. Please use GetReverseExpansionDirection() instead");
if (m_ReverseExpansionDirection == false)
{
return true;
}
else
{
return false;
}
}
/** Turn On/Off the flag which determines whether Positive or Negative speed
* terms will cause surface expansion. If set to TRUE then negative speed
* terms will cause the surface to expand and positive speed terms will cause
* the surface to contract. If set to FALSE (default) then positive speed terms will
* cause the surface to expand and negative speed terms will cause the
* surface to contract. This method can be safely used to reverse the
* expansion/contraction as appropriate to a particular application or data
* set. */
itkSetMacro(ReverseExpansionDirection, bool);
itkGetConstMacro(ReverseExpansionDirection, bool);
itkBooleanMacro(ReverseExpansionDirection);
/** Turn On/Off automatic generation of Speed and Advection terms when Update
is called. If set to Off, the Speed and Advection images must be set
explicitly using SetSpeedImage, SetAdvectionImage OR the methods
GenerateSpeedImage() and GenerateAdvectionImage() should be called prior
to updating the filter. */
itkSetMacro(AutoGenerateSpeedAdvection, bool);
itkGetConstMacro(AutoGenerateSpeedAdvection, bool);
itkBooleanMacro(AutoGenerateSpeedAdvection);
/** Combined scaling of the propagation and advection speed
terms. You should use either this -or- Get/SetPropagationScaling and
Get/SetAdvectionScaling (if appropriate). See subclasses for details
on when and whether to set these parameters. */
void
SetFeatureScaling(ValueType v)
{
if (Math::NotExactlyEquals(v, m_SegmentationFunction->GetPropagationWeight()))
{
this->SetPropagationScaling(v);
}
if (Math::NotExactlyEquals(v, m_SegmentationFunction->GetAdvectionWeight()))
{
this->SetAdvectionScaling(v);
}
}
/** Set/Get the scaling of the propagation speed. Setting the FeatureScaling
parameter overrides any previous values set for PropagationScaling. */
void
SetPropagationScaling(ValueType v)
{
if (Math::NotExactlyEquals(v, m_SegmentationFunction->GetPropagationWeight()))
{
m_SegmentationFunction->SetPropagationWeight(v);
this->Modified();
}
}
ValueType
GetPropagationScaling() const
{
return m_SegmentationFunction->GetPropagationWeight();
}
/** Set/Get the scaling of the advection field. Setting the FeatureScaling
parameter will override any existing value for AdvectionScaling. */
void
SetAdvectionScaling(ValueType v)
{
if (Math::NotExactlyEquals(v, m_SegmentationFunction->GetAdvectionWeight()))
{
m_SegmentationFunction->SetAdvectionWeight(v);
this->Modified();
}
}
ValueType
GetAdvectionScaling() const
{
return m_SegmentationFunction->GetAdvectionWeight();
}
/** Set/Get the scaling of the curvature. Use this parameter to
* increase the influence of curvature on the movement of the
* surface. Higher values relative to Advection and Propagation
* values will give smoother surfaces. */
void
SetCurvatureScaling(ValueType v)
{
if (Math::NotExactlyEquals(v, m_SegmentationFunction->GetCurvatureWeight()))
{
m_SegmentationFunction->SetCurvatureWeight(v);
this->Modified();
}
}
ValueType
GetCurvatureScaling() const
{
return m_SegmentationFunction->GetCurvatureWeight();
}
/** */
void
SetUseMinimalCurvature(bool b)
{
if (m_SegmentationFunction->GetUseMinimalCurvature() != b)
{
m_SegmentationFunction->SetUseMinimalCurvature(b);
this->Modified();
}
}
bool
GetUseMinimalCurvature() const
{
return m_SegmentationFunction->GetUseMinimalCurvature();
}
void
UseMinimalCurvatureOn()
{
this->SetUseMinimalCurvature(true);
}
void
UseMinimalCurvatureOff()
{
this->SetUseMinimalCurvature(false);
}
/** Set the segmentation function. In general, this should only be
* called by a subclass of this object. It is made public to allow
* itk::Command objects access. The method is inline to avoid a
* problem with the gcc 2.95 compiler matching the declaration with
* the definition. */
virtual void
SetSegmentationFunction(SegmentationFunctionType * s)
{
m_SegmentationFunction = s;
typename SegmentationFunctionType::RadiusType r;
r.Fill(1);
m_SegmentationFunction->Initialize(r);
this->SetDifferenceFunction(m_SegmentationFunction);
this->Modified();
}
virtual SegmentationFunctionType *
GetSegmentationFunction()
{
return m_SegmentationFunction;
}
/** Set/Get the maximum constraint for the curvature term factor in the time step
* calculation. Changing this value from the default is not recommended or
* necessary but could be used to speed up the surface evolution at the risk
* of creating an unstable solution. */
void
SetMaximumCurvatureTimeStep(double n)
{
if (n != m_SegmentationFunction->GetMaximumCurvatureTimeStep())
{
m_SegmentationFunction->SetMaximumCurvatureTimeStep(n);
this->Modified();
}
}
double
GetMaximumCurvatureTimeStep() const
{
return m_SegmentationFunction->GetMaximumCurvatureTimeStep();
}
/** Set/Get the maximum constraint for the scalar/vector term factor of the time step
* calculation. Changing this value from the default is not recommended or
* necessary but could be used to speed up the surface evolution at the risk
* of creating an unstable solution. */
void
SetMaximumPropagationTimeStep(double n)
{
if (n != m_SegmentationFunction->GetMaximumPropagationTimeStep())
{
m_SegmentationFunction->SetMaximumPropagationTimeStep(n);
this->Modified();
}
}
double
GetMaximumPropagationTimeStep() const
{
return m_SegmentationFunction->GetMaximumPropagationTimeStep();
}
/** Allocate and calculate the speed term image in the SegmentationFunction
object. This method is called automatically on filter execution
unless AutoGenerateSpeedAdvection is set to Off. */
void
GenerateSpeedImage();
/** Allocate and calculate the advection term image in the
* SegmentationFunction object This method is called automatically on
* filter execution unless AutoGenerateSpeedAdvection is set to Off. */
void
GenerateAdvectionImage();
#ifdef ITK_USE_CONCEPT_CHECKING
// Begin concept checking
itkConceptMacro(OutputHasNumericTraitsCheck, (Concept::HasNumericTraits<TOutputPixelType>));
// End concept checking
#endif
protected:
~SegmentationLevelSetImageFilter() override = default;
SegmentationLevelSetImageFilter();
void
PrintSelf(std::ostream & os, Indent indent) const override;
/** Overrides parent implementation */
void
InitializeIteration() override
{
Superclass::InitializeIteration();
// Estimate the progress of the filter
this->UpdateProgress(static_cast<float>(this->GetElapsedIterations()) /
static_cast<float>(this->GetNumberOfIterations()));
}
/** Overridden from ProcessObject to set certain values before starting the
* finite difference solver and then create an appropriate output */
void
GenerateData() override;
/** Flag which sets the inward/outward direction of propagation speed. See
SetReverseExpansionDirection for more information. */
bool m_ReverseExpansionDirection{};
/** Flag to indicate whether Speed and Advection images are automatically
* generated when running the filter. Otherwise, a pointer to images must
* be explicitly set or GenerateSpeedImage() and/or GenerateAdvectionImage()
* called directly before updating the filter */
bool m_AutoGenerateSpeedAdvection{};
private:
SegmentationFunctionType * m_SegmentationFunction{};
};
} // end namespace itk
#ifndef ITK_MANUAL_INSTANTIATION
# include "itkSegmentationLevelSetImageFilter.hxx"
#endif
#endif
|