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 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkLabelStatisticsImageFilter.txx,v $
Language: C++
Date: $Date: 2008-02-13 15:59:36 $
Version: $Revision: 1.18 $
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 _itkLabelStatisticsImageFilter_txx
#define _itkLabelStatisticsImageFilter_txx
#include "itkLabelStatisticsImageFilter.h"
#include "itkImageRegionIterator.h"
#include "itkImageRegionConstIterator.h"
#include "itkImageRegionConstIteratorWithIndex.h"
#include "itkNumericTraits.h"
#include "itkProgressReporter.h"
namespace itk {
#if defined(__GNUC__) && (__GNUC__ <= 2) //NOTE: This class needs a mutex for gnu 2.95
/** Used for mutex locking */
#define LOCK_HASHMAP this->m_Mutex.Lock()
#define UNLOCK_HASHMAP this->m_Mutex.Unlock()
#else
#define LOCK_HASHMAP
#define UNLOCK_HASHMAP
#endif
template<class TInputImage, class TLabelImage>
LabelStatisticsImageFilter<TInputImage, TLabelImage>
::LabelStatisticsImageFilter()
{
this->SetNumberOfRequiredInputs(2);
m_UseHistograms = false;
m_NumBins[0] = 20;
m_LowerBound = static_cast<RealType>( NumericTraits<PixelType>::NonpositiveMin() );
m_UpperBound = static_cast<RealType>( NumericTraits<PixelType>::max() );
}
template<class TInputImage, class TLabelImage>
void
LabelStatisticsImageFilter<TInputImage, TLabelImage>
::GenerateInputRequestedRegion()
{
Superclass::GenerateInputRequestedRegion();
if ( this->GetInput() )
{
InputImagePointer image =
const_cast< typename Superclass::InputImageType * >( this->GetInput() );
if (image)
{
image->SetRequestedRegionToLargestPossibleRegion();
}
}
if ( this->GetLabelInput() )
{
LabelImagePointer label =
const_cast< TLabelImage * >( this->GetLabelInput() );
label->SetRequestedRegionToLargestPossibleRegion();
}
}
template<class TInputImage, class TLabelImage>
void
LabelStatisticsImageFilter<TInputImage, TLabelImage>
::EnlargeOutputRequestedRegion(DataObject *data)
{
Superclass::EnlargeOutputRequestedRegion(data);
data->SetRequestedRegionToLargestPossibleRegion();
}
template<class TInputImage, class TLabelImage>
void
LabelStatisticsImageFilter<TInputImage, TLabelImage>
::AllocateOutputs()
{
// Pass the input through as the output
InputImagePointer image =
const_cast< TInputImage * >( this->GetInput() );
this->GraftOutput( image );
// Nothing that needs to be allocated for the remaining outputs
}
template<class TInputImage, class TLabelImage>
void
LabelStatisticsImageFilter<TInputImage, TLabelImage>
::SetHistogramParameters(const int numBins, RealType lowerBound, RealType upperBound)
{
m_NumBins[0] = numBins;
m_LowerBound = lowerBound;
m_UpperBound = upperBound;
m_UseHistograms = true;
}
template<class TInputImage, class TLabelImage>
void
LabelStatisticsImageFilter<TInputImage, TLabelImage>
::BeforeThreadedGenerateData()
{
int numberOfThreads = this->GetNumberOfThreads();
// Resize the thread temporaries
m_LabelStatisticsPerThread.resize(numberOfThreads);
// Initialize the temporaries
for (int i=0; i < numberOfThreads; ++i)
{
m_LabelStatisticsPerThread[i].clear();
}
// Initialize the final map
m_LabelStatistics.clear();
}
template<class TInputImage, class TLabelImage>
void
LabelStatisticsImageFilter<TInputImage, TLabelImage>
::AfterThreadedGenerateData()
{
MapIterator mapIt;
MapConstIterator threadIt;
int i;
int numberOfThreads = this->GetNumberOfThreads();
// Run through the map for each thread and accumulate the count,
// sum, and sumofsquares
for (i = 0; i < numberOfThreads; i++)
{
// iterate over the map for this thread
for (threadIt = m_LabelStatisticsPerThread[i].begin();
threadIt != m_LabelStatisticsPerThread[i].end();
++threadIt)
{
// does this label exist in the cumulative stucture yet?
mapIt = m_LabelStatistics.find( (*threadIt).first );
if (mapIt == m_LabelStatistics.end())
{
// create a new entry
typedef typename MapType::value_type MapValueType;
if (m_UseHistograms)
{
mapIt = m_LabelStatistics.insert( MapValueType((*threadIt).first,
LabelStatistics(m_NumBins[0], m_LowerBound, m_UpperBound)) ).first;
}
else
{
mapIt = m_LabelStatistics.insert( MapValueType((*threadIt).first,
LabelStatistics()) ).first;
}
}
// accumulate the information from this thread
(*mapIt).second.m_Count += (*threadIt).second.m_Count;
(*mapIt).second.m_Sum += (*threadIt).second.m_Sum;
(*mapIt).second.m_SumOfSquares += (*threadIt).second.m_SumOfSquares;
if ((*mapIt).second.m_Minimum > (*threadIt).second.m_Minimum)
{
(*mapIt).second.m_Minimum = (*threadIt).second.m_Minimum;
}
if ((*mapIt).second.m_Maximum < (*threadIt).second.m_Maximum)
{
(*mapIt).second.m_Maximum = (*threadIt).second.m_Maximum;
}
//bounding box is min,max pairs
int dimension = (*mapIt).second.m_BoundingBox.size() / 2;
for (int ii = 0; ii < (dimension * 2) ; ii += 2 )
{
if ((*mapIt).second.m_BoundingBox[ii] > (*threadIt).second.m_BoundingBox[ii])
{
(*mapIt).second.m_BoundingBox[ii] = (*threadIt).second.m_BoundingBox[ii];
}
if ((*mapIt).second.m_BoundingBox[ii + 1] < (*threadIt).second.m_BoundingBox[ii + 1])
{
(*mapIt).second.m_BoundingBox[ii + 1] = (*threadIt).second.m_BoundingBox[ii + 1];
}
}
// if enabled, update the histogram for this label
if (m_UseHistograms)
{
typename HistogramType::IndexType index;
for (unsigned int bin=0; bin<m_NumBins[0]; bin++)
{
index[0] = bin;
(*mapIt).second.m_Histogram->IncreaseFrequency(bin, (*threadIt).second.m_Histogram->GetFrequency(bin));
}
}
} // end of thread map iterator loop
} // end of thread loop
// compute the remainder of the statistics
for (mapIt = m_LabelStatistics.begin();
mapIt != m_LabelStatistics.end();
++mapIt)
{
// mean
(*mapIt).second.m_Mean = (*mapIt).second.m_Sum /
static_cast<RealType>( (*mapIt).second.m_Count );
// variance
if ((*mapIt).second.m_Count > 1)
{
// unbiased estimate of variance
(*mapIt).second.m_Variance
= ((*mapIt).second.m_SumOfSquares
- ((*mapIt).second.m_Sum*(*mapIt).second.m_Sum
/ static_cast<RealType>((*mapIt).second.m_Count)))
/ (static_cast<RealType>((*mapIt).second.m_Count) - 1);
}
else
{
(*mapIt).second.m_Variance = NumericTraits<RealType>::Zero;
}
// sigma
(*mapIt).second.m_Sigma = vcl_sqrt((*mapIt).second.m_Variance);
}
}
template<class TInputImage, class TLabelImage>
void
LabelStatisticsImageFilter<TInputImage, TLabelImage>
::ThreadedGenerateData(const RegionType& outputRegionForThread,
int threadId)
{
RealType value;
LabelPixelType label;
ImageRegionConstIteratorWithIndex<TInputImage> it (this->GetInput(),
outputRegionForThread);
ImageRegionConstIterator<TLabelImage> labelIt (this->GetLabelInput(),
outputRegionForThread);
MapIterator mapIt;
// support progress methods/callbacks
ProgressReporter progress(this, threadId,
outputRegionForThread.GetNumberOfPixels());
// do the work
while (!it.IsAtEnd())
{
value = static_cast<RealType>(it.Get());
label = labelIt.Get();
// is the label already in this thread?
mapIt = m_LabelStatisticsPerThread[threadId].find( label );
if (mapIt == m_LabelStatisticsPerThread[threadId].end())
{
// create a new statistics object
typedef typename MapType::value_type MapValueType;
LOCK_HASHMAP;
if (m_UseHistograms)
{
mapIt = m_LabelStatisticsPerThread[threadId].insert( MapValueType(label,
LabelStatistics(m_NumBins[0], m_LowerBound, m_UpperBound)) ).first;
}
else
{
mapIt = m_LabelStatisticsPerThread[threadId].insert( MapValueType(label,
LabelStatistics()) ).first;
}
UNLOCK_HASHMAP;
}
// update the values for this label and this thread
if (value < (*mapIt).second.m_Minimum)
{
(*mapIt).second.m_Minimum = value;
}
if (value > (*mapIt).second.m_Maximum)
{
(*mapIt).second.m_Maximum = value;
}
// bounding box is min,max pairs
for (unsigned int i = 0; i < ( 2 * it.GetImageDimension()) ; i+=2 )
{
typename ImageRegionConstIteratorWithIndex<TInputImage>::IndexType index = it.GetIndex();
if ((*mapIt).second.m_BoundingBox[i] > index[i/2])
{
(*mapIt).second.m_BoundingBox[i] = index[i/2];
}
if ((*mapIt).second.m_BoundingBox[i + 1] < index[i/2])
{
(*mapIt).second.m_BoundingBox[i + 1] = index[i/2];
}
}
(*mapIt).second.m_Sum += value;
(*mapIt).second.m_SumOfSquares += (value * value);
(*mapIt).second.m_Count++;
// if enabled, update the histogram for this label
if (m_UseHistograms)
{
typename HistogramType::MeasurementVectorType meas;
meas[0] = value;
(*mapIt).second.m_Histogram->IncreaseFrequency(meas, 1.0F);
}
++it;
++labelIt;
progress.CompletedPixel();
}
}
template<class TInputImage, class TLabelImage>
typename LabelStatisticsImageFilter<TInputImage, TLabelImage>::RealType
LabelStatisticsImageFilter<TInputImage, TLabelImage>
::GetMinimum(LabelPixelType label) const
{
MapConstIterator mapIt;
mapIt = m_LabelStatistics.find( label );
if ( mapIt == m_LabelStatistics.end() )
{
// label does not exist, return a default value
return NumericTraits<PixelType>::max();
}
else
{
return (*mapIt).second.m_Minimum;
}
}
template<class TInputImage, class TLabelImage>
typename LabelStatisticsImageFilter<TInputImage, TLabelImage>::RealType
LabelStatisticsImageFilter<TInputImage, TLabelImage>
::GetMaximum(LabelPixelType label) const
{
MapConstIterator mapIt;
mapIt = m_LabelStatistics.find( label );
if ( mapIt == m_LabelStatistics.end() )
{
// label does not exist, return a default value
return NumericTraits<PixelType>::NonpositiveMin();
}
else
{
return (*mapIt).second.m_Maximum;
}
}
template<class TInputImage, class TLabelImage>
typename LabelStatisticsImageFilter<TInputImage, TLabelImage>::RealType
LabelStatisticsImageFilter<TInputImage, TLabelImage>
::GetMean(LabelPixelType label) const
{
MapConstIterator mapIt;
mapIt = m_LabelStatistics.find( label );
if ( mapIt == m_LabelStatistics.end() )
{
// label does not exist, return a default value
return NumericTraits<PixelType>::Zero;
}
else
{
return (*mapIt).second.m_Mean;
}
}
template<class TInputImage, class TLabelImage>
typename LabelStatisticsImageFilter<TInputImage, TLabelImage>::RealType
LabelStatisticsImageFilter<TInputImage, TLabelImage>
::GetSum(LabelPixelType label) const
{
MapConstIterator mapIt;
mapIt = m_LabelStatistics.find( label );
if ( mapIt == m_LabelStatistics.end() )
{
// label does not exist, return a default value
return NumericTraits<PixelType>::Zero;
}
else
{
return (*mapIt).second.m_Sum;
}
}
template<class TInputImage, class TLabelImage>
typename LabelStatisticsImageFilter<TInputImage, TLabelImage>::RealType
LabelStatisticsImageFilter<TInputImage, TLabelImage>
::GetSigma(LabelPixelType label) const
{
MapConstIterator mapIt;
mapIt = m_LabelStatistics.find( label );
if ( mapIt == m_LabelStatistics.end() )
{
// label does not exist, return a default value
return NumericTraits<PixelType>::Zero;
}
else
{
return (*mapIt).second.m_Sigma;
}
}
template<class TInputImage, class TLabelImage>
typename LabelStatisticsImageFilter<TInputImage, TLabelImage>::RealType
LabelStatisticsImageFilter<TInputImage, TLabelImage>
::GetVariance(LabelPixelType label) const
{
MapConstIterator mapIt;
mapIt = m_LabelStatistics.find( label );
if ( mapIt == m_LabelStatistics.end() )
{
// label does not exist, return a default value
return NumericTraits<PixelType>::Zero;
}
else
{
return (*mapIt).second.m_Variance;
}
}
template<class TInputImage, class TLabelImage>
typename LabelStatisticsImageFilter<TInputImage, TLabelImage>::BoundingBoxType
LabelStatisticsImageFilter<TInputImage, TLabelImage>
::GetBoundingBox(LabelPixelType label) const
{
MapConstIterator mapIt;
mapIt = m_LabelStatistics.find( label );
if ( mapIt == m_LabelStatistics.end() )
{
BoundingBoxType emptyBox;
// label does not exist, return a default value
return emptyBox;
}
else
{
return (*mapIt).second.m_BoundingBox;
}
}
template<class TInputImage, class TLabelImage>
typename LabelStatisticsImageFilter<TInputImage, TLabelImage>::RegionType
LabelStatisticsImageFilter<TInputImage, TLabelImage>
::GetRegion(LabelPixelType label) const
{
MapConstIterator mapIt;
mapIt = m_LabelStatistics.find( label );
if ( mapIt == m_LabelStatistics.end() )
{
RegionType emptyRegion;
// label does not exist, return a default value
return emptyRegion;
}
else
{
BoundingBoxType bbox = this->GetBoundingBox( label );
IndexType index;
SizeType size;
unsigned int dimension = bbox.size() / 2;
for (unsigned int i = 0; i < dimension; i++)
{
index[i] = bbox[2*i];
size[i] = bbox[2*i+1] - bbox[2*i] + 1;
}
RegionType region;
region.SetSize(size);
region.SetIndex(index);
return region;
}
}
template<class TInputImage, class TLabelImage>
unsigned long
LabelStatisticsImageFilter<TInputImage, TLabelImage>
::GetCount(LabelPixelType label) const
{
MapConstIterator mapIt;
mapIt = m_LabelStatistics.find( label );
if ( mapIt == m_LabelStatistics.end() )
{
// label does not exist, return a default value
return 0;
}
else
{
return (*mapIt).second.m_Count;
}
}
template<class TInputImage, class TLabelImage>
typename LabelStatisticsImageFilter<TInputImage, TLabelImage>::RealType
LabelStatisticsImageFilter<TInputImage, TLabelImage>
::GetMedian(LabelPixelType label) const
{
RealType median = 0.0;
MapConstIterator mapIt;
mapIt = m_LabelStatistics.find( label );
if ( mapIt == m_LabelStatistics.end() || !m_UseHistograms)
{
// label does not exist OR histograms not enabled, return a default value
return median;
}
else
{
typename HistogramType::SizeType::SizeValueType bin = 0;
typename HistogramType::IndexType index;
RealType total = 0;
// count bins until just over half the distribution is counted
while (total <= ((*mapIt).second.m_Count/ 2) && (bin < m_NumBins[0]))
{
index[0] = bin;
total += (*mapIt).second.m_Histogram->GetFrequency(index);
bin++;
}
bin--;
index[0] = bin;
// return center of bin range
RealType lowRange = (*mapIt).second.m_Histogram->GetBinMin(0, bin);
RealType highRange = (*mapIt).second.m_Histogram->GetBinMax(0, bin);
median = lowRange + (highRange - lowRange) / 2;
return median;
}
}
template<class TInputImage, class TLabelImage>
typename LabelStatisticsImageFilter<TInputImage, TLabelImage>::HistogramPointer
LabelStatisticsImageFilter<TInputImage, TLabelImage>
::GetHistogram(LabelPixelType label) const
{
MapConstIterator mapIt;
mapIt = m_LabelStatistics.find( label );
if ( mapIt == m_LabelStatistics.end())
{
// label does not exist, return a default value
return 0;
}
else
{
// this will be zero if histograms have not been enabled
return (*mapIt).second.m_Histogram;
}
}
template <class TImage, class TLabelImage>
void
LabelStatisticsImageFilter<TImage, TLabelImage>
::PrintSelf(std::ostream& os, Indent indent) const
{
Superclass::PrintSelf(os,indent);
os << indent << "Number of labels: " << m_LabelStatistics.size()
<< std::endl;
os << indent << "Use Histograms: " << m_UseHistograms
<< std::endl;
os << indent << "Histogram Lower Bound: " << m_LowerBound
<< std::endl;
os << indent << "Histogram Upper Bound: " << m_UpperBound
<< std::endl;
}
}// end namespace itk
#endif
|