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
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: Histogram.cxx
Language: C++
Date: $Date$
Version: $Revision$
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.
=========================================================================*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
// Software Guide : BeginLatex
//
// This example shows how to create an \subdoxygen{Statistics}{Histogram}
// object and use it.
//
// \index{itk::Sample!Histogram}
//
// We call an instance in a \code{Histogram} object a \emph{bin}. The
// Histogram differs from the
// \subdoxygen{Statistics}{ListSample},
// \subdoxygen{Statistics}{ImageToListAdaptor}, or
// \subdoxygen{Statistics}{PointSetToListAdaptor} in significant ways.
// Histogram can have a variable number of values (\code{float}
// type) for each measurement vector, while the three other classes
// have a fixed value (one) for all measurement vectors. Also
// those array-type containers can have multiple instances (data
// elements) that have identical measurement vector values. However,
// in a Histogram object, there is one unique instance for any
// given measurement vector.
//
// \begin{figure}
// \centering
// \includegraphics[width=0.4\textwidth]{Histogram.eps}
// \itkcaption[Histogram]{Conceptual histogram data structure.}
// \protect\label{fig:StatHistogram}
// \end{figure}
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
#include "itkHistogram.h"
// Software Guide : EndCodeSnippet
int main()
{
// Software Guide : BeginLatex
//
// Here we create a histogram with 2-component measurement
// vectors.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef float MeasurementType ;
typedef itk::Statistics::Histogram< MeasurementType, 2 > HistogramType ;
HistogramType::Pointer histogram = HistogramType::New() ;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We initialize it as a $3\times3$ histogram with equal size intervals.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
HistogramType::SizeType size ;
size.Fill(3) ;
HistogramType::MeasurementVectorType lowerBound ;
HistogramType::MeasurementVectorType upperBound ;
lowerBound[0] = 1.1 ;
lowerBound[1] = 2.6 ;
upperBound[0] = 7.1 ;
upperBound[1] = 8.6 ;
histogram->Initialize(size, lowerBound, upperBound ) ;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Now the histogram is ready for storing frequency values. We will
// fill the each bin's frequency according to the Figure
// \ref{fig:StatHistogram}. There are three ways of accessing data
// elements in the histogram:
// \begin{itemize}
// \item using instance identifiers---just like any other Sample object;
// \item using n-dimensional indices---just like an Image object;
// \item using an iterator---just like any other Sample object.
// \end{itemize}
// In this example, the index $(0, 0)$ refers the same bin as the instance
// identifier (0) refers to. The instance identifier of the index (0,
// 1) is (3), (0, 2) is (6), (2, 2) is (8), and so on.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
histogram->SetFrequency(0UL, 0.0) ;
histogram->SetFrequency(1UL, 2.0) ;
histogram->SetFrequency(2UL, 3.0) ;
histogram->SetFrequency(3UL, 2.0) ;
histogram->SetFrequency(4UL, 0.5) ;
histogram->SetFrequency(5UL, 1.0) ;
histogram->SetFrequency(6UL, 5.0) ;
histogram->SetFrequency(7UL, 2.5) ;
histogram->SetFrequency(8UL, 0.0) ;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Let us examine if the frequency is set correctly by calling the
// \code{GetFrequency(index)} method. We can use the
// \code{GetFrequency(instance identifier)} method for the same purpose.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
HistogramType::IndexType index ;
index[0] = 0 ;
index[1] = 2 ;
std::cout << "Frequency of the bin at index " << index
<< " is " << histogram->GetFrequency(index)
<< ", and the bin's instance identifier is "
<< histogram->GetInstanceIdentifier(index) << std::endl ;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// For test purposes, we create a measurement vector and an index
// that belongs to the center bin.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
HistogramType::MeasurementVectorType mv ;
mv[0] = 4.1 ;
mv[1] = 5.6 ;
index.Fill(1) ;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We retrieve the measurement vector at the index value (1, 1), the center
// bin's measurement vector. The output is [4.1, 5.6].
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
std::cout << "Measurement vector at the center bin is "
<< histogram->GetMeasurementVector(index) << std::endl ;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Since all the measurement vectors are unique in the Histogram class, we
// can determine the index from a measurement vector.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
HistogramType::IndexType resultingIndex;
histogram->GetIndex(mv,resultingIndex);
std::cout << "Index of the measurement vector " << mv
<< " is " << resultingIndex << std::endl ;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// In a similar way, we can get the instance identifier from the index.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
std::cout << "Instance identifier of index " << index
<< " is " << histogram->GetInstanceIdentifier(index)
<< std::endl ;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// If we want to check if an index is a valid one, we use the method
// \code{IsIndexOutOfBounds(index)}. The following code snippet fills the
// index variable with (100, 100). It is obviously not a valid index.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
index.Fill(100) ;
if ( histogram->IsIndexOutOfBounds(index) )
{
std::cout << "Index " << index << "is out of bounds." << std::endl ;
}
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The following code snippets show how to get the histogram size
// and frequency dimension.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
std::cout << "Number of bins = " << histogram->Size()
<< " Total frequency = " << histogram->GetTotalFrequency()
<< " Dimension sizes = " << histogram->GetSize() << std::endl ;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The Histogram class has a quantile calculation method,
// \code{Quantile(dimension, percent)}. The following code returns the 50th
// percentile along the first dimension. Note that the quantile calculation
// considers only one dimension.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
std::cout << "50th percentile along the first dimension = "
<< histogram->Quantile(0, 0.5) << std::endl ;
// Software Guide : EndCodeSnippet
return 0 ;
}
|