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
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkIsolatedConnectedImageFilter.txx,v $
Language: C++
Date: $Date: 2006-03-19 04:36:56 $
Version: $Revision: 1.27 $
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 __itkIsolatedConnectedImageFilter_txx_
#define __itkIsolatedConnectedImageFilter_txx_
#include "itkIsolatedConnectedImageFilter.h"
#include "itkBinaryThresholdImageFunction.h"
#include "itkFloodFilledImageFunctionConditionalIterator.h"
#include "itkProgressReporter.h"
#include "itkIterationReporter.h"
namespace itk
{
/**
* Constructor
*/
template <class TInputImage, class TOutputImage>
IsolatedConnectedImageFilter<TInputImage, TOutputImage>
::IsolatedConnectedImageFilter()
{
m_Lower = NumericTraits<InputImagePixelType>::NonpositiveMin();
m_Upper = NumericTraits<InputImagePixelType>::max();
m_Seeds1.clear();
m_Seeds2.clear();
m_ReplaceValue = NumericTraits<OutputImagePixelType>::One;
m_IsolatedValue = NumericTraits<InputImagePixelType >::Zero;
m_IsolatedValueTolerance = NumericTraits<InputImagePixelType >::One;
m_FindUpperThreshold = true;
m_ThresholdingFailed = false;
}
/**
* Standard PrintSelf method.
*/
template <class TInputImage, class TOutputImage>
void
IsolatedConnectedImageFilter<TInputImage, TOutputImage>
::PrintSelf(std::ostream& os, Indent indent) const
{
this->Superclass::PrintSelf(os, indent);
os << indent << "Lower: "
<< static_cast<typename NumericTraits<InputImagePixelType>::PrintType>(m_Lower)
<< std::endl;
os << indent << "Upper: "
<< static_cast<typename NumericTraits<InputImagePixelType>::PrintType>(m_Upper)
<< std::endl;
os << indent << "ReplaceValue: "
<< static_cast<typename NumericTraits<OutputImagePixelType>::PrintType>(m_ReplaceValue)
<< std::endl;
os << indent << "IsolatedValue: "
<< static_cast<typename NumericTraits<InputImagePixelType>::PrintType>(m_IsolatedValue)
<< std::endl;
os << indent << "IsolatedValueTolerance: "
<< static_cast<typename NumericTraits<InputImagePixelType>::PrintType>(m_IsolatedValueTolerance)
<< std::endl;
os << indent << "FindUpperThreshold: "
<< static_cast<typename NumericTraits<bool>::PrintType>(m_FindUpperThreshold)
<< std::endl;
os << indent << "Thresholding Failed: "
<< static_cast<typename NumericTraits<bool>::PrintType>(m_ThresholdingFailed)
<< std::endl;
}
template <class TInputImage, class TOutputImage>
void
IsolatedConnectedImageFilter<TInputImage,TOutputImage>
::GenerateInputRequestedRegion()
{
Superclass::GenerateInputRequestedRegion();
if ( this->GetInput() )
{
InputImagePointer image =
const_cast< TInputImage * >( this->GetInput() );
image->SetRequestedRegionToLargestPossibleRegion();
}
}
template <class TInputImage, class TOutputImage>
void
IsolatedConnectedImageFilter<TInputImage,TOutputImage>
::EnlargeOutputRequestedRegion(DataObject *output)
{
Superclass::EnlargeOutputRequestedRegion(output);
output->SetRequestedRegionToLargestPossibleRegion();
}
template <class TInputImage, class TOutputImage>
void
IsolatedConnectedImageFilter<TInputImage,TOutputImage>
::GenerateData()
{
InputImageConstPointer inputImage = this->GetInput();
OutputImagePointer outputImage = this->GetOutput();
typedef typename NumericTraits<InputImagePixelType>::AccumulateType AccumulateType;
// Zero the output
OutputImageRegionType region = outputImage->GetRequestedRegion() ;
outputImage->SetBufferedRegion( region );
outputImage->Allocate();
outputImage->FillBuffer ( NumericTraits<OutputImagePixelType>::Zero );
typedef BinaryThresholdImageFunction<InputImageType> FunctionType;
typedef FloodFilledImageFunctionConditionalIterator<OutputImageType, FunctionType> IteratorType;
typename FunctionType::Pointer function = FunctionType::New();
function->SetInputImage ( inputImage );
float progressWeight;
float cumulatedProgress;
IteratorType it = IteratorType ( outputImage, function, m_Seeds1 );
IterationReporter iterate( this, 0, 1);
// If the upper threshold has not been set, find it.
if (m_FindUpperThreshold)
{
AccumulateType lower = static_cast<AccumulateType>(m_Lower);
AccumulateType upper = static_cast<AccumulateType>(m_Upper);
AccumulateType guess = upper;
// do a binary search to find an upper threshold that separates the
// two sets of seeds.
const unsigned int maximumIterationsInBinarySearch =
static_cast< unsigned int > (
vcl_log(( static_cast<float>( upper ) - static_cast< float >( lower ) ) /
static_cast<float>( m_IsolatedValueTolerance ) ) / vcl_log(2.0 ) );
progressWeight = 1.0f / static_cast<float>( maximumIterationsInBinarySearch + 2 );
cumulatedProgress = 0.0f;
while (lower + m_IsolatedValueTolerance < guess)
{
ProgressReporter progress( this, 0, region.GetNumberOfPixels(), 100, cumulatedProgress, progressWeight );
cumulatedProgress += progressWeight;
outputImage->FillBuffer ( NumericTraits<OutputImagePixelType>::Zero );
function->ThresholdBetween ( m_Lower, static_cast<InputImagePixelType>(guess));
it.GoToBegin();
while( !it.IsAtEnd())
{
it.Set(m_ReplaceValue);
if (it.GetIndex() == *m_Seeds2.begin())
{
break;
}
++it;
progress.CompletedPixel(); // potential exception thrown here
}
// If any of second seeds are included, decrease the upper bound.
// Find the sum of the intensities in m_Seeds2. If the second
// seeds are not included, the sum should be zero. Otherwise,
// it will be other than zero.
InputRealType seedIntensitySum = 0;
typename SeedsContainerType::const_iterator si = m_Seeds2.begin();
typename SeedsContainerType::const_iterator li = m_Seeds2.end();
while( si != li )
{
const InputRealType value =
static_cast< InputRealType >( outputImage->GetPixel( *si ) );
seedIntensitySum += value;
si++;
}
if (seedIntensitySum != 0)
{
upper = guess;
}
// Otherwise, increase the lower bound.
else
{
lower = guess;
}
guess = (upper + lower) /2;
iterate.CompletedStep();
}
m_IsolatedValue = static_cast<InputImagePixelType>(lower); //the lower bound on the upper threshold guess
}
// If the lower threshold has not been set, find it.
else if (!m_FindUpperThreshold)
{
AccumulateType lower = static_cast<AccumulateType>(m_Lower);
AccumulateType upper = static_cast<AccumulateType>(m_Upper);
AccumulateType guess = lower;
// do a binary search to find a lower threshold that separates the
// two sets of seeds.
const unsigned int maximumIterationsInBinarySearch =
static_cast< unsigned int > (
vcl_log(( static_cast<float>( upper ) - static_cast< float >( lower ) ) /
static_cast<float>( m_IsolatedValueTolerance ) ) / vcl_log(2.0 ) );
progressWeight = 1.0f / static_cast<float>( maximumIterationsInBinarySearch + 2 );
cumulatedProgress = 0.0f;
while (guess < upper - m_IsolatedValueTolerance)
{
ProgressReporter progress( this, 0, region.GetNumberOfPixels(), 100, cumulatedProgress, progressWeight );
cumulatedProgress += progressWeight;
outputImage->FillBuffer ( NumericTraits<OutputImagePixelType>::Zero );
function->ThresholdBetween ( static_cast<InputImagePixelType>(guess), m_Upper );
it.GoToBegin();
while( !it.IsAtEnd())
{
it.Set(m_ReplaceValue);
if (it.GetIndex() == *m_Seeds2.begin())
{
break;
}
++it;
progress.CompletedPixel(); // potential exception thrown here
}
// If any of second seeds are included, increase the lower bound.
// Find the sum of the intensities in m_Seeds2. If the second
// seeds are not included, the sum should be zero. Otherwise,
// it will be other than zero.
InputRealType seedIntensitySum = 0;
typename SeedsContainerType::const_iterator si = m_Seeds2.begin();
typename SeedsContainerType::const_iterator li = m_Seeds2.end();
while( si != li )
{
const InputRealType value =
static_cast< InputRealType >( outputImage->GetPixel( *si ) );
seedIntensitySum += value;
si++;
}
if (seedIntensitySum != 0)
{
lower = guess;
}
// Otherwise, decrease the upper bound.
else
{
upper = guess;
}
guess = (upper + lower) /2;
iterate.CompletedStep();
}
m_IsolatedValue = static_cast<InputImagePixelType>(upper); //the upper bound on the lower threshold guess
}
// now rerun the algorithm with the thresholds that separate the seeds.
ProgressReporter progress( this, 0, region.GetNumberOfPixels(), 100, cumulatedProgress, progressWeight );
outputImage->FillBuffer ( NumericTraits<OutputImagePixelType>::Zero );
if (m_FindUpperThreshold)
{
function->ThresholdBetween ( m_Lower, m_IsolatedValue);
}
else if (!m_FindUpperThreshold)
{
function->ThresholdBetween ( m_IsolatedValue, m_Upper);
}
it.GoToBegin();
while( !it.IsAtEnd())
{
it.Set(m_ReplaceValue);
++it;
progress.CompletedPixel(); // potential exception thrown here
}
// If any of the second seeds are included or some of the first
// seeds are not included, the algorithm could not find any threshold
// that would separate the two sets of seeds. Set an error flag in
// this case.
// Find the sum of the intensities in m_Seeds2. If the second
// seeds are not included, the sum should be zero. Otherwise,
// it will be other than zero.
InputRealType seed1IntensitySum = 0;
InputRealType seed2IntensitySum = 0;
typename SeedsContainerType::const_iterator si1 = m_Seeds1.begin();
typename SeedsContainerType::const_iterator li1 = m_Seeds1.end();
while( si1 != li1 )
{
const InputRealType value =
static_cast< InputRealType >( outputImage->GetPixel( *si1 ) );
seed1IntensitySum += value;
si1++;
}
typename SeedsContainerType::const_iterator si2 = m_Seeds2.begin();
typename SeedsContainerType::const_iterator li2 = m_Seeds2.end();
while( si2 != li2 )
{
const InputRealType value =
static_cast< InputRealType >( outputImage->GetPixel( *si2 ) );
seed2IntensitySum += value;
si2++;
}
if (seed1IntensitySum != m_ReplaceValue*m_Seeds1.size() || seed2IntensitySum != 0)
{
m_ThresholdingFailed = true;
}
iterate.CompletedStep();
}
} // end namespace itk
#endif
|