ITK 实例4 OTSU算法对PNG图像进行多阈值二维分割
1 #include "itkOtsuMultipleThresholdsCalculator.h"//包含头文件 2 3 #include "itkImage.h" 4 #include "itkImageFileReader.h" 5 #include "itkImageFileWriter.h" 6 #include "itkScalarImageToHistogramGenerator.h" 7 #include "itkBinaryThresholdImageFilter.h" 8 #include "itkNumericTraits.h" 9 10 #include <ianip> 11 #include <stdio.h> 12 //这个例子阐述了如何使用 itk::OtsuMultipleThresholdsCalculator 13 int main( int argc, char * argv[] ) 14 */ 22 23 //Convenience typedefs 24 typedef unsigned short InputPixelType; 25 typedef unsigned char OutputPixelType; 26 27 typedef itk::Image< InputPixelType, 2 > InputImageType; 28 typedef itk::Image< OutputPixelType, 2 > OutputImageType; 29 30 /*OtsuMultipleThresholdsCalculator 以最大化类之间的变异的原则来计算一个给定的直方 31 图的门限。我们使用 ScalarImageToHistogramGenerator 来得到直方图*/ 32 typedef itk::Statistics::ScalarImageToHistogramGenerator< 33 InputImageType > ScalarImageToHistogramGeneratorType; 34 35 typedef ScalarImageToHistogramGeneratorType::HistogramType HistogramType; 36 37 typedef itk::OtsuMultipleThresholdsCalculator< HistogramType > CalculatorType; 38 39 typedef itk::ImageFileReader< InputImageType > ReaderType; 40 typedef itk::ImageFileWriter< OutputImageType > WriterType; 41 //一旦计算得到门限值,我们将使用 BinaryThresholdImageFilter 来对图像进行分割 42 typedef itk::BinaryThresholdImageFilter< 43 InputImageType, OutputImageType > FilterType; 44 45 ScalarImageToHistogramGeneratorType::Pointer scalarImageToHistogramGenerator 46 = ScalarImageToHistogramGeneratorType::New(); 47 48 CalculatorType::Pointer calculator = CalculatorType::New(); 49 FilterType::Pointer filter = FilterType::New(); 50 51 ReaderType::Pointer reader = ReaderType::New(); 52 WriterType::Pointer writer = WriterType::New(); 53 54 scalarImageToHistogramGenerator>SetNumberOfBins( 128 ); 55 //设置Otsu分割阈值数目 56 calculator>SetNumberOfThresholds( atoi("3") ); 57 58 const OutputPixelType outsideValue = 0; 59 const OutputPixelType insideValue = 255; 60 61 filter>SetOutsideValue( outsideValue ); 62 filter>SetInsideValue( insideValue ); 64 //Connect Pipeline 65 reader>SetFileName( "BrainProtonDensitySlice.png" ); 66 67 //管道如下所示 68 scalarImageToHistogramGenerator>SetInput( reader>GetOutput() ); 69 calculator>SetInputHistogram(scalarImageToHistogramGenerator>GetOutput() ); 70 filter>SetInput( reader>GetOutput() ); 71 writer>SetInput( filter>GetOutput() ); 72 //读取报错异常 73 try 74 77 catch( itk::ExceptionObject & excp ) 78 81 scalarImageToHistogramGenerator>Cpute(); 82 //calculator报错异常 83 try 84 87 catch( itk::ExceptionObject & excp ) 88 91 92 //使用 GetOutput 方式可以得到门限值 93 const CalculatorType::OutputType &thresholdVector = calculator>GetOutput(); 94 95 std::string outputFileBase = "BrainProtonDensitySlice_OtsuMu"; 96 97 InputPixelType lowerThreshold = itk::NumericTraits<InputPixelType>::min(); 98 InputPixelType upperThreshold; 99 100 typedef CalculatorType::OutputType::const_iterator ThresholdItType; 101 102 for( ThresholdItType itNum = thresholdVector.begin(); 103 itNum != thresholdVector.end(); 104 ++itNum ) 105 131 catch( itk::ExceptionObject & excp ) 132 135 } 136 137 upperThreshold = itk::NumericTraits<InputPixelType>::max(); 138 filter>SetLowerThreshold( lowerThreshold ); 139 filter>SetUpperThreshold( upperThreshold ); 140 141 std::ostringstream outputFilename2; 142 outputFilename2 << outputFileBase 143 << std::setfill('0') << std::setw(3) << thresholdVector.size() 144 << "." 145 << "jpg"; 146 writer>SetFileName( outputFilename2.str() ); 147 148 try 149 152 catch( itk::ExceptionObject & excp ) 153 156 157 return EXIT_SUCCESS; 158 }
上一篇:ITK 实例13 ITK分水岭算法对PNG图像进行二维分割
下一篇:OpenCV3.2图像分割 实例6:基于距离变换的分水岭粘连对象分离与计数
ITK
pla文件怎么看,pla文件用什么打开?
pl1文件怎么看,pl1文件用什么打开?
pl文件怎么看,pl文件用什么打开?
pl0文件怎么看,pl0文件用什么打开?
pkt文件怎么看,pkt文件用什么打开?
pkm文件怎么看,pkm文件用什么打开?
pks文件怎么看,pks文件用什么打开?
pka文件怎么看,pka文件用什么打开?
pkh文件怎么看,pkh文件用什么打开?
pkg文件怎么看,pkg文件用什么打开?