逻辑运算
#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"//与
int _tmain(int argc, _TCHAR* argv[])
{
double factor = 2.0;
typedef itk::Image<short,2> ImageType;
typedef itk::ImageFileReader<ImageType> ReaderType;
typedef itk::ImageToVTKImageFilter<ImageType> ConnectorType;
ReaderType::Pointer reader= ReaderType::New();
ConnectorType::Pointer connector= ConnectorType::New();
reader->SetFileName("D:\\MedImg\\SE2\\IM182");
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();
connector->SetInput( andFilter->GetOutput() );
vtkImageViewer* viewer= vtkImageViewer::New();
vtkWin32RenderWindowInteractor* renderWindowInteractor=
vtkWin32RenderWindowInteractor::New();
viewer->SetupInteractor( renderWindowInteractor);
viewer->SetInput( connector->GetOutput() );
viewer->Render();
viewer->SetColorWindow(600);
viewer->SetColorLevel(300);
renderWindowInteractor->Start();
return 0;
}