ITK 实例1 连接门限对脑部切片PNG图像进行二维分割


1 #include "itkConnectedThresholdImageFilter.h"//连接门限头文件 2 #include "itkImage.h" 3 #include "itkCastImageFilter.h" 4 #include "itkCurvatureFlowImageFilter.h" 5 #include "itkImageFileReader.h" 6 #include "itkImageFileWriter.h" 7 //图像中存在的噪声将大大降低滤波器生长大面积区域的能力。当面对噪声图像时,通常 8 //是使用一个边缘保留平滑滤波器。 9 int main( int argc, char *argv[]) 10 */ 18 /*我们基于一个特殊的像素类型和维来定义图像类型。由于平滑滤波器的需要,在这里我 19 们使用浮点型数据定义像素*/ 20 typedef float InternalPixelType; 21 const unsigned int Dimension = 2; 22 typedef itk::Image< InternalPixelType, Dimension > InternalImageType; 23 24 typedef unsigned char OutputPixelType; 25 typedef itk::Image< OutputPixelType, Dimension > OutputImageType; 26 typedef itk::CastImageFilter< InternalImageType, OutputImageType > 27 CastingFilterType; 28 CastingFilterType::Pointer caster = CastingFilterType::New(); 29 30 //图像读取与图像写类型定义 31 typedef itk::ImageFileReader< InternalImageType > ReaderType; 32 typedef itk::ImageFileWriter< OutputImageType > WriterType; 33 //图像读取与图像写对象实例化 34 ReaderType::Pointer reader = ReaderType::New(); 35 WriterType::Pointer writer = WriterType::New(); 36 37 reader>SetFileName( "BrainProtonDensitySlice.png" ); 38 writer>SetFileName( "BrainProtonDensitySlice_huizhi.png" ); 39 40 //使用图像类型作为模板参数来对平滑滤波器进行实例化 41 typedef itk::CurvatureFlowImageFilter< InternalImageType, InternalImageType > 42 CurvatureFlowImageFilterType; 43 //调用 New() 方式来创建滤波器并将接指向 itk::SmartPointer 44 //平滑滤波器实例化对象smoothing 45 CurvatureFlowImageFilterType::Pointer smoothing = 46 CurvatureFlowImageFilterType::New(); 47 //声明区域生长滤波器的类型,本例中使用 ConnectedThresholdImageFilter 48 typedef itk::ConnectedThresholdImageFilter< InternalImageType, 49 InternalImageType > ConnectedFilterType; 50 //使用 New( ) 方式构造这种类的一个滤波器 51 //连接门限滤波器实例化对象connectedThreshold 52 ConnectedFilterType::Pointer connectedThreshold = ConnectedFilterType::New(); 53 //读取图像进行平滑滤波 54 smoothing>SetInput( reader>GetOutput() ); 55 //滤波后进行连接门限 56 connectedThreshold>SetInput( smoothing>GetOutput() ); 57 /*由于只有一小部分图像文件格式支持浮点型数据类型,所以使 58 用 cast filter 将浮点型数据类型转换成整型*/ 59 caster>SetInput( connectedThreshold>GetOutput() ); 60 //处理后输出数据到writer 61 writer>SetInput( caster>GetOutput() ); 62 /*CurvatureFlowImageFilter(平滑滤波器)需要定义两个参数。下面是一个二维图像的常见值。 当然它们也需要根据输入图像存在的噪声的数量进行适当的调整*/ 64 smoothing>SetNumberOfIterations( 5 ); 65 smoothing>SetTimeStep( 0.125 ); 66 /*ConnectedThresholdImageFilter(连通门限图像滤波)有两个主要的 67 参数(lowerThreshold和upperThreshold)需要定义, 68 它们分别是为了确定是否包含在区域中的亮度值而制定的标准的上门限和下门限。 69 这两个值设定得太接近势必会降低区域生长的机动性,而设定得太远必将整个图像都卷入区域中*/ 70 const InternalPixelType lowerThreshold = atof( "180" ); 71 const InternalPixelType upperThreshold = atof( "210" ); 72 73 connectedThreshold>SetLower( lowerThreshold ); 74 connectedThreshold>SetUpper( upperThreshold ); 75 /*这个滤波器的输出是一个二值图像,这个二值图像除了分割出的区域外到处都是零值像 76 素。区域中的亮度值是由 SetReplaceValue() 方式来选择的*/ 77 connectedThreshold>SetReplaceValue( 255 ); 78 79 InternalImageType::IndexType index; 80 81 /*这个算法的实例化需要用户提供一个种子点index。将这个点选在被分割的解剖学结构的典型 82 区域是很便捷的。种子是以一种 itk::Index 的形式传递给 SetSeed() 方式的*/ 83 index[0] = atoi( "107" ); 84 index[1] = atoi( "69" ); 85 connectedThreshold>SetSeed( index ); 86 /*writer 上的 Updata() 方法引发了管道的运行。通常在出现错误和抛出异议时, 从一个 87 try / catch 模块调用 updata :*/ 88 try 89 92 catch( itk::ExceptionObject & excep ) 93 97 return EXIT_SUCCESS; 98 }



上一篇:ITK 实例10 边缘保留平滑滤波对PNG图像进行二维滤波

下一篇:OpenCV3.2图像分割 实例3:KMeans图像分割


ITK
Copyright © 2002-2019 k262电脑网 www.k262.cn 皖ICP备2020016292号
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!QQ:251442993 热门搜索 网站地图