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
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkScalarImageToGreyLevelCooccurrenceMatrixGenerator.txx,v $
Language: C++
Date: $Date: 2009-03-04 19:29:54 $
Version: $Revision: 1.11 $
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 __itkScalarImageToGreyLevelCooccurrenceMatrixGenerator_txx
#define __itkScalarImageToGreyLevelCooccurrenceMatrixGenerator_txx
#include "itkScalarImageToGreyLevelCooccurrenceMatrixGenerator.h"
#include "itkConstNeighborhoodIterator.h"
#include "vnl/vnl_math.h"
namespace itk {
namespace Statistics {
template< class TImageType, class THistogramFrequencyContainer >
ScalarImageToGreyLevelCooccurrenceMatrixGenerator< TImageType,
THistogramFrequencyContainer >::
ScalarImageToGreyLevelCooccurrenceMatrixGenerator() :
m_NumberOfBinsPerAxis(itkGetStaticConstMacro(DefaultBinsPerAxis)), m_Normalize(false)
{
m_LowerBound.Fill(NumericTraits<PixelType>::min());
m_UpperBound.Fill(NumericTraits<PixelType>::max() + 1);
m_Min = NumericTraits<PixelType>::min();
m_Max = NumericTraits<PixelType>::max();
}
template< class TImageType, class THistogramFrequencyContainer >
void
ScalarImageToGreyLevelCooccurrenceMatrixGenerator< TImageType,
THistogramFrequencyContainer >::
Compute( void )
{
// First, create an appropriate histogram with the right number of bins
// and mins and maxes correct for the image type.
m_Output = HistogramType::New();
typename HistogramType::SizeType size;
size.Fill(m_NumberOfBinsPerAxis);
m_Output->Initialize(size, m_LowerBound, m_UpperBound);
// Next, find the minimum radius that encloses all the offsets.
unsigned int minRadius = 0;
typename OffsetVector::ConstIterator offsets;
for(offsets = m_Offsets->Begin(); offsets != m_Offsets->End(); offsets++)
{
for(unsigned int i = 0; i < offsets.Value().GetOffsetDimension(); i++)
{
unsigned int distance = vnl_math_abs(offsets.Value()[i]);
if(distance > minRadius)
{
minRadius = distance;
}
}
}
RadiusType radius;
radius.Fill(minRadius);
// Now fill in the histogram
this->FillHistogram(radius, m_Input->GetRequestedRegion());
// Normalizse the histogram if requested
if(m_Normalize)
{
this->NormalizeHistogram();
}
}
template< class TImageType, class THistogramFrequencyContainer >
void
ScalarImageToGreyLevelCooccurrenceMatrixGenerator< TImageType,
THistogramFrequencyContainer >::
FillHistogram(RadiusType radius, RegionType region)
{
// Iterate over all of those pixels and offsets, adding each
// co-occurrence pair to the histogram
typedef ConstNeighborhoodIterator<ImageType> NeighborhoodIteratorType;
NeighborhoodIteratorType neighborIt;
neighborIt = NeighborhoodIteratorType(radius, m_Input, region);
for (neighborIt.GoToBegin(); !neighborIt.IsAtEnd(); ++neighborIt)
{
const PixelType centerPixelIntensity = neighborIt.GetCenterPixel();
if (centerPixelIntensity < m_Min ||
centerPixelIntensity > m_Max)
{
continue; // don't put a pixel in the histogram if the value
// is out-of-bounds.
}
typename OffsetVector::ConstIterator offsets;
for(offsets = m_Offsets->Begin(); offsets != m_Offsets->End(); offsets++)
{
bool pixelInBounds;
const PixelType pixelIntensity =
neighborIt.GetPixel(offsets.Value(), pixelInBounds);
if (!pixelInBounds)
{
continue; // don't put a pixel in the histogram if it's out-of-bounds.
}
if (pixelIntensity < m_Min ||
pixelIntensity > m_Max)
{
continue; // don't put a pixel in the histogram if the value
// is out-of-bounds.
}
// Now make both possible co-occurrence combinations and increment the
// histogram with them.
MeasurementVectorType cooccur;
cooccur[0] = centerPixelIntensity;
cooccur[1] = pixelIntensity;
m_Output->IncreaseFrequency(cooccur, 1);
cooccur[1] = centerPixelIntensity;
cooccur[0] = pixelIntensity;
m_Output->IncreaseFrequency(cooccur, 1);
}
}
}
template< class TImageType, class THistogramFrequencyContainer >
void
ScalarImageToGreyLevelCooccurrenceMatrixGenerator< TImageType,
THistogramFrequencyContainer >::
NormalizeHistogram( void )
{
typename HistogramType::Iterator hit;
typename HistogramType::FrequencyType totalFrequency =
m_Output->GetTotalFrequency();
for (hit = m_Output->Begin(); hit != m_Output->End(); ++hit)
{
hit.SetFrequency(hit.GetFrequency() / totalFrequency);
}
}
template< class TImageType, class THistogramFrequencyContainer >
void
ScalarImageToGreyLevelCooccurrenceMatrixGenerator< TImageType,
THistogramFrequencyContainer >::
SetPixelValueMinMax( PixelType min, PixelType max )
{
itkDebugMacro("setting Min to " << min << "and Max to " << max);
m_Min = min;
m_Max = max;
m_LowerBound.Fill(min);
m_UpperBound.Fill(max + 1);
this->Modified();
}
template< class TImageType, class THistogramFrequencyContainer >
void
ScalarImageToGreyLevelCooccurrenceMatrixGenerator< TImageType,
THistogramFrequencyContainer >::
PrintSelf(std::ostream& os, Indent indent) const
{
Superclass::PrintSelf(os,indent);
}
} // end of namespace Statistics
} // end of namespace itk
#endif
|