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
|
/*=========================================================================
*
* Copyright Insight Software Consortium
*
* 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.
*
*=========================================================================*/
#ifndef itkThresholdMaximumConnectedComponentsImageFilter_hxx
#define itkThresholdMaximumConnectedComponentsImageFilter_hxx
#include "itkThresholdMaximumConnectedComponentsImageFilter.h"
#include "itkImageRegionIterator.h"
#include "itkNumericTraits.h"
#include "itkObjectFactory.h"
namespace itk
{
/** Constructor
*
*/
template< typename TInputImage, typename TOutputImage >
ThresholdMaximumConnectedComponentsImageFilter< TInputImage, TOutputImage >
::ThresholdMaximumConnectedComponentsImageFilter()
{
m_ThresholdFilter = ThresholdFilterType::New();
m_ConnectedComponent = ConnectedFilterType::New();
m_LabeledComponent = RelabelFilterType::New();
m_MinMaxCalculator = MinMaxCalculatorType::New();
//
// Connecting the internal pipeline.
//
m_ConnectedComponent->SetInput( m_ThresholdFilter->GetOutput() );
m_LabeledComponent->SetInput( m_ConnectedComponent->GetOutput() );
const typename NumericTraits< PixelType >::AccumulateType maxLabel =
NumericTraits< PixelType >::max();
const typename NumericTraits< PixelType >::AccumulateType minLabel =
NumericTraits< PixelType >::NonpositiveMin();
//Default. Use ITK set macro "SetMinimumObjectSizeInPixels" to change
m_MinimumObjectSizeInPixels = 0;
m_ThresholdValue = static_cast< PixelType >( ( maxLabel + minLabel ) / 2 );
// Initialize values for the threshold filters
// Default. Use ITK set macro "SetOutsideValue" to change
m_OutsideValue = NumericTraits< OutputPixelType>::ZeroValue();
// Default. Use ITK set macro "SetInsideValue" to change
m_InsideValue = NumericTraits< OutputPixelType >::max();
m_LowerBoundary = m_ThresholdValue;
// Default. Use ITK set macro "SetUpperBoundary" to change
m_UpperBoundary = static_cast< PixelType >( maxLabel );
// Initialize the counter for the number of connected components
// (objects) in the image.
m_NumberOfObjects = 0;
} // end of the constructor
/**
*
*/
template< typename TInputImage, typename TOutputImage >
SizeValueType
ThresholdMaximumConnectedComponentsImageFilter< TInputImage, TOutputImage >
::ComputeConnectedComponents()
{
m_ThresholdFilter->SetLowerThreshold(m_ThresholdValue);
m_LabeledComponent->SetMinimumObjectSize(m_MinimumObjectSizeInPixels);
m_LabeledComponent->Update();
return m_LabeledComponent->GetNumberOfObjects();
} // end of ComputeConnectedComponents()
/**
* This is the meat of the filter. It essentially uses a bisection
* method to search for the threshold setPt that maximizes the number
* of connected components in the image. The
* "ComputeConnectedComponents" does the threshold and then a
* connected components object count. It is removed from "GenerateData"
* to make this all easier to read.
*
* Remove the comments on the output statements to see how the search
* strategy works.
*/
template< typename TInputImage, typename TOutputImage >
void ThresholdMaximumConnectedComponentsImageFilter< TInputImage, TOutputImage >
::GenerateData(void)
{
//
// Setup pointers to get input image and send info to output image
//
typename Superclass::InputImageConstPointer inputPtr = this->GetInput();
// Find the min and max of the image.
m_MinMaxCalculator->SetImage( this->GetInput() );
m_MinMaxCalculator->Compute();
// Initial values to maximize search strategy
// These are set to the smallest and largest image values so that
// there is no chance that the found threshold is outside of this range.
PixelType lowerBound = m_MinMaxCalculator->GetMinimum();
PixelType upperBound = m_MinMaxCalculator->GetMaximum();
// If the upper boundary is higher than the calculated maximum image
// value, clamp it to this value. This saves computation time
// because there is no reason to search for values higher than the
// max image value.
upperBound = std::min( upperBound, m_UpperBoundary );
m_ThresholdFilter->SetInput(inputPtr);
m_ThresholdFilter->SetOutsideValue(m_OutsideValue);
m_ThresholdFilter->SetInsideValue(m_InsideValue);
m_ThresholdFilter->SetUpperThreshold(m_UpperBoundary);
PixelType midpoint = ( upperBound - lowerBound ) / 2;
PixelType midpointL = ( lowerBound + ( midpoint - lowerBound ) / 2 );
PixelType midpointR = ( upperBound - ( upperBound - midpoint ) / 2 );
#ifndef NDEBUG
SizeValueType iterationCounter = 0;
#endif
while ( ( upperBound - lowerBound ) > 2 )
{
m_ThresholdValue = midpointR;
const SizeValueType connectedComponentsRight =
this->ComputeConnectedComponents();
m_ThresholdValue = midpointL;
const SizeValueType connectedComponentsLeft =
this->ComputeConnectedComponents();
// If the two thresholds give equal number of connected
// components, we choose the lower threshold.
if ( connectedComponentsRight > connectedComponentsLeft )
{
lowerBound = midpoint;
midpoint = midpointR;
m_NumberOfObjects = connectedComponentsRight;
}
else
{
upperBound = midpoint;
midpoint = midpointL;
m_NumberOfObjects = connectedComponentsLeft;
}
itkDebugMacro("lowerbound: " << lowerBound
<< "\t midpoint:" << midpoint
<< "\t upperBound:" << upperBound);
itkDebugMacro("Number of objects at left point: " << connectedComponentsLeft
<< "; at right point: " << connectedComponentsRight);
//
// Set up values for next iteration
//
midpointL = ( lowerBound + ( midpoint - lowerBound ) / 2 );
midpointR = ( upperBound - ( upperBound - midpoint ) / 2 );
#ifndef NDEBUG
itkDebugMacro("new midpointL: " << midpointL
<< "\t new midpoint:" << midpoint
<< "\t new midpointR:" << midpointR << std::endl);
itkDebugMacro("Iteration #:" << iterationCounter);
iterationCounter++;
#endif
} // end of the thresholdloop
//
// The two output values
//
m_ThresholdValue = midpoint;
m_ThresholdFilter->SetLowerThreshold(m_ThresholdValue);
m_ThresholdFilter->Update();
//
// Graft the output of the thresholding filter to the output of this filter.
//
this->GraftOutput( m_ThresholdFilter->GetOutput() );
} // end of GenerateData Process
/** Standard Run of the mill PrintSelf
*
*/
template< typename TInputImage, typename TOutputImage >
void
ThresholdMaximumConnectedComponentsImageFilter< TInputImage, TOutputImage >
::PrintSelf(std::ostream & os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "InsideValue: "
<< static_cast< typename NumericTraits< OutputPixelType >::PrintType >(
m_InsideValue ) << std::endl;
os << indent << "OutsideValue: "
<< static_cast< typename NumericTraits< OutputPixelType >::PrintType >(
m_OutsideValue ) << std::endl;
os << indent << "Lower: "
<< static_cast< typename NumericTraits< PixelType >::PrintType >(
m_LowerBoundary ) << std::endl;
os << indent << "Upper: "
<< static_cast< typename NumericTraits< PixelType >::PrintType >(
m_UpperBoundary ) << std::endl;
os << indent << "Threshold Value: "
<< static_cast< typename NumericTraits< PixelType >::PrintType >(
m_ThresholdValue ) << std::endl;
os << indent << "Number of Objects: " << m_NumberOfObjects << std::endl;
os << indent << "Minimum Object Size in Pixels: "
<< m_MinimumObjectSizeInPixels << std::endl;
}
} // end namespace itk
#endif
|