ITKで画像処理を始めよう(例:2値化のプログラム)
こんにちは!
今日はITKで画像処理を行う流れについてですヽ(・ω・´メ)
私はC++を用いていますので、今後もC++で書いていきます。
☆-----------------------------------------------------------------------☆
■大きな流れ
reader(画像の入力) ⇒ 処理 ⇒ writer(画像の出力)
となっています。
「処理」の部分に自分がやりたい処理を書きます。
■ITKのクラスの呼び出し
typedef itk::クラス名<テンプレート引数> 適当な名前型;
適当な名前型::Pointer 適当な変数名=適当な名前型::New();
適当な変数名 -> 関数; ←必要な、行いたい処理
適当な変数名 -> Update(); ←ここまでの処理が実行される
■使ってみよう(例)2値化:固定しきい値法
<入出力に必要なヘッダ>
・itkImageFileReader.h
・itkImageFileWriter.h
・itkImage.h
<2値化に必要なヘッダ>
・itkBinaryThresholdImageFilter.h
// -------- 必要なヘッダをinclude --------- //
//****** ITK ******//
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkImage.h"
// 2値化
#include "itkBinaryThresholdImageFilter.h"
//****** C++ ******//
#include<iostream>
#include<string>
using namespace std;
int main(int argc,char *argv[])
{
if( argc < 3 )
{
cerr << "引数が一致しません." << endl;
return EXIT_FAILURE;
}
//使用するピクセルをtypedef
typedef short InputPixelType;
typedef short OutputPixelType;
const unsigned int Dimensions = 3; // 次元数
//使用する領域・画像サイズをtypedef
typedef itk::Image< InputPixelType, Dimensions > InputImageType;
typedef itk::Image< OutputPixelType, Dimensions > OutputImageType;
//■□■□■□■□■□■□ 出力用 □■□■□■□■□■□■//
typedef itk::ImageFileWriter< OutputImageType > WriterFilterType;
WriterFilterType::Pointer writer = WriterFilterType::New();
//■□■□■□■□■□■□ 必要な変数 □■□■□■□■□■□■//
string inputfilename, outputfilename; // 入力・出力名
int UThreshold = 200; //Upperしきい値の値(使用したいしきい値)
int LThreshold = 100; //Lowerしきい値の値(使用したいしきい値)
/************************************/
//■□■□■□■□■□■□ 設定 □■□■□■□■□■□■//
inputfilename = argv[1];
outputfilename = argv[2];
cout << inputfilename << " Start!" << endl;
/************** 入力画像について **************/
cout << "/*-------- 入力画像の読み込み ------------*/ " << endl;
//■□■□■□ 変数 □■□■□■//
typedef itk::ImageFileReader< InputImageType > ReaderFilterType;
ReaderFilterType::Pointer reader = ReaderFilterType::New();
//■□■□■□ 原画像 Reader フィルタ □■□■□■//
cout << "Reader starts." << endl;
reader -> SetFileName( inputfilename ); // 入力画像の名前
try{
reader -> Update();
}
catch(itk::ExceptionObject &e){ // エラー処理
cerr << "Can't Reader." << endl;
cerr << e << endl;
return EXIT_FAILURE;
}
cout << "Reader has finishied.\n" << endl;
/************** 2値化のフィルタ **************/
cout << "/*-------- itkBinaryフィルタ ------------*/ " << endl;
//■□■□■□ 変数 □■□■□■//
typedef itk::BinaryThresholdImageFilter< InputImageType, OutputImageType > BinaryType;
BinaryType::Pointer binary = BinaryType::New();
//■□■□■□ binary フィルタ □■□■□■//
cout << "itkBinary Starts! " << endl;
binary -> SetInput( reader ->GetOutput() ); //readerで読み込んだ画像をセット
binary -> SetUpperThreshold( UThreshold ); //2値化の上限値
binary -> SetLowerThreshold( LThreshold ); //2値化の下限値
binary -> SetInsideValue( 255 ); //上限値と下限値の間の画素値
binary -> SetOutsideValue( 0 ); //下限値以下、上限値以上のときの画素値
try{
binary -> Update();
}
catch(itk::ExceptionObject &e){ // エラー処理
cerr << "Can't Binary Filter." << endl;
cerr << e << endl;
return EXIT_FAILURE;
}
cout << "itkBinary has finished. " << endl;
/************** Writer **************/
cout << "/*-------- 出力画像へ書き込み ------------*/ " << endl;
//■□■□■□ Writer フィルタ □■□■□■//
cout << "Writer starts." << endl;
writer -> SetInput( binary -> GetOutput() ); // 処理後画像をセット
writer -> SetFileName( outputfilename );
try{
writer -> Update();
}
catch(itk::ExceptionObject &e){
cerr << "Can't Writer." << endl;
cerr << e << endl;
return EXIT_FAILURE;
}
cout << "Completion of Writing!" << endl;
cout << "Finish!" << endl;
return 0;
}
☆-----------------------------------------------------------------------☆
このように、入力⇒処理⇒出力、と書いていけば、自分の行いたい画像処理のプログラムを簡単に書くことが出来ますヽ(○`・v・)人(・v・´●)ノ
次回からは他のフィルタについても書いていきますヾ(=・ω・=)o