ITK/Examples/WishList/Segmentation/OtsuMultipleThresholdsCalculator: Difference between revisions
Line 1: | Line 1: | ||
< | /* | ||
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 | |||
*/ | |||
#include "itkOtsuMultipleThresholdsCalculator.h" | #include "itkOtsuMultipleThresholdsCalculator.h" | ||
#include "itkImage.h" | |||
#include "itkImageFileReader.h" | |||
#include "itkImageFileWriter.h" | |||
#include "itkScalarImageToHistogramGenerator.h" | #include "itkScalarImageToHistogramGenerator.h" | ||
#include "itkBinaryThresholdImageFilter.h" | #include "itkBinaryThresholdImageFilter.h" | ||
#include "itkNumericTraits.h" | #include "itkNumericTraits.h" | ||
#include <itkAddImageFilter.h> | #include <itkAddImageFilter.h> | ||
#include <stdio.h> | |||
#include <math.h> | |||
typedef itk::Image< | typedef unsigned char PixelType; | ||
typedef itk::Image<PixelType, 2> ImageType; | |||
void CreateImage(ImageType::Pointer image); | void CreateImage(ImageType::Pointer image); | ||
int main( | 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; | |||
} | |||
ImageType::Pointer | typedef itk::ImageFileReader< ImageType > ReaderType; | ||
ReaderType::Pointer reader = ReaderType::New(); | |||
reader->SetFileName( argv[1] ); | |||
reader->Update(); | |||
ImageType:: | ImageType::Pointer image = reader -> GetOutput(); | ||
ImageType:: | ImageType::Pointer sumimage = ImageType::New(); | ||
ImageType::RegionType region = image->GetLargestPossibleRegion(); | |||
ImageType::SizeType size = region.GetSize(); | ImageType::SizeType size = region.GetSize(); | ||
ImageType::IndexType start; | ImageType::IndexType start; | ||
start.Fill(0); | start.Fill(0); | ||
region.SetIndex( start ); | region.SetIndex( start ); | ||
region.SetSize( size ); | region.SetSize( size ); | ||
sumimage->SetRegions(region); | sumimage->SetRegions(region); | ||
sumimage->Allocate(); | sumimage->Allocate(); | ||
sumimage->FillBuffer(0) ; | sumimage->FillBuffer(0) ; | ||
typedef itk::ImageFileWriter< ImageType > WriterType; | typedef itk::ImageFileWriter< ImageType > WriterType; | ||
typedef itk::Statistics::ScalarImageToHistogramGenerator<ImageType > | typedef itk::Statistics::ScalarImageToHistogramGenerator<ImageType > | ||
Line 44: | Line 79: | ||
typedef itk::AddImageFilter< ImageType, ImageType ,ImageType> | typedef itk::AddImageFilter< ImageType, ImageType ,ImageType> | ||
AddFilterType; | AddFilterType; | ||
ScalarImageToHistogramGeneratorType::Pointer | ScalarImageToHistogramGeneratorType::Pointer | ||
scalarImageToHistogramGenerator = | scalarImageToHistogramGenerator = | ||
Line 51: | Line 86: | ||
FilterType::Pointer filter = FilterType::New(); | FilterType::Pointer filter = FilterType::New(); | ||
AddFilterType::Pointer addFilter = AddFilterType::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()); | calculator->SetInputHistogram(scalarImageToHistogramGenerator->GetOutput()); | ||
filter->SetInput(image | filter->SetInput(image ); | ||
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]; | |||
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++) | for(; itNum < thresholdVector.end(); itNum++) | ||
{ | { | ||
const PixelType insideValue = count; | |||
filter->SetInsideValue( insideValue ); | filter->SetInsideValue( insideValue ); | ||
upperThreshold = static_cast< | 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 ); | ||
addFilter->Update(); | addFilter->Update(); | ||
sumimage = addFilter->GetOutput() ; | sumimage = addFilter->GetOutput() ; | ||
count+ | count = count + step; | ||
} | } | ||
PixelType insideValue = 255; | |||
filter->SetInsideValue( | |||
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 ); | ||
addFilter->Update(); | addFilter->Update(); | ||
addFilter->GetOutput(); | |||
sumimage = addFilter->GetOutput() ; | |||
writer->SetInput( | writer->SetInput(sumimage ); | ||
writer->SetFileName( | 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; | ||
} | } | ||
CMakeLists.txt | |||
{ | cmake_minimum_required(VERSION 2.6) | ||
project(OtsuMultipleThresholdsCalculator) | |||
find_package(ITK REQUIRED) | |||
include(${ITK_USE_FILE}) | |||
add_executable(OtsuMultipleThresholdsCalculator OtsuMultipleThresholdsCalculator.cxx) | |||
TARGET_LINK_LIBRARIES(OtsuMultipleThresholdsCalculator ITK |
Revision as of 22:53, 1 July 2011
/* 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
- /
- include "itkOtsuMultipleThresholdsCalculator.h"
- include "itkImage.h"
- include "itkImageFileReader.h"
- include "itkImageFileWriter.h"
- include "itkScalarImageToHistogramGenerator.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::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;
}
CMakeLists.txt
cmake_minimum_required(VERSION 2.6)
project(OtsuMultipleThresholdsCalculator)
find_package(ITK REQUIRED) include(${ITK_USE_FILE})
add_executable(OtsuMultipleThresholdsCalculator OtsuMultipleThresholdsCalculator.cxx) TARGET_LINK_LIBRARIES(OtsuMultipleThresholdsCalculator ITK