itk 读取图像,两种格式之间转换

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/guo503604087/article/details/51125518


/************************************************************************
 
	控制台运行程序
	输入:
		程序名称
		读取图像文件名
		保存图像文件名
	输出:
		保存另一种类型的图像
	功能:
		图像在jpeg和bmp两种格式之间互换
************************************************************************/

#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkImageIOFactory.h"
#include "itkBMPImageIOFactory.h"
#include "itkJPEGImageIOFactory.h"
#include <iostream>

int main(int argc , char *argv[])
{
	if (argc < 3)
	{
		std::cerr << "Usage: " << std::endl;
		std::cerr << argv[0] << " inputImageFile  outputImageFile " << std::endl;
		return EXIT_FAILURE;
	}

	itk::BMPImageIOFactory::RegisterOneFactory();
	itk::JPEGImageIOFactory::RegisterOneFactory();

	typedef itk::Image< unsigned char, 2 > ImageType;
	typedef itk::ImageFileReader< ImageType > ReaderType;
	typedef itk::ImageFileWriter< ImageType > WriterType;

	ReaderType::Pointer reader = ReaderType::New();
	WriterType::Pointer writer = WriterType::New();

	const char * inputFilename = argv[1];
	const char * outputFilename = argv[2];

	reader->SetFileName( inputFilename );
	writer->SetFileName( outputFilename );

	writer->SetInput( reader->GetOutput() );
	try
	{
		writer->Update();
	}
	catch( itk::ExceptionObject & err )
	{
		std::cerr << "ExceptionObject caught !" << std::endl;
		std::cerr << err << std::endl;
		return EXIT_FAILURE;
	}

	system("pause");
	return 0;
}


展开阅读全文

没有更多推荐了,返回首页