分割后图像保存(循环保存一序列)
#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 <iostream>
#include <string>
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<ImageType> ReaderType;
typedef itk::ImageToVTKImageFilter<ImageType> 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<ImageType, ImageType> 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<ImageType, ImageType> 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<ImageType::PixelType, ImageType::ImageDimension>
StructuringElementType;
StructuringElementType structuringElement;
structuringElement.SetRadius(8);
structuringElement.CreateStructuringElement();
typedef itk::BinaryMorphologicalOpeningImageFilter <ImageType, ImageType, StructuringElementType>
BinaryMorphologicalOpeningImageFilterType;
BinaryMorphologicalOpeningImageFilterType::Pointer openingFilter
= BinaryMorphologicalOpeningImageFilterType::New();
openingFilter->SetInput(Thresholdfilter->GetOutput());
openingFilter->SetKernel(structuringElement);
openingFilter->Update();
typedef itk::MultiplyImageFilter <ImageType,ImageType,ImageType> MultiplyImageFilterType;
MultiplyImageFilterType::Pointer multiplyFilter
= MultiplyImageFilterType::New ();
multiplyFilter->SetInput1( resample->GetOutput());
multiplyFilter->SetInput2( openingFilter->GetOutput() );
typedef itk::AndImageFilter <ImageType> AndImageFilterType;
AndImageFilterType::Pointer andFilter
= AndImageFilterType::New();
andFilter->SetInput(0, resample->GetOutput());
andFilter->SetInput(1, openingFilter->GetOutput());
andFilter->Update();
typedef itk::CastImageFilter<ImageType, UnsignedCharImageType > 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;
}