[Insight-users] Histogram

Luis Ibanez luis . ibanez at kitware . com
Mon, 01 Dec 2003 09:42:08 -0500


Hi David,

The code below will compute the histogram
of an 8bits/pixel.

This code example has been commited under

   Insight/Examples/Statistics/ImageHistogram1.cxx


Please let us know if you find any problems,


Thanks


   Luis

------------- Code Begin ---------------------

#include "itkScalarImageToListAdaptor.h"
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkListSampleToHistogramGenerator.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 -1;
     }

   typedef unsigned char       PixelType;
   const unsigned int          Dimension = 2;

   typedef itk::Image<PixelType, Dimension > ImageType;
   typedef itk::ImageFileReader< ImageType > ReaderType;
   ReaderType::Pointer reader = ReaderType::New();\
   reader->SetFileName( argv[1] );
   reader->Update();

   typedef itk::Statistics::ScalarImageToListAdaptor<
                                    ImageType >   AdaptorType;

   AdaptorType::Pointer adaptor = AdaptorType::New();

   adaptor->SetImage(  reader->GetOutput() );

   typedef PixelType        HistogramMeasurementType;

   typedef itk::Statistics::ListSampleToHistogramGenerator<
                                             AdaptorType,
                                             HistogramMeasurementType
                                                  > GeneratorType;

   GeneratorType::Pointer generator = GeneratorType::New();

   typedef GeneratorType::HistogramType  HistogramType;

   HistogramType::SizeType size;
   size.Fill( 255 );

   generator->SetListSample( adaptor );
   generator->SetNumberOfBins( size );
   generator->SetMarginalScale( 10.0 );
   generator->Update();

   HistogramType::Pointer histogram = generator->GetOutput();

   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;
     }

   return 0;


---------------- Code End ------------------------------


--------------------------
David Llanos wrote:
> hi Luis,
> 
> I want to get three separate histograms,one for each one of the three RGB
> components...
> 
> I think the first part is resolved  in the example ImadeAdaptor2.cxx
> 
> correct me if I make a mistake: in the part of the code when I arrive the
> line:
> 
>   adaptor->SetImage (reader->GetOutput ());
> 
>  I have the red component in the pointer "adaptor"
> 
> Now I want to make the histogram of this component, but I don't know to use
> the ImageToList
> adaptor in order to connect the image data to the statistical framework. I
> don't understand
> the explanation of the SoftwareGuide.
> 
> I would thank it to you if you are more specific
> 
> 
> 
> thank you for your attention and a greeting
> 
> 
> 
> 
> 
> ----- Original Message ----- 
> From: "Luis Ibanez" <luis . ibanez at kitware . com>
> To: "David Llanos" <gva02 at elai . upm . es>
> Cc: <insight-users at itk . org>
> Sent: Monday, November 24, 2003 7:41 PM
> Subject: Re: [Insight-users] Histogram
> 
> 
> 
>>Hi David,
>>
>>If you want to get three separate histograms,
>>one for each one of the three RGB components you
>>can use the ImageAdaptors described in the software
>>guide.
>>
>>   http://www . itk . org/ItkSoftwareGuide . pdf
>>
>>in Section 12.3 you will find an example that
>>extracts the R, G, B components from an RGB
>>image and present them as being a single
>>component image.
>>
>>Then you use the ImageToList adaptor in order
>>to connect the image data to the statistical
>>framework.
>>
>>You will find this description in Section 10.2,
>>of the SoftwareGuide. pdf-page 424.
>>
>>-------------------------------------------------
>>
>>If what you want is the Joint histogram of the
>>RGB components, this will be a 3D image.
>>
>>In that case, as a shortcut you could use the small
>>program we used for extracting RGB histograms from the
>>Visible Woman Data set.  This was done for removing
>>the blue gel from the dataset as a preprocessing
>>step for generating the segmentations that are shown
>>in the Cover of the SoftwareGuide.
>>
>>Here is the code from
>>
>>InsightDocuments/SoftwareGuide/Cover/Source
>>
>>
> 
> http://www . itk . org/cgi-bin/cvsweb . cgi/InsightDocuments/SoftwareGuide/Cover/Source/VWHistogramRGB . cxx?cvsroot=Insight
> 
>>
>>For the segmentation of the cover it was quite useful
>>to volume render the 3D histogram since it make visible
>>all the clusters. You can use the free version of VolView
>>for that purpose.
>>
>>VolView can be downloaded from:
>>
>>    http://www . kitware . com/products/volview . html
>>
>>
>>Please let us know if you have further questions,
>>
>>
>>Thanks
>>
>>
>>    Luis
>>
>>
>>-------------------
>>David Llanos wrote:
>>
>>
>>>hi all,
>>>
>>>As I can apply a histogram to a RGB image for each component?
>>>
>>>I have been studying the example histogram.cxx but this it doesn't try
>>>with images.
>>>
>>>does any example exist in the sources that given an image like entrance
>>>returns its histogram?
>>>
>>>
>>>
>>>thanks in advance
>>>
>>
>>
>>
>>
> 
>