itk 讀取圖像,兩種格式之間轉換


/************************************************************************
 
	控制檯運行程序
	輸入:
		程序名稱
		讀取圖像文件名
		保存圖像文件名
	輸出:
		保存另一種類型的圖像
	功能:
		圖像在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;
}


發佈了94 篇原創文章 · 獲贊 19 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章