[Insight-users] OtsuMultipleThresholdImageFilter

john smith mkitkinsightuser at gmail.com
Fri Apr 8 10:32:10 EDT 2011


Hello,
 I have the followin code from itk-manual to run
OtsuMultipleThresholdImageFilter. What must I type in the command window to
run the project?
Thanks in advance

////////////  code//////////////


#include "itkOtsuMultipleThresholdsCalculator.h"


#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkScalarImageToHistogramGenerator.h"
#include "itkBinaryThresholdImageFilter.h"
#include "itkNumericTraits.h"

#include <stdio.h>
int main( int argc, char * argv[] )
{
  if( argc < 5 )
    {
    std::cerr << "Usage: " << argv[0];
    std::cerr << " inputImageFile outputImageFileBase ";
    std::cerr << "  outputImageFileExtension numberOfThresholdsToCalculate
"  << std::endl;
    return EXIT_FAILURE;
    }


  typedef  unsigned short  InputPixelType;
  typedef  unsigned char   OutputPixelType;

  typedef itk::Image< InputPixelType,  3 >   InputImageType;
  typedef itk::Image< OutputPixelType, 3 >   OutputImageType;


  typedef itk::Statistics::ScalarImageToHistogramGenerator<
  InputImageType >
    ScalarImageToHistogramGeneratorType;

  typedef ScalarImageToHistogramGeneratorType::HistogramType
HistogramType;

  typedef itk::OtsuMultipleThresholdsCalculator< HistogramType >
CalculatorType;


  typedef itk::ImageFileReader< InputImageType >  ReaderType;
  typedef itk::ImageFileWriter< OutputImageType >  WriterType;


  typedef itk::BinaryThresholdImageFilter<
  InputImageType, OutputImageType >  FilterType;

  ScalarImageToHistogramGeneratorType::Pointer
scalarImageToHistogramGenerator =
    ScalarImageToHistogramGeneratorType::New();

  CalculatorType::Pointer calculator = CalculatorType::New();
  FilterType::Pointer filter = FilterType::New();


  ReaderType::Pointer reader = ReaderType::New();
  WriterType::Pointer writer = WriterType::New();


  scalarImageToHistogramGenerator->SetNumberOfBins( 128 );
  calculator->SetNumberOfThresholds( atoi( argv[4] ) );

  const OutputPixelType outsideValue = 0;
  const OutputPixelType insideValue = 255;

  filter->SetOutsideValue( outsideValue );
  filter->SetInsideValue(  insideValue  );


  reader->SetFileName( argv[1] );


  scalarImageToHistogramGenerator->SetInput( reader->GetOutput() );
  calculator->SetInputHistogram(
scalarImageToHistogramGenerator->GetOutput() );
  filter->SetInput( reader->GetOutput() );
  writer->SetInput( filter->GetOutput() );

  try
    {
    reader->Update();
    }
  catch( itk::ExceptionObject & excp )
    {
    std::cerr << "Exception thrown while reading image" << excp <<
std::endl;
    }
  scalarImageToHistogramGenerator->Compute();

  try
    {
    calculator->Update();
    }
  catch( itk::ExceptionObject & excp )
    {
    std::cerr << "Exception thrown " << excp << std::endl;
    }


  const CalculatorType::OutputType &thresholdVector =
calculator->GetOutput();
  CalculatorType::OutputType::const_iterator itNum =
thresholdVector.begin();

  std::string outputFileBase = argv[2];
  std::string outputFile;

  InputPixelType lowerThreshold = 0;
  InputPixelType upperThreshold;

  std::string format = argv[2];

  char outputFilename[1000];
  outputFile = outputFileBase + "%03d.";
  outputFile += argv[3];   // filename extension


  for(; itNum < thresholdVector.end(); itNum++)
    {
    std::cout << "OtsuThreshold["
              << (int)(itNum - thresholdVector.begin())
              << "] = "
              <<
static_cast<itk::NumericTraits<CalculatorType::MeasurementType>::PrintType>(*itNum)

              << std::endl;


    upperThreshold = static_cast<InputPixelType>(*itNum);

    filter->SetLowerThreshold( lowerThreshold );
    filter->SetUpperThreshold( upperThreshold );

    lowerThreshold = upperThreshold;

    sprintf (outputFilename, outputFile.c_str(), (itNum -
thresholdVector.begin()));
    writer->SetFileName( outputFilename );


    try
      {
      writer->Update();
      }
    catch( itk::ExceptionObject & excp )
      {
      std::cerr << "Exception thrown " << excp << std::endl;
      }

    }

  upperThreshold = itk::NumericTraits<InputPixelType>::max();
  filter->SetLowerThreshold( lowerThreshold );
  filter->SetUpperThreshold( upperThreshold );

  sprintf (outputFilename, outputFile.c_str(), (thresholdVector.size() ));
  writer->SetFileName( outputFilename );

  try
    {
    writer->Update();
    }
  catch( itk::ExceptionObject & excp )
    {
    std::cerr << "Exception thrown " << excp << std::endl;
    }


  return EXIT_SUCCESS;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20110408/25b0914f/attachment.htm>


More information about the Insight-users mailing list