#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle)
VTK_MODULE_INIT(vtkRenderingContextOpenGL2)
VTK_MODULE_INIT(vtkRenderingFreeType)
#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 "itkImageFileWriter.h"
#include "itkCastImageFilter.h"
#include "itkGDCMImageIOFactory.h"
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
typedef itk::Image<unsigned char, 2> ImageType;
typedef itk::Image<unsigned char, 2> UnsignedCharImageType;
typedef itk::ImageFileReader<ImageType> ReaderType;
typedef itk::ImageToVTKImageFilter<ImageType> ConnectorType;
std::string DCM = ".DCM";
itk::GDCMImageIOFactory::RegisterOneFactory();
for (int num = 1; num<118; num++)
{
std::string inputFilename = "E:\\imgdata\\2013-06-04\\1.2.840.113619.2.55.3.2831155457.416.1370299545.455";
std::string outputFilename = "D:\\IM\\1.2.840.113619.2.55.3.2831155457.416.1370299545.455";
char t[256];
string s;
sprintf_s(t, ".%d", num);
s = t;
inputFilename = inputFilename + s + DCM;
outputFilename = outputFilename + s + DCM;
struct stat buffer;
if (stat(inputFilename.c_str(), &buffer) != 0)
continue;
ReaderType::Pointer reader = ReaderType::New();
ConnectorType::Pointer connector = ConnectorType::New();
reader->SetFileName(inputFilename);
//reader->SetImageIO( );
//itk::GDCMImageIOFactory::RegisterOneFactory();
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];
outputSize[1] = inputSize[1];
ImageType::SpacingType outputSpacing;
outputSpacing[0] = reader->GetOutput()->GetSpacing()[0];
outputSpacing[1] = reader->GetOutput()->GetSpacing()[1];
typedef itk::BinaryThresholdImageFilter<ImageType, ImageType> FilterType;
FilterType::Pointer Thresholdfilter = FilterType::New();
Thresholdfilter->SetInput(reader->GetOutput());
Thresholdfilter->SetLowerThreshold(100);
Thresholdfilter->SetUpperThreshold(180);
//默认设置 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::CastImageFilter<ImageType, UnsignedCharImageType > CastFilterType;
CastFilterType::Pointer castFilter = CastFilterType::New();
castFilter->SetInput(Thresholdfilter->GetOutput());
typedef itk::ImageFileWriter< UnsignedCharImageType > WriterType;
WriterType::Pointer writer = WriterType::New();
writer->SetFileName(outputFilename);
writer->SetInput(castFilter->GetOutput());
writer->Update();
}
return 0;
}
今天试了下,如果还转换成unsigned short数据,相当于没转,只是做了下滤波,新数据就少了3Kb,不知道为什么,ITK不是专门处理医学图像的吗,为何他的Demo里多是png图像,真搞不懂