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
|
/*=========================================================================
*
* 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.
*
*=========================================================================*/
// Software Guide : BeginLatex
//
// From the previous example you will have noticed that there is a significant
// number of operations to perform to compute the simple histogram of
// a scalar image. Given that this is a relatively common operation, it is
// convenient to encapsulate many of these operations in a single helper class.
//
// The \subdoxygen{Statistics}{ScalarImageToHistogramGenerator} is the result
// of such encapsulation. This example illustrates how to compute the
// histogram of a scalar image using this helper class.
//
// Software Guide : EndLatex
// Software Guide : BeginLatex
//
// We should first include the header of the histogram generator and the image
// class.
//
// \index{itk::Statistics::Scalar\-Image\-To\-Histogram\-Generator!header}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
#include "itkScalarImageToHistogramGenerator.h"
#include "itkImage.h"
// Software Guide : EndCodeSnippet
#include "itkImageFileReader.h"
int main( int argc, char * argv [] )
{
if( argc < 2 )
{
std::cerr << "Missing command line arguments" << std::endl;
std::cerr << "Usage : ImageHistogram1 inputImageFileName " << std::endl;
return EXIT_FAILURE;
}
// Software Guide : BeginLatex
//
// The image type must be defined using the typical pair of pixel type and
// dimension specification.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef unsigned char PixelType;
const unsigned int Dimension = 2;
typedef itk::Image<PixelType, Dimension > ImageType;
// Software Guide : EndCodeSnippet
typedef itk::ImageFileReader< ImageType > ReaderType;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName( argv[1] );
try
{
reader->Update();
}
catch( itk::ExceptionObject & excp )
{
std::cerr << "Problem reading image file : " << argv[1] << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
// Software Guide : BeginLatex
//
// We use now the image type in order to instantiate the type of the
// corresponding histogram generator class, and invoke its \code{New()} method
// in order to construct one.
//
// \index{itk::Statistics::Scalar\-Image\-To\-Histogram\-Generator!header}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef itk::Statistics::ScalarImageToHistogramGenerator<
ImageType > HistogramGeneratorType;
HistogramGeneratorType::Pointer histogramGenerator =
HistogramGeneratorType::New();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The image to be passed as input to the histogram generator is taken in this
// case from the output of an image reader.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
histogramGenerator->SetInput( reader->GetOutput() );
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We define also the typical parameters that specify the characteristics of
// the histogram to be computed.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
histogramGenerator->SetNumberOfBins( 256 );
histogramGenerator->SetMarginalScale( 10.0 );
histogramGenerator->SetHistogramMin( -0.5 );
histogramGenerator->SetHistogramMax( 255.5 );
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Finally we trigger the computation of the histogram by invoking the
// \code{Compute()} method of the generator. Note again, that a generator is
// not a pipeline object and therefore it is up to you to make sure that the
// filters providing the input image have been updated.
//
// \index{itk::Statistics::Scalar\-Image\-To\-Histogram\-Generator!Compute()}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
histogramGenerator->Compute();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The resulting histogram can be obtained from the generator by invoking its
// \code{GetOutput()} method. It is also convenient to get the Histogram type
// from the traits of the generator type itself as shown in the code below.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef HistogramGeneratorType::HistogramType HistogramType;
const HistogramType * histogram = histogramGenerator->GetOutput();
// Software Guide : EndCodeSnippet
const unsigned int histogramSize = histogram->Size();
std::cout << "Histogram size " << histogramSize << std::endl;
unsigned int bin;
for( bin=0; bin < histogramSize; bin++ )
{
std::cout << "bin = " << bin << " frequency = ";
std::cout << histogram->GetFrequency( bin, 0 ) << std::endl;
}
// Software Guide : BeginLatex
//
// In this case we simply print out the frequency values of the histogram.
// These values can be accessed by using iterators.
//
// \index{itk::Statistics::Histogram!Iterators}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
HistogramType::ConstIterator itr = histogram->Begin();
HistogramType::ConstIterator end = histogram->End();
unsigned int binNumber = 0;
while( itr != end )
{
std::cout << "bin = " << binNumber << " frequency = ";
std::cout << itr.GetFrequency() << std::endl;
++itr;
++binNumber;
}
// Software Guide : EndCodeSnippet
return EXIT_SUCCESS;
}
|