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

ITK elastix 刚性 配准

程序员文章站 2022-04-01 08:19:28
...

读取数据设置 3D

const unsigned int Dimension = 3;
	typedef float PixelType;
	typedef itk::Image<PixelType, Dimension> ImageType;
	typedef itk::ImageFileReader<ImageType> ReaderType;
	ReaderType::Pointer fixReader = ReaderType::New();
	ReaderType::Pointer movReader = ReaderType::New();
	fixReader->SetFileName("D:/input/fixed.mhd");
	movReader->SetFileName("D:/input/moving.mhd");
	try
	{
		fixReader->Update();
		movReader->Update();
	}
	catch (itk::ExceptionObject &ex)
	{
		std::cout << ex << std::endl;
		return 0;
	}

准备配准参数设置


	typedef ELASTIX::ParameterMapType RegistrationParametersType;
	typedef itk::ParameterFileParser ParserType;
	// Create parser for transform parameters text file.
	ParserType::Pointer file_parser = ParserType::New();
	// Try parsing transform parameters text file.
	file_parser->SetParameterFileName("D:/input/parameters_Rigid.txt");
	try
	{
		file_parser->ReadParameterFile();
	}
	catch (itk::ExceptionObject & e)
	{
		std::cout << e.what() << std::endl;
		// Do some error handling!
	}

运行elastix

	RegistrationParametersType parameters = file_parser->GetParameterMap();
	typedef std::vector<RegistrationParametersType> RegistrationParametersContainerType;
	ELASTIX* elastix = new ELASTIX();
	int error = 0;
	try
	{
		error = elastix->RegisterImages(
		 	static_cast<  itk::DataObject::Pointer>(fixReader->GetOutput()),
			static_cast<  itk::DataObject::Pointer>(movReader->GetOutput()),
			parameters, // Parameter map read in previous code
			"D:\\output", // Directory where output is written, if enabled
			true, // Enable/disable writing of elastix.log
			true, // Enable/disable output to console
			0, // Provide fixed image mask (optional, 0 = no mask)
			0 // Provide moving image mask (optional, 0 = no mask)
			);
	}
	catch (itk::ExceptionObject &err)
	{
		// Do some error handling.
	}
	if (error == 0)
	{
		if (elastix->GetResultImage().IsNotNull())
		{
			// Typedef the ITKImageType first...
			ImageType * output_image = static_cast<ImageType *>(
				elastix->GetResultImage().GetPointer());

		 
		}
		else
		{
			// Registration failure. Do some error handling.
		}
		// Get transform parameters of all registration steps.
	
		// Clean up memory.
		delete elastix;
	}

保存数据(注意 ImageType PixelTyp,读取和保存时,一定要注意,否则会有崩溃的可能)

		//// Write the deformation field
		    itk::MetaImageIOFactory::RegisterOneFactory();
			typedef  itk::ImageFileWriter<ImageType>   WriterType;
			WriterType::Pointer writer = WriterType::New();
			writer->SetFileName("D:/output/imag.mhd");
			writer->SetInput(output_image);
	 
			writer->SetUseCompression(false);
		 
			try
			{
				writer->Update();
			}
			catch (itk::ExceptionObject & err)
			{
				std::cerr << "ExceptionObject caught !" << std::endl;
				std::cerr << err << std::endl;
				 
			} 

工程配置很复杂,这里不展开

相关标签: ITK