《itk实用demo》-分割后图像保存(循环保存一序列)

分割后图像保存(循环保存一序列)

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageToVTKImageFilter.h"
#include "vtkImageViewer.h"
#include "vtkWin32RenderWindowInteractor.h"
#include "itkResampleImageFilter.h"//采样
#include "itkBinaryThresholdImageFilter.h"//二值化
#include "itkThresholdImageFilter.h"//阈值分割
#include "itkBinaryBallStructuringElement.h"
#include "itkBinaryMorphologicalOpeningImageFilter.h"//开运算
#include "itkMultiplyImageFilter.h"//乘法
#include "itkAndImageFilter.h"//与
#include "itkImageFileWriter.h"
#include "itkCastImageFilter.h"
#include 
#include 
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
 typedef itk::Image<unsigned short,2> ImageType;
 typedef itk::Image<unsigned char, 2>  UnsignedCharImageType;
 typedef itk::ImageFileReader ReaderType;
 typedef itk::ImageToVTKImageFilter ConnectorType;
 std::string bmp = ".bmp";
 double factor = 2.0;
 for (int num= 0;num<214;num++)
 {
  std::string  inputFilename = "D:\\MedImg\\SE6\\IM";
  std::string  outputFilename = "D:\\se6\\bmp\\IM";
  char t[256];
  string s;
  sprintf(t, "%d", num);
  s = t;
  inputFilename = inputFilename + s;
  outputFilename = outputFilename + s + bmp;

  ReaderType::Pointer reader= ReaderType::New();
  ConnectorType::Pointer connector= ConnectorType::New();
  reader->SetFileName(inputFilename);
  reader->Update();
  ImageType::SizeType inputSize = reader->GetOutput()->GetLargestPossibleRegion().GetSize();
  std::cout << "Original size: " << reader->GetOutput()->GetLargestPossibleRegion().GetSize() << std::endl;
  // Resize
  ImageType::SizeType outputSize;
  outputSize[0] = inputSize[0]/2;
  outputSize[1] = inputSize[1]/2;
  ImageType::SpacingType outputSpacing;
  outputSpacing[0] =  reader->GetOutput()->GetSpacing()[0] * 2;
  outputSpacing[1] =  reader->GetOutput()->GetSpacing()[1] * 2;
  typedef itk::IdentityTransform<double, 2> TransformType;
  typedef itk::ResampleImageFilter ResampleImageFilterType;
  ResampleImageFilterType::Pointer resample = ResampleImageFilterType::New();
  resample->SetInput( reader->GetOutput());
  resample->SetSize(outputSize);
  resample->SetOutputSpacing(outputSpacing);
  resample->SetTransform(TransformType::New());
  resample->UpdateLargestPossibleRegion();
  ImageType::PointType origin;
  origin[0]=-static_cast<double>(inputSize[0])/4;
  origin[1]=-static_cast<double>(inputSize[1])/4;
  resample->SetOutputOrigin(origin);//设定原点
  resample->Update();
  ImageType::Pointer output = resample->GetOutput();
  std::cout << "Output size: " << output->GetLargestPossibleRegion().GetSize() << std::endl;
  typedef itk::BinaryThresholdImageFilter  FilterType;
  FilterType::Pointer Thresholdfilter = FilterType::New();
  Thresholdfilter->SetInput( resample->GetOutput() );
  Thresholdfilter->SetLowerThreshold(224);
  Thresholdfilter->SetUpperThreshold(676);
  //默认设置 SetInsideValue 255 SetOutsideValue 0
  Thresholdfilter->Update();
  typedef itk::BinaryBallStructuringElement
   StructuringElementType;
  StructuringElementType structuringElement;
  structuringElement.SetRadius(8);
  structuringElement.CreateStructuringElement();
  typedef itk::BinaryMorphologicalOpeningImageFilter 
   BinaryMorphologicalOpeningImageFilterType;
  BinaryMorphologicalOpeningImageFilterType::Pointer openingFilter
   = BinaryMorphologicalOpeningImageFilterType::New();
  openingFilter->SetInput(Thresholdfilter->GetOutput());
  openingFilter->SetKernel(structuringElement);
  openingFilter->Update();
  typedef itk::MultiplyImageFilter  MultiplyImageFilterType;
  MultiplyImageFilterType::Pointer multiplyFilter
   = MultiplyImageFilterType::New ();
  multiplyFilter->SetInput1( resample->GetOutput());
  multiplyFilter->SetInput2( openingFilter->GetOutput() );
  typedef itk::AndImageFilter  AndImageFilterType;
  AndImageFilterType::Pointer andFilter
   = AndImageFilterType::New();
  andFilter->SetInput(0, resample->GetOutput());
  andFilter->SetInput(1, openingFilter->GetOutput());
  andFilter->Update();

  typedef itk::CastImageFilter CastFilterType;
  CastFilterType::Pointer castFilter = CastFilterType::New();
  castFilter->SetInput(andFilter->GetOutput());
  typedef  itk::ImageFileWriter< UnsignedCharImageType  > WriterType;
  WriterType::Pointer writer = WriterType::New();
  writer->SetFileName(outputFilename);
  writer->SetInput(castFilter->GetOutput());
  writer->Update();
  //connector->SetInput( andFilter->GetOutput() );
  //vtkImageViewer* viewer= vtkImageViewer::New();
  //vtkWin32RenderWindowInteractor* renderWindowInteractor=
  // vtkWin32RenderWindowInteractor::New();
  //viewer->SetupInteractor( renderWindowInteractor);
  //viewer->SetInput( connector->GetOutput() );
  //viewer->Render();
  //viewer->SetColorWindow(600);
  //renderWindowInteractor->Start();
 }
 return 0;
}

你可能感兴趣的:(ITK医学图像处理)