ITK/Examples/WishList/Segmentation/OtsuMultipleThresholdsCalculator: Difference between revisions
Daviddoria (talk | contribs) No edit summary |
No edit summary |
||
Line 186: | Line 186: | ||
{{ITKCMakeLists| | {{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">
- 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;
} </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.