一杯清酒邀明月
天下本无事,庸人扰之而烦耳。
  1 #include "itkImage.h"
  2 #include "itkImageFileReader.h"
  3 #include "itkImageFileWriter.h"
  4 //线性映射头文件
  5 #include "itkCastImageFilter.h"
  6 #include "itkRescaleIntensityImageFilter.h"
  7 #include "itkNormalizeImageFilter.h"
  8  
  9 int main( int argc, char * argv[] )
 10 {
 11  /* if( argc < 2 )
 12     {
 13     std::cerr << "Usage: " << std::endl;
 14     std::cerr << argv[0] << "   inputImageFile " << std::endl;
 15     return EXIT_FAILURE;
 16     }*/
 17 //我们定义输入和输出图像的像素类型
 18   typedef   float    InputPixelType;
 19   //typedef   float            OutputPixelType;
 20   typedef  unsigned char  OutputPixelType;
 21   //然后,定义输入和输出图像类型
 22   typedef itk::Image< InputPixelType,  2 >   InputImageType;
 23   typedef itk::Image< OutputPixelType, 2 >   OutputImageType;
 24  
 25   typedef itk::ImageFileReader< InputImageType >  ReaderType;//读文件类型
 26   typedef itk::ImageFileWriter< OutputImageType >  WriterType;//写文件类型
 27   //使用定义的图像类型来对滤波器进行实例化
 28   typedef itk::CastImageFilter<
 29                InputImageType, OutputImageType >  CastFilterType;
 30  
 31   typedef itk::RescaleIntensityImageFilter<
 32                InputImageType, OutputImageType >  RescaleFilterType1;
 33  
 34   typedef itk::ShiftScaleImageFilter<
 35                InputImageType, OutputImageType >  ShiftScaleFilterType;
 36  
 37   typedef itk::NormalizeImageFilter<
 38                InputImageType, OutputImageType >  NormalizeFilterType;
 39  
 40   ReaderType::Pointer reader = ReaderType::New();//实例化对象reader
 41   WriterType::Pointer writer1 = WriterType::New();//实例化对象writer1
 42   WriterType::Pointer writer2 = WriterType::New();//实例化对象writer2
 43   WriterType::Pointer writer3 = WriterType::New();//实例化对象writer3
 44   WriterType::Pointer writer4 = WriterType::New();//实例化对象writer4
 45   //通过调用 New( ) 操作来创建对象滤波器并将结果指向 itk::SmartPointers 
 46   CastFilterType::Pointer       castFilter       = CastFilterType::New();
 47   RescaleFilterType1::Pointer    rescaleFilter    = RescaleFilterType1::New();
 48   ShiftScaleFilterType::Pointer shiftFilter      = ShiftScaleFilterType::New();
 49   NormalizeFilterType::Pointer  normalizeFilter = NormalizeFilterType::New();
 50  
 51   reader->SetFileName( "BrainProtonDensitySlice.png" );
 52   //现在将一个 reader 滤波器 ( 这里并未展示它的创建 ) 的输出作为输入连接到投射滤波器(四种)
 53   castFilter->SetInput( reader->GetOutput() );//castFilter滤波器
 54   shiftFilter->SetInput( reader->GetOutput() );//shiftFilter滤波器
 55   rescaleFilter->SetInput( reader->GetOutput() );//rescaleFilter滤波器
 56   normalizeFilter->SetInput( reader->GetOutput() );//normalizeFilter滤波器
 57   /*接下来我们设置每个滤波器需要的参数。 CastImageFilter 和 NormalizeImageFilter 不需要
 58       任何参数。另一方面, RescaleIntensityImageFilter 需要用户提供想得到的输出图像像素值的
 59       最大最小值,这可以通过使用 SetOutputMinimum() 和 SetOutputMaximum() 方式来实现,如
 60       下所示*/
 61   rescaleFilter->SetOutputMinimum(  0 );
 62   rescaleFilter->SetOutputMaximum( 255 );
 63   /*ShiftScaleImageFilter 需要一个乘数因子(比例)(一个传递比例的附加值(移位)。可以
 64       分别使用 SetScale() 和 SetShift() 方式来设置这些值*/
 65   shiftFilter->SetScale( 1.2 );//1.2
 66   shiftFilter->SetShift( 25 );//25
 67   //最后,通过调用 Update() 方式来运行这些滤波器
 68   /*castFilter->Update();
 69   shiftFilter->Update();
 70   rescaleFilter->Update();
 71   normalizeFilter->Update();*/
 72   //写文件,输出线性映射四种滤波效果
 73   writer1->SetFileName("castFilter_out.png");
 74   writer2->SetFileName("shiftFilter_out.png");
 75   writer3->SetFileName("rescaleFilter_out.png");
 76   writer4->SetFileName("normalizeFilter_out.png");
 77  
 78   typedef unsigned char                          WritePixelType;
 79   typedef itk::Image< WritePixelType, 2 >        WriteImageType;
 80   typedef itk::RescaleIntensityImageFilter<
 81       OutputImageType, WriteImageType > RescaleFilterType;
 82  
 83   RescaleFilterType::Pointer rescaler1 = RescaleFilterType::New();
 84   RescaleFilterType::Pointer rescaler2 = RescaleFilterType::New();
 85   RescaleFilterType::Pointer rescaler3 = RescaleFilterType::New();
 86   RescaleFilterType::Pointer rescaler4 = RescaleFilterType::New();
 87  
 88   rescaler1->SetOutputMinimum(0);
 89   rescaler1->SetOutputMaximum(255);
 90  
 91   rescaler2->SetOutputMinimum(0);
 92   rescaler2->SetOutputMaximum(255);
 93  
 94   rescaler3->SetOutputMinimum(155);
 95   rescaler3->SetOutputMaximum(255);
 96  
 97   rescaler4->SetOutputMinimum(0);
 98   rescaler4->SetOutputMaximum(255);
 99  
100   rescaler1->SetInput(castFilter->GetOutput());
101   rescaler2->SetInput(shiftFilter->GetOutput());
102   rescaler3->SetInput(rescaleFilter->GetOutput());
103   rescaler4->SetInput(normalizeFilter->GetOutput());
104  
105   writer1->SetInput(rescaler1->GetOutput());
106   writer2->SetInput(rescaler2->GetOutput());
107   writer3->SetInput(rescaler3->GetOutput());
108   writer4->SetInput(rescaler4->GetOutput());
109  
110   writer1->Update();
111   writer2->Update();
112   writer3->Update();
113   writer4->Update();
114  
115   return EXIT_SUCCESS;
116 }

 输入图像

 castFilter图像 

 normalizeFilter图像

 rescaleFilter图像  

 shiftFilter图像

posted on 2023-08-16 14:17  一杯清酒邀明月  阅读(21)  评论(0编辑  收藏  举报