本文主要是介绍ITK学习笔记——dicom读取写入,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
ITK毕竟是医学图像处理专用,主要还是处理dicom医学格式的图像。dicom数据类型为signed short型。
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkGDCMImageIO.h"#include <list>
#include <fstream>int main(int argc, char* argv[])
{typedef signed short InputPixelType;const unsigned int InputDimension = 2;typedef itk::Image< InputPixelType, InputDimension > InputImageType;typedef itk::ImageFileReader< InputImageType > ReaderType;ReaderType::Pointer reader = ReaderType::New();reader->SetFileName("C:/input/lung.dcm");typedef itk::GDCMImageIO ImageIOType;ImageIOType::Pointer gdcmImageIO = ImageIOType::New();reader->SetImageIO(gdcmImageIO);try{reader->Update();}catch (itk::ExceptionObject & e){std::cerr << "exception in file reader " << std::endl;std::cerr << e << std::endl;return EXIT_FAILURE;}typedef itk::ImageFileWriter< InputImageType > Writer1Type;Writer1Type::Pointer writer = Writer1Type::New();writer->SetFileName("C:/output/lung.dcm");writer->SetInput(reader->GetOutput());try{writer->Update();}catch (itk::ExceptionObject & e){std::cerr << "exception in file writer " << std::endl;std::cerr << e << std::endl;return EXIT_FAILURE;}return EXIT_SUCCESS;
}
一般对dicom进行处理都要将其转换为0~255单通道灰度图,这时我们需要添加#include "itkRescaleIntensityImageFilter.h"文件。
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkGDCMImageIO.h"
#include "itkRescaleIntensityImageFilter.h"#include <list>
#include <fstream>int main(int argc, char* argv[])
{typedef signed short InputPixelType;const unsigned int InputDimension = 2;typedef itk::Image< InputPixelType, InputDimension > InputImageType;typedef itk::ImageFileReader< InputImageType > ReaderType;ReaderType::Pointer reader = ReaderType::New();reader->SetFileName("C:/input/lung.dcm");typedef itk::GDCMImageIO ImageIOType;ImageIOType::Pointer gdcmImageIO = ImageIOType::New();reader->SetImageIO(gdcmImageIO);try{reader->Update();}catch (itk::ExceptionObject & e){std::cerr << "exception in file reader " << std::endl;std::cerr << e << std::endl;return EXIT_FAILURE;}typedef unsigned char WritePixelType;typedef itk::Image< WritePixelType, 2 > WriteImageType;typedef itk::RescaleIntensityImageFilter<InputImageType, WriteImageType > RescaleFilterType;RescaleFilterType::Pointer rescaler = RescaleFilterType::New();rescaler->SetOutputMinimum(0);rescaler->SetOutputMaximum(255);typedef itk::ImageFileWriter< WriteImageType > Writer2Type;Writer2Type::Pointer writer = Writer2Type::New();writer->SetFileName("C:/output/lung.dcm");rescaler->SetInput(reader->GetOutput());writer->SetInput(rescaler->GetOutput());try{writer->Update();}catch (itk::ExceptionObject & e){std::cerr << "exception in file writer " << std::endl;std::cerr << e << std::endl;return EXIT_FAILURE;}return EXIT_SUCCESS;
}
这时候,写入的文件就变成了灰度图。
这篇关于ITK学习笔记——dicom读取写入的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!