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
|
/*=========================================================================
*
* 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 itkLabelStatisticsImageFilter_h
#define itkLabelStatisticsImageFilter_h
#include "itkImageSink.h"
#include "itkNumericTraits.h"
#include "itkSimpleDataObjectDecorator.h"
#include "itkHistogram.h"
#include "itkPrintHelper.h"
#include <mutex>
#include <unordered_map>
#include <vector>
namespace itk
{
/** \class LabelStatisticsImageFilter
* \brief Given an intensity image and a label map, compute min, max, variance and mean of the pixels associated with
* each label or segment
*
* LabelStatisticsImageFilter computes the minimum, maximum, sum,
* mean, median, variance and sigma of regions of an intensity image, where
* the regions are defined via a label map (a second input). The
* label image should be integral type. The filter needs all of its
* input image. It behaves as a filter with an input and output. Thus
* it can be inserted in a pipeline with other filters and the
* statistics will only be recomputed if a downstream filter changes.
*
* Optionally, the filter also computes intensity histograms on each
* object. If histograms are enabled, a median intensity value can
* also be computed, although its accuracy is limited to the bin width
* of the histogram. If histograms are not enabled, the median returns
* zero.
*
* This filter is automatically multi-threaded and can stream its
* input when NumberOfStreamDivisions is set to more than
* 1. Statistics are independently computed for each streamed and
* threaded region then merged.
*
* \ingroup MathematicalStatisticsImageFilters
* \ingroup ITKImageStatistics
*
* \sphinx
* \sphinxexample{Filtering/ImageStatistics/StatisticalPropertiesOfRegions,Statistical Properties Of Labeled Regions}
* \endsphinx
*/
template <typename TInputImage, typename TLabelImage>
class ITK_TEMPLATE_EXPORT LabelStatisticsImageFilter : public ImageSink<TInputImage>
{
public:
ITK_DISALLOW_COPY_AND_MOVE(LabelStatisticsImageFilter);
/** Standard Self type alias */
using Self = LabelStatisticsImageFilter;
using Superclass = ImageSink<TInputImage>;
using Pointer = SmartPointer<Self>;
using ConstPointer = SmartPointer<const Self>;
/** Method for creation through the object factory. */
itkNewMacro(Self);
/** \see LightObject::GetNameOfClass() */
itkOverrideGetNameOfClassMacro(LabelStatisticsImageFilter);
/** Image related type alias. */
using InputImagePointer = typename TInputImage::Pointer;
using RegionType = typename TInputImage::RegionType;
using SizeType = typename TInputImage::SizeType;
using IndexType = typename TInputImage::IndexType;
using PixelType = typename TInputImage::PixelType;
/** Label image related type alias. */
using LabelImageType = TLabelImage;
using LabelImagePointer = typename TLabelImage::Pointer;
using LabelRegionType = typename TLabelImage::RegionType;
using LabelSizeType = typename TLabelImage::SizeType;
using LabelIndexType = typename TLabelImage::IndexType;
using LabelPixelType = typename TLabelImage::PixelType;
/** Image related type alias. */
static constexpr unsigned int ImageDimension = TInputImage::ImageDimension;
/** Type to use for computations. */
using RealType = typename NumericTraits<PixelType>::RealType;
/** Smart Pointer type to a DataObject. */
using DataObjectPointer = typename DataObject::Pointer;
/** Type of DataObjects used for scalar outputs */
using RealObjectType = SimpleDataObjectDecorator<RealType>;
/** Bounding Box-related type alias */
using BoundingBoxType = std::vector<IndexValueType>;
/** Histogram-related type alias */
using HistogramType = itk::Statistics::Histogram<RealType>;
using HistogramPointer = typename HistogramType::Pointer;
/** \class LabelStatistics
* \brief Statistics stored per label
* \ingroup ITKImageStatistics
*/
class LabelStatistics
{
public:
// default constructor
LabelStatistics()
{
// initialized to the default values
m_Count = IdentifierType{};
m_Sum = RealType{};
m_SumOfSquares = RealType{};
// Set such that the first pixel encountered can be compared
m_Minimum = NumericTraits<RealType>::max();
m_Maximum = NumericTraits<RealType>::NonpositiveMin();
// Default these to zero
m_Mean = RealType{};
m_Sigma = RealType{};
m_Variance = RealType{};
const unsigned int imageDimension = Self::ImageDimension;
m_BoundingBox.resize(imageDimension * 2);
for (unsigned int i = 0; i < imageDimension * 2; i += 2)
{
m_BoundingBox[i] = NumericTraits<IndexValueType>::max();
m_BoundingBox[i + 1] = NumericTraits<IndexValueType>::NonpositiveMin();
}
m_Histogram = nullptr;
}
// constructor with histogram enabled
LabelStatistics(int size, RealType lowerBound, RealType upperBound)
{
// initialized to the default values
m_Count = IdentifierType{};
m_Sum = RealType{};
m_SumOfSquares = RealType{};
// Set such that the first pixel encountered can be compared
m_Minimum = NumericTraits<RealType>::max();
m_Maximum = NumericTraits<RealType>::NonpositiveMin();
// Default these to zero
m_Mean = RealType{};
m_Sigma = RealType{};
m_Variance = RealType{};
const unsigned int imageDimension = Self::ImageDimension;
m_BoundingBox.resize(imageDimension * 2);
for (unsigned int i = 0; i < imageDimension * 2; i += 2)
{
m_BoundingBox[i] = NumericTraits<IndexValueType>::max();
m_BoundingBox[i + 1] = NumericTraits<IndexValueType>::NonpositiveMin();
}
// Histogram
m_Histogram = HistogramType::New();
typename HistogramType::SizeType hsize;
typename HistogramType::MeasurementVectorType lb;
typename HistogramType::MeasurementVectorType ub;
hsize.SetSize(1);
lb.SetSize(1);
ub.SetSize(1);
m_Histogram->SetMeasurementVectorSize(1);
hsize[0] = size;
lb[0] = lowerBound;
ub[0] = upperBound;
m_Histogram->Initialize(hsize, lb, ub);
}
// need copy constructor because of smart pointer to histogram
LabelStatistics(const LabelStatistics & l)
{
m_Count = l.m_Count;
m_Minimum = l.m_Minimum;
m_Maximum = l.m_Maximum;
m_Mean = l.m_Mean;
m_Sum = l.m_Sum;
m_SumOfSquares = l.m_SumOfSquares;
m_Sigma = l.m_Sigma;
m_Variance = l.m_Variance;
m_BoundingBox = l.m_BoundingBox;
m_Histogram = l.m_Histogram;
}
LabelStatistics(LabelStatistics &&) = default;
// added for completeness
LabelStatistics &
operator=(const LabelStatistics & l)
{
if (this != &l)
{
m_Count = l.m_Count;
m_Minimum = l.m_Minimum;
m_Maximum = l.m_Maximum;
m_Mean = l.m_Mean;
m_Sum = l.m_Sum;
m_SumOfSquares = l.m_SumOfSquares;
m_Sigma = l.m_Sigma;
m_Variance = l.m_Variance;
m_BoundingBox = l.m_BoundingBox;
m_Histogram = l.m_Histogram;
}
return *this;
}
friend std::ostream &
operator<<(std::ostream & os, const LabelStatistics & labelStatistics)
{
using namespace print_helper;
os << "Count: " << static_cast<typename NumericTraits<IdentifierType>::PrintType>(labelStatistics.m_Count)
<< std::endl;
os << "Minimum: " << static_cast<typename NumericTraits<RealType>::PrintType>(labelStatistics.m_Minimum)
<< std::endl;
os << "Maximum: " << static_cast<typename NumericTraits<RealType>::PrintType>(labelStatistics.m_Maximum)
<< std::endl;
os << "Mean: " << static_cast<typename NumericTraits<RealType>::PrintType>(labelStatistics.m_Mean) << std::endl;
os << "Sum: " << static_cast<typename NumericTraits<RealType>::PrintType>(labelStatistics.m_Sum) << std::endl;
os << "SumOfSquares: " << static_cast<typename NumericTraits<RealType>::PrintType>(labelStatistics.m_SumOfSquares)
<< std::endl;
os << "Sigma: " << static_cast<typename NumericTraits<RealType>::PrintType>(labelStatistics.m_Sigma) << std::endl;
os << "Variance: " << static_cast<typename NumericTraits<RealType>::PrintType>(labelStatistics.m_Variance)
<< std::endl;
os << "BoundingBox: " << labelStatistics.m_BoundingBox << std::endl;
os << "Histogram: ";
if (labelStatistics.m_Histogram)
{
labelStatistics.m_Histogram->Print(os);
}
else
{
os << "nullptr" << std::endl;
}
return os;
}
IdentifierType m_Count;
RealType m_Minimum;
RealType m_Maximum;
RealType m_Mean;
RealType m_Sum;
RealType m_SumOfSquares;
RealType m_Sigma;
RealType m_Variance;
BoundingBoxType m_BoundingBox;
typename HistogramType::Pointer m_Histogram;
};
/** Type of the map used to store data per label */
using MapType = std::unordered_map<LabelPixelType, LabelStatistics>;
using MapIterator = typename MapType::iterator;
using MapConstIterator = typename MapType::const_iterator;
using MapSizeType = IdentifierType;
/** Type of the container used to store valid label values */
using ValidLabelValuesContainerType = std::vector<LabelPixelType>;
// macros for Histogram enables
itkSetMacro(UseHistograms, bool);
itkGetConstMacro(UseHistograms, bool);
itkBooleanMacro(UseHistograms);
virtual const ValidLabelValuesContainerType &
GetValidLabelValues() const
{
return m_ValidLabelValues;
}
/** Set the label image */
itkSetInputMacro(LabelInput, TLabelImage);
itkGetInputMacro(LabelInput, TLabelImage);
/** Does the specified label exist? Can only be called after a call
* a call to Update(). */
bool
HasLabel(LabelPixelType label) const
{
return m_LabelStatistics.find(label) != m_LabelStatistics.end();
}
/** Get the number of labels used */
MapSizeType
GetNumberOfObjects() const
{
return static_cast<MapSizeType>(m_LabelStatistics.size());
}
MapSizeType
GetNumberOfLabels() const
{
return static_cast<MapSizeType>(this->GetNumberOfObjects());
}
/** Return the computed Minimum for a label. */
RealType
GetMinimum(LabelPixelType label) const;
/** Return the computed Maximum for a label. */
RealType
GetMaximum(LabelPixelType label) const;
/** Return the computed Mean for a label. */
RealType
GetMean(LabelPixelType label) const;
/** Return the computed Median for a label. Requires histograms to be enabled!
*/
RealType
GetMedian(LabelPixelType label) const;
/** Return the computed Standard Deviation for a label. */
RealType
GetSigma(LabelPixelType label) const;
/** Return the computed Variance for a label. */
RealType
GetVariance(LabelPixelType label) const;
/** Return the computed bounding box for a label. A vector of
* minIndex, maxIndex pairs for each axis. The intervals include
* the endpoints.*/
BoundingBoxType
GetBoundingBox(LabelPixelType label) const;
/** Return the computed region. */
RegionType
GetRegion(LabelPixelType label) const;
/** Return the compute Sum for a label. */
RealType
GetSum(LabelPixelType label) const;
/** Return the number of pixels for a label. */
MapSizeType
GetCount(LabelPixelType label) const;
/** Return the histogram for a label */
HistogramPointer
GetHistogram(LabelPixelType label) const;
/** specify Histogram parameters */
void
SetHistogramParameters(const int numBins, RealType lowerBound, RealType upperBound);
// Change the access from protected to public to expose streaming option, a using statement can not be used due to
// limitations of wrapping.
void
SetNumberOfStreamDivisions(const unsigned int n) override
{
Superclass::SetNumberOfStreamDivisions(n);
}
unsigned int
GetNumberOfStreamDivisions() const override
{
return Superclass::GetNumberOfStreamDivisions();
}
#ifdef ITK_USE_CONCEPT_CHECKING
// Begin concept checking
itkConceptMacro(InputHasNumericTraitsCheck, (Concept::HasNumericTraits<PixelType>));
// End concept checking
#endif
protected:
LabelStatisticsImageFilter();
~LabelStatisticsImageFilter() override = default;
void
PrintSelf(std::ostream & os, Indent indent) const override;
void
BeforeStreamedGenerateData() override
{
this->AllocateOutputs();
m_LabelStatistics.clear();
}
/** Do final mean and variance computation from data accumulated in threads.
*/
void
AfterStreamedGenerateData() override;
void
ThreadedStreamedGenerateData(const RegionType &) override;
private:
void
MergeMap(MapType &, MapType &) const;
MapType m_LabelStatistics{};
ValidLabelValuesContainerType m_ValidLabelValues{};
bool m_UseHistograms{};
typename HistogramType::SizeType m_NumBins{};
RealType m_LowerBound{};
RealType m_UpperBound{};
std::mutex m_Mutex{};
}; // end of class
} // end namespace itk
#ifndef ITK_MANUAL_INSTANTIATION
# include "itkLabelStatisticsImageFilter.hxx"
#endif
#endif
|