ITK/Examples/WishList/Segmentation/OtsuMultipleThresholdsCalculator: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
(Created page with "== OtsuMultipleThresholdsCalculator.cxx == <source lang="cpp"> #include "itkImage.h" #include "itkImageFileWriter.h" #include "itkOtsuThresholdImageFilter.h" #include "itkImageRe...")
 
No edit summary
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
== OtsuMultipleThresholdsCalculator.cxx ==
/*
Just a script to make one image out of otsu's multiple threshold scheme. Say, if one defines two threshold
values, then there will be an image with three values: value 1 for (min value of the image < first threshold),( first threshold <  value 2. < second threshold) and (second threshold < value 3 < max value of the image).
Just change the imagetype for different images.
 
 
example:
 
OtsuMultipleThresholdCalculator.exe inputimagename.png outputimagename .png 128 2
 
*/
 
 
 
 
<source lang="cpp">
<source lang="cpp">
#include "itkOtsuMultipleThresholdsCalculator.h"
#include "itkImage.h"
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkImageFileWriter.h"
#include "itkOtsuThresholdImageFilter.h"
#include "itkScalarImageToHistogramGenerator.h"
#include "itkImageRegionIterator.h"
#include "itkBinaryThresholdImageFilter.h"
#include "itkNumericTraits.h"
#include <itkAddImageFilter.h>
#include <stdio.h>
#include <math.h>
 
typedef unsigned char  PixelType;
typedef itk::Image<PixelType, 2>  ImageType;
void CreateImage(ImageType::Pointer image);
int main( int argc, char * argv[])
{
  if( argc < 6)
    {
    std::cerr << "Usage: " << argv[0];
    std::cerr << " inputImageFile outputImageFileBase "; 
    std::cerr << "  outputImageFileExtension numberOfhistogrambins " ;
std::cerr << " numberOfThresholdsToCalculate "<< std::endl; 
    return EXIT_FAILURE;
    }


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


void CreateImage(ImageType::Pointer image);
  ImageType::Pointer image = reader -> GetOutput();


int main(int, char *[])
{
  typedef itk::Image<float, 2> ImageType;
   ImageType::Pointer sumimage = ImageType::New();
   ImageType::Pointer sumimage = ImageType::New();
  ImageType::Pointer sumimage1 = ImageType::New();
  ImageType::SizeType size;


  //THIS WORKS
   size[0]  = src_img_in.GetDimensionSize( 0 );
   ImageType::RegionType region = image->GetLargestPossibleRegion();
   size[1]  = src_img_in.GetDimensionSize( 1 );
  //
   ImageType::SizeType size = region.GetSize();
   ImageType::IndexType start;
   ImageType::IndexType start;
   start.Fill(0);
   start.Fill(0);


  ImageType::RegionType region;
   region.SetIndex( start );
   region.SetIndex( start );
   region.SetSize( size );
   region.SetSize( size );
 
   sumimage->SetLargestPossibleRegion( region );
   sumimage->SetRegions(region);
  sumimage->SetBufferedRegion( region );
  sumimage->SetRequestedRegion( region );
   sumimage->Allocate();
   sumimage->Allocate();
  sumimage->FillBuffer(0) ;


 
   sumimage1->SetLargestPossibleRegion( region );
   typedef itk::ImageFileWriter< ImageType > WriterType;  
  sumimage1->SetBufferedRegion( region );
  sumimage1->SetRequestedRegion( region );
  sumimage1->Allocate();
   typedef itk::Statistics::ScalarImageToHistogramGenerator<ImageType >
   typedef itk::Statistics::ScalarImageToHistogramGenerator<ImageType >
   ScalarImageToHistogramGeneratorType;
   ScalarImageToHistogramGeneratorType;
Line 47: Line 78:
   typedef itk::AddImageFilter< ImageType, ImageType ,ImageType>
   typedef itk::AddImageFilter< ImageType, ImageType ,ImageType>
   AddFilterType;
   AddFilterType;
 
   ScalarImageToHistogramGeneratorType::Pointer
   ScalarImageToHistogramGeneratorType::Pointer
   scalarImageToHistogramGenerator =
   scalarImageToHistogramGenerator =
Line 54: Line 85:
   FilterType::Pointer filter = FilterType::New();
   FilterType::Pointer filter = FilterType::New();
   AddFilterType::Pointer addFilter = AddFilterType::New();
   AddFilterType::Pointer addFilter = AddFilterType::New();
 
 


   scalarImageToHistogramGenerator->SetNumberOfBins(n_bins);
   WriterType::Pointer writer = WriterType::New();
  calculator->SetNumberOfThresholds(n_trsh);
  //THIS WORKS
  if(flag) scalarImageToHistogramGenerator->SetInput(dmtoitk2(src_img_in));
  else scalarImageToHistogramGenerator->SetInput(dmtoitk2(src_img));
  //


  scalarImageToHistogramGenerator->SetNumberOfBins( atoi( argv[4] ) );
  calculator->SetNumberOfThresholds(atoi( argv[5] ) );
  scalarImageToHistogramGenerator->SetInput(image);
  scalarImageToHistogramGenerator->Compute();
   calculator->SetInputHistogram(scalarImageToHistogramGenerator->GetOutput());
   calculator->SetInputHistogram(scalarImageToHistogramGenerator->GetOutput());
   filter->SetInput(dmtoitk2(src_img_in) );
   filter->SetInput(image );


  scalarImageToHistogramGenerator->Compute();
   calculator->Update();
   calculator->Update();
 
   const CalculatorType::OutputType &thresholdVector =
   const CalculatorType::OutputType &thresholdVector =
   calculator->GetOutput();
   calculator->GetOutput();
   CalculatorType::OutputType::const_iterator itNum =
   CalculatorType::OutputType::const_iterator itNum =
   thresholdVector.begin();
   thresholdVector.begin();
  PixelType min = 0;
  PixelType max = 255;
  const PixelType outsideValue = 0;
  std::string outputFileBase = argv[2];
  std::string outputFile;
  std::string format = argv[2];


   //THIS WORKS
   char outputFilename[1000];
  DM::ImageCalculateMinMax( src_img_in,1, 1, &min, &max );
   outputFile = outputFileBase;
   //
   outputFile += argv[3]// filename extension
  const float outsideValue = 0;
 
 
   float lowerThreshold = min;
   float upperThreshold;
 
  filter->SetOutsideValue( outsideValue );


  PixelType lowerThreshold = min;
  PixelType upperThreshold;
  filter->SetOutsideValue(outsideValue );
  PixelType step =floor(255/(atoi( argv[5] )+1) );
  PixelType count=step;
   for(; itNum < thresholdVector.end(); itNum++)
   for(; itNum < thresholdVector.end(); itNum++)
   {
   {


   const float insideValue = count;
   const PixelType insideValue = count;
   filter->SetInsideValue( insideValue );
   filter->SetInsideValue( insideValue );
 
   upperThreshold = static_cast<float>(*itNum);
   upperThreshold =static_cast<PixelType>(*itNum);
 
 
   filter->SetLowerThreshold( lowerThreshold );
   filter->SetLowerThreshold( lowerThreshold );
   filter->SetUpperThreshold( upperThreshold );
   filter->SetUpperThreshold(upperThreshold );
   filter->Update();
   filter->Update();
 
   lowerThreshold = upperThreshold;
   lowerThreshold = upperThreshold;
 
   addFilter->SetInput1( filter->GetOutput() );
   addFilter->SetInput1( filter->GetOutput() );
   addFilter->SetInput2(  sumimage );
   addFilter->SetInput2(  sumimage );
  //sumimage = addFilter->GetOutput( );
   addFilter->Update();
   addFilter->Update();
   sumimage1 = addFilter->GetOutput() ;
   sumimage = addFilter->GetOutput() ;
  sumimage = sumimage1;
 
   count++;
   count = count + step;
   }
   }
   const float insideValue = count;
 
   PixelType insideValue = 255;
 
   filter->SetInsideValue( count  );
   filter->SetInsideValue( insideValue  );
   filter->SetLowerThreshold( lowerThreshold );
   filter->SetLowerThreshold( lowerThreshold );
   filter->SetUpperThreshold( max );
   filter->SetUpperThreshold(max );
   filter->Update();
   filter->Update();
 
 
   addFilter->SetInput1( filter->GetOutput() );
   addFilter->SetInput1( filter->GetOutput() );
   addFilter->SetInput2(  sumimage );
   addFilter->SetInput2(  sumimage );
  //sumimage = addFilter->GetOutput( );
   addFilter->Update();
   addFilter->Update();
   sumimage1 = addFilter->GetOutput() ;
    
   //THIS WORKS
  sumimage = addFilter->GetOutput() ;
   l_deriv_img_out = itktodm2( sumimage1);
   writer->SetInput(sumimage );
  l_deriv_img_out.SetName(( std::string("Segmentated ") + name ).c_str());
 
   writer->SetFileName(  outputFile.c_str());
   
    try
      {
      writer->Update();
      }
    catch( itk::ExceptionObject & excp )
      {
      std::cerr << "Exception thrown " << excp << std::endl;
      }
    // Software Guide : BeginCodeSnippet
   return EXIT_SUCCESS;
   return EXIT_SUCCESS;
}
}
</source>


void CreateImage(ImageType::Pointer image)
{
  // Create an image
  ImageType::IndexType start;
  start.Fill(0);
  ImageType::SizeType size;
  size.Fill(100);
  ImageType::RegionType region;
  region.SetSize(size);
  region.SetIndex(start);


  image->SetRegions(region);
  image->Allocate();
  // Make the whole image white
  itk::ImageRegionIterator<ImageType> iterator(image,image->GetLargestPossibleRegion());
  /*
  //Create a square
  while(!iterator.IsAtEnd())
    {
    iterator.Set(255);
    ++iterator;
    }
  */
}
</source>


{{ITKCMakeLists|OtsuMultipleThresholdsCalculator}}
{{ITKCMakeLists|{{SUBPAGENAME}}}}

Latest revision as of 13:08, 25 December 2012

/* Just a script to make one image out of otsu's multiple threshold scheme. Say, if one defines two threshold values, then there will be an image with three values: value 1 for (min value of the image < first threshold),( first threshold < value 2. < second threshold) and (second threshold < value 3 < max value of the image). Just change the imagetype for different images.


example:

OtsuMultipleThresholdCalculator.exe inputimagename.png outputimagename .png 128 2

  • /



<source lang="cpp">

  1. include "itkOtsuMultipleThresholdsCalculator.h"
  2. include "itkImage.h"
  3. include "itkImageFileReader.h"
  4. include "itkImageFileWriter.h"
  5. include "itkScalarImageToHistogramGenerator.h"
  6. include "itkBinaryThresholdImageFilter.h"
  7. include "itkNumericTraits.h"
  8. include <itkAddImageFilter.h>
  9. include <stdio.h>
  10. include <math.h>

typedef unsigned char PixelType; typedef itk::Image<PixelType, 2> ImageType;

void CreateImage(ImageType::Pointer image);

int main( int argc, char * argv[]) {

  if( argc < 6)
   {
   std::cerr << "Usage: " << argv[0];
   std::cerr << " inputImageFile outputImageFileBase ";  
   std::cerr << "  outputImageFileExtension numberOfhistogrambins " ;

std::cerr << " numberOfThresholdsToCalculate "<< std::endl;

   return EXIT_FAILURE;
   }
 typedef itk::ImageFileReader< ImageType >  ReaderType;
 ReaderType::Pointer reader = ReaderType::New();
 reader->SetFileName( argv[1] );
 reader->Update();
 ImageType::Pointer image = reader -> GetOutput();
 ImageType::Pointer sumimage = ImageType::New();


 ImageType::RegionType region = image->GetLargestPossibleRegion();

 ImageType::SizeType size = region.GetSize();

 ImageType::IndexType start;
 start.Fill(0);
 region.SetIndex( start );
 region.SetSize( size );

 sumimage->SetRegions(region);
 sumimage->Allocate();
 sumimage->FillBuffer(0) ;


 typedef itk::ImageFileWriter< ImageType >  WriterType; 
 typedef itk::Statistics::ScalarImageToHistogramGenerator<ImageType >
 ScalarImageToHistogramGeneratorType;
 typedef
 itk::OtsuMultipleThresholdsCalculator<ScalarImageToHistogramGeneratorType::HistogramType
 > CalculatorType;
 typedef itk::BinaryThresholdImageFilter< ImageType, ImageType >
 FilterType;
 typedef itk::AddImageFilter< ImageType, ImageType ,ImageType>
 AddFilterType;

 ScalarImageToHistogramGeneratorType::Pointer
 scalarImageToHistogramGenerator =
 ScalarImageToHistogramGeneratorType::New();
 CalculatorType::Pointer calculator = CalculatorType::New();
 FilterType::Pointer filter = FilterType::New();
 AddFilterType::Pointer addFilter = AddFilterType::New();
 
 
 WriterType::Pointer writer = WriterType::New();
 scalarImageToHistogramGenerator->SetNumberOfBins( atoi( argv[4] ) );
 calculator->SetNumberOfThresholds(atoi( argv[5] ) );

 scalarImageToHistogramGenerator->SetInput(image);
 scalarImageToHistogramGenerator->Compute();
 calculator->SetInputHistogram(scalarImageToHistogramGenerator->GetOutput());
 filter->SetInput(image );


 calculator->Update();

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

 PixelType min = 0;
 PixelType max = 255;

 const PixelType outsideValue = 0;

 std::string outputFileBase = argv[2];
 std::string outputFile;
 std::string format = argv[2];
 char outputFilename[1000];
 outputFile = outputFileBase;
 outputFile += argv[3];   // filename extension
 PixelType lowerThreshold = min;
 PixelType upperThreshold;

 filter->SetOutsideValue(outsideValue );
 PixelType step =floor(255/(atoi( argv[5] )+1) );
 PixelType count=step;

 for(; itNum < thresholdVector.end(); itNum++)
 {
 const PixelType insideValue = count;
 filter->SetInsideValue( insideValue );

 upperThreshold =static_cast<PixelType>(*itNum);
 
 filter->SetLowerThreshold( lowerThreshold );
 filter->SetUpperThreshold(upperThreshold );
 filter->Update();

 lowerThreshold = upperThreshold;

 addFilter->SetInput1( filter->GetOutput() );
 addFilter->SetInput2(  sumimage );

 addFilter->Update();
 sumimage = addFilter->GetOutput() ;


 count = count + step;
 }

 PixelType insideValue = 255;


 filter->SetInsideValue( insideValue   );
 filter->SetLowerThreshold( lowerThreshold );
 filter->SetUpperThreshold(max );
 filter->Update();


 addFilter->SetInput1( filter->GetOutput() );
 addFilter->SetInput2(  sumimage );

 addFilter->Update();
 
 sumimage = addFilter->GetOutput() ;
 writer->SetInput(sumimage );

 writer->SetFileName(  outputFile.c_str());
   
   try
     { 
     writer->Update(); 
     }
   catch( itk::ExceptionObject & excp )
     {
     std::cerr << "Exception thrown " << excp << std::endl;
     }
   // Software Guide : BeginCodeSnippet


 return EXIT_SUCCESS;

} </source>


CMakeLists.txt

<syntaxhighlight lang="cmake"> cmake_minimum_required(VERSION 3.9.5)

project(OtsuMultipleThresholdsCalculator)

find_package(ITK REQUIRED) include(${ITK_USE_FILE}) if (ITKVtkGlue_LOADED)

 find_package(VTK REQUIRED)
 include(${VTK_USE_FILE})

endif()

add_executable(OtsuMultipleThresholdsCalculator MACOSX_BUNDLE OtsuMultipleThresholdsCalculator.cxx)

if( "${ITK_VERSION_MAJOR}" LESS 4 )

 target_link_libraries(OtsuMultipleThresholdsCalculator ITKReview ${ITK_LIBRARIES})

else( "${ITK_VERSION_MAJOR}" LESS 4 )

 target_link_libraries(OtsuMultipleThresholdsCalculator ${ITK_LIBRARIES})

endif( "${ITK_VERSION_MAJOR}" LESS 4 )

</syntaxhighlight>

Download and Build OtsuMultipleThresholdsCalculator

Click here to download OtsuMultipleThresholdsCalculator and its CMakeLists.txt file. Once the tarball OtsuMultipleThresholdsCalculator.tar has been downloaded and extracted,

cd OtsuMultipleThresholdsCalculator/build
  • If ITK is installed:
cmake ..
  • If ITK is not installed but compiled on your system, you will need to specify the path to your ITK build:
cmake -DITK_DIR:PATH=/home/me/itk_build ..

Build the project:

make

and run it:

./OtsuMultipleThresholdsCalculator

WINDOWS USERS PLEASE NOTE: Be sure to add the ITK bin directory to your path. This will resolve the ITK dll's at run time.

Building All of the Examples

Many of the examples in the ITK Wiki Examples Collection require VTK. You can build all of the the examples by following these instructions. If you are a new VTK user, you may want to try the Superbuild which will build a proper ITK and VTK.

ItkVtkGlue

ITK >= 4

For examples that use QuickView (which depends on VTK), you must have built ITK with Module_ITKVtkGlue=ON.

ITK < 4

Some of the ITK Examples require VTK to display the images. If you download the entire ITK Wiki Examples Collection, the ItkVtkGlue directory will be included and configured. If you wish to just build a few examples, then you will need to download ItkVtkGlue and build it. When you run cmake it will ask you to specify the location of the ItkVtkGlue binary directory.