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
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: ImageHistogram1.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 compute the histogram of a scalar image. Since
// the statistics framework classes operate on Samples and
// ListOfSamples, we need to introduce a class that will make the image look
// like a list of samples. This class is the
// \subdoxygen{Statistics}{ScalarImageToListAdaptor}. Once we have connected
// this adaptor to an image, we can proceed to use the
// \subdoxygen{Statistics}{ListSampleToHistogramGenerator} in order to compute
// the histogram of the image.
//
// First, we need to include the headers for the
// \subdoxygen{Statistics}{ScalarImageToListAdaptor} and the \doxygen{Image} classes.
//
// \index{itk::Statistics::Scalar\-Image\-To\-List\-Adaptor!header}
// \index{Statistics!Images}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
#include "itkScalarImageToListAdaptor.h"
#include "itkImage.h"
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Now we include the headers for the \code{ListSampleToHistogramGenerator} and
// the reader that we will use for reading the image from a file.
//
// \index{itk::Statistics::List\-Sample\-To\-Histogram\-Generator!header}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
#include "itkImageFileReader.h"
#include "itkListSampleToHistogramGenerator.h"
// Software Guide : EndCodeSnippet
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 -1;
}
// 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
// Software Guide : BeginLatex
//
// Using the same image type we instantiate the type of the image reader that
// will provide the image source for our example.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef itk::ImageFileReader< ImageType > ReaderType;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName( argv[1] );
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Now we introduce the central piece of this example, which is the use of the
// adaptor that will present the \doxygen{Image} as if it was a list of
// samples. We instantiate the type of the adaptor by using the actual image
// type. Then construct the adaptor by invoking its \code{New()} method and
// assigning the result to the corresponding smart pointer. Finally we connect
// the output of the image reader to the input of the adaptor.
//
// \index{itk::Statistics::Scalar\-Image\-To\-List\-Adaptor!instantiation}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef itk::Statistics::ScalarImageToListAdaptor< ImageType > AdaptorType;
AdaptorType::Pointer adaptor = AdaptorType::New();
adaptor->SetImage( reader->GetOutput() );
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// You must keep in mind that adaptors are not pipeline objects. This means
// that they do not propagate update calls. It is therefore your responsibility
// to make sure that you invoke the \code{Update()} method of the reader before
// you attempt to use the output of the adaptor. As usual, this must be done
// inside a try/catch block because the read operation can potentially throw
// exceptions.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
try
{
reader->Update();
}
catch( itk::ExceptionObject & excp )
{
std::cerr << "Problem reading image file : " << argv[1] << std::endl;
std::cerr << excp << std::endl;
return -1;
}
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// At this point, we are ready for instantiating the type of the histogram
// generator. Note that the adaptor type is used as template parameter of the
// generator. Having instantiated this type, we proceed to create one generator
// by invoking its \code{New()} method.
//
// \index{itk::Statistics::List\-Sample\-To\-Histogram\-Generator!instantiation}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef PixelType HistogramMeasurementType;
typedef itk::Statistics::ListSampleToHistogramGenerator<
AdaptorType,
HistogramMeasurementType
> GeneratorType;
GeneratorType::Pointer generator = GeneratorType::New();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We define now the characteristics of the Histogram that we want to compute.
// This typically includes the size of each one of the component, but given
// that in this simple example we are dealing with a scalar image, then our
// histogram will have a single component. For the sake of generality, however,
// we use the \code{HistogramType} as defined inside of the Generator type. We
// define also the marginal scale factor that will control the precision used
// when assigning values to histogram bins. Finally we invoke the
// \code{Update()} method in the generator.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef GeneratorType::HistogramType HistogramType;
HistogramType::SizeType size;
size.Fill( 256 );
generator->SetListSample( adaptor );
generator->SetNumberOfBins( size );
generator->SetMarginalScale( 10.0 );
HistogramType::MeasurementVectorType min;
HistogramType::MeasurementVectorType max;
min.Fill( -0.5 );
max.Fill( 255.5 );
generator->SetHistogramMin( min );
generator->SetHistogramMax( max );
generator->Update();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Now we are ready for using the image histogram for any further processing.
// The histogram is obtained from the generator by invoking the
// \code{GetOutput()} method.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
HistogramType::ConstPointer histogram = generator->GetOutput();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// In this current example we simply print out the frequency values of all the
// bins in the image histogram.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
const unsigned int histogramSize = histogram->Size();
std::cout << "Histogram size " << histogramSize << std::endl;
for( unsigned int bin=0; bin < histogramSize; bin++ )
{
std::cout << "bin = " << bin << " frequency = ";
std::cout << histogram->GetFrequency( bin, 0 ) <<std::endl;
}
// Software Guide : EndCodeSnippet
return 0;
}
|