如何用ITK读取序列图像,并对每一帧图像进行处理,最后显示,保存成同序列图像。

typedef itk::GDCMImageIO                        ImageIOType;
	typedef itk::GDCMSeriesFileNames                NamesGeneratorType;

	ImageIOType::Pointer gdcmIO = ImageIOType::New();
	NamesGeneratorType::Pointer namesGenerator = NamesGeneratorType::New();
	
	namesGenerator->SetInputDirectory("E:/imgdata/2017-07-05");

	const ReaderType::FileNamesContainer filenames = namesGenerator->GetInputFileNames();
		
	
	std::size_t numberOfFileNames = filenames.size();
	std::cout << numberOfFileNames << std::endl;
	for (unsigned int fni = 0; fni < numberOfFileNames; ++fni)
	{
		 
		std::cout << filenames[fni] << std::endl;
		//ITK读取数据
		typedef itk::ImageFileReader<ImageType> ReaderType;
		//读取指针
		ReaderType::Pointer reader = ReaderType::New();
		//reader->SetFileName(filenames[fni].c_str());
		reader->SetFileName(filenames[fni]);
		//创建读取DCM的GDCMIOImage类
		typedef itk::GDCMImageIO       ImageIOType;
		ImageIOType::Pointer dicomIO = ImageIOType::New();
		reader->SetImageIO(dicomIO);
		try
		{
			reader->Update();
		}
		catch (itk::ExceptionObject& e)
		{
			std::cerr << "exception in file reader" << std::endl;
			std::cerr << e << std::endl;
			 
		}
		typedef itk::MetaDataDictionary   DictionaryType;
		const DictionaryType& dict1 = dicomIO->GetMetaDataDictionary();
		m_pCastFilter->SetInput(reader->GetOutput());
		//1.高斯去噪

		
		//2.灰度拉伸
		m_rescaleFliter->SetInput(m_pCastFilter->GetOutput());
		m_rescaleFliter->SetOutputMaximum(255);
		m_rescaleFliter->SetOutputMinimum(0);
		m_rescaleFliter->Update();
		
		//3.阈值分割

		m_pBinaryThresholdImageFilter->SetInput(m_rescaleFliter->GetOutput());
		m_pBinaryThresholdImageFilter->SetOutsideValue(0);
		m_pBinaryThresholdImageFilter->SetInsideValue(255);
		m_pBinaryThresholdImageFilter->SetLowerThreshold(180);
		m_pBinaryThresholdImageFilter->SetUpperThreshold(255);
		//4.显示处理后的图片
		
		//数据转换为signed short类型
		            m_pCast2Short->SetInput(m_pBinaryThresholdImageFilter->GetOutput());//m_pOtsuThresholdImageFilter->GetOutput()
		m_pCast2Short->Update();
		
		//链接滤波器,转换为VTK类型
		m_pConnector->SetInput(m_pCast2Short->GetOutput());
		try
		{
			m_pConnector->Update();
		}
		catch (itk::ExceptionObject& e)
		{
			std::cerr << "exception in file reader" << std::endl;
			std::cerr << e << std::endl;
		}

		//显示图像
		vtkSmartPointer<vtkImageActor> actor = vtkImageActor::New();
		actor->SetInputData(m_pConnector->GetOutput());
		actor->InterpolateOff();
		actor->Update();
		m_pRender2D->AddActor(actor);
		m_pImageViewer->GetImageActor()->InterpolateOff();
		m_pRender2D->ResetCamera();
		m_pRender2D->DrawOn();
		ui.m_qvtk2DWidget->GetRenderWindow()->Render();

		Sleep(200);
		m_pRender2D->RemoveActor2D(actor);
		//把文件写入文件夹
		using OutputPixelType = signed short;
		constexpr unsigned int OutputDimension = 3;
		typedef itk::Image< OutputPixelType, OutputDimension > WriteImageType;
		typedef itk::ImageFileWriter< WriteImageType >  WriterType;
		WriterType::Pointer writer = WriterType::New();
		using Image2DType = itk::Image<OutputPixelType, OutputDimension>;
		writer->SetInput(m_pCast2Short->GetOutput());

		char drive[_MAX_DRIVE] = { 0 };
		char dir[_MAX_DIR] = { 0 };
		char fname[_MAX_FNAME] = { 0 };
		char ext[_MAX_EXT] = { 0 };
		_splitpath(filenames[fni].c_str(), drive, dir, fname, ext);
		std::string filename = "E:/imgdata/2020-10-09/";
		filename += fname; filename += ext;
		writer->SetFileName(filename);
		writer->SetImageIO(dicomIO);
		writer->UseInputMetaDataDictionaryOff();
		dicomIO->KeepOriginalUIDOn();//此句非常重要,没有此句话,图像序列被破坏
//		writer->SetUseInputMetaDataDictionary(m_pCast2Short->GetMetaDataDictionary());
		writer->SetMetaDataDictionary(dict1);
		
		try
		{
			writer->Update();
		}
		catch (itk::ExceptionObject& e)
		{
			std::cerr << "exception in file reader" << std::endl;
			std::cerr << e << std::endl;
		}
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值