欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ITK学习笔记——dicom读取写入

程序员文章站 2022-04-01 08:45:18
...

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