目錄
- 1、itkFlipImageFilter 圖像翻轉濾波器
- 2、itkResampleImageFilter 重采樣圖像濾波器
1、itkFlipImageFilter 圖像翻轉濾波器
該類的主要功能是使輸入數據在用戶指定的軸上進行翻轉。
翻轉軸通過函數SetFlipAxes(array) 設置,其中輸入是FixArray<bool,ImageDimension>。 圖像在 array[i] 為 true 的軸上翻轉。
就網格坐標而言,圖像在輸入圖像的最大可能區域內翻轉,因此,輸出圖像的 LargestPossibleRegion與輸入圖像相同。
就幾何坐標而言,輸出原點是圖像相對于坐標軸翻轉的。
常用的成員函數:
Set/GetFlipAxes()
:設置/獲取要翻轉的軸, 圖像沿著array[i]為true的軸翻轉,默認為 falseSet/GetFlipAboutOrigin()
:設置/獲取計算的輸出原點,如果FlipAboutOrigin為“On”,則翻轉將圍繞軸的原點發生,如為“false”則翻轉將圍繞軸的中心發生,默認為“On”FlipAboutOriginOn/Off()
:同上
示例代碼:
#include "itkImage.h"
#include "itkFlipImageFilter.h"typedef itk::Image<short, 3> ShortImageType;bool flipImageFilter(ShortImageType* image, ShortImageType* outImage)
{typename FlipFilterType::FlipAxesArrayType flipArray;flipArray[0] = true; //沿x軸翻轉flipArray[1] = false;flipArray[2] = false;typedef itk::FlipImageFilter<ShortImageType> FlipFilterType;typename FlipFilterType::Pointer flipFilter = FlipFilterType::New();flipFilter->SetInput(image);flipFilter->SetFlipAxes(flipArray);flipFilter->SetFlipAboutOrigin(false); //On:圍繞圓點進行翻轉,false:圍繞軸中心進行翻轉,默認為“On”//flipFilter->FlipAboutOriginOn(); //與SetFlipAboutOrigin功能一樣//flipFilter->FlipAboutOriginOff();try{flipFilter->Update();}catch (itk::ExceptionObject& ex){//讀取過程發生錯誤std::cerr << "Error: " << ex << std::endl;return false;}outImage = flipFilter->GetOutput();return true;
}
2、itkResampleImageFilter 重采樣圖像濾波器
該類通過坐標變換對圖像進行重新采樣,并通過插值函數對圖像進行插值
該類是根據輸入和輸出圖像的類型進行模板化的,
請注意,插值器函數的選擇可能很重要,該函數通過**SetInterpolator()**進行設置。 默認為 LinearInterpolateImageFunction<InputImageType, TInterpolatorPrecisionType>,這對于普通醫學圖像來說是合理的。 然而,一些合成圖像的像素是從有限的指定集合中提取的, 比如一個掩模,它指示將大腦分割成少量的組織類型,對于這樣的圖像,一般不在不同的像素值之間進行插值,NearestNeighborInterpolateImageFunction<InputImageType, TCoordRep> 會是更好的選擇。
如果樣本是從圖像域外部獲取的,則默認行為是使用默認像素值。 如果需要不同的行為,可以使用 SetExtrapolator() 設置外推器函數。
應設置輸出圖像的輸出信息(間距、大小和方向),該信息具有單位間距、零原點和同一方向的正常默認值,輸出信息可以從參考圖像獲取;如果提供了參考圖像并且UseReferenceImage 為“On”,則將使用參考圖像的間距、原點和方向。重采樣是在空間坐標中執行,而不是像素/網格坐標。
由于此過濾器生成的圖像與其輸入的大小不同,因此它需要重寫ProcessObject中定義的多個方法,以便正確管理管道執行模型。 特別是,此過濾器重寫 ProcessObject::GenerateInputRequestedRegion() 和 ProcessObject::GenerateOutputInformation()。
此過濾器亦可實現多線程過濾器,使用DynamicThreadedGenerateData() 方法來實現。
常用的成員函數:
Set/GetInterpolator()
:設置/獲取插值器函數,默認值為 LinearInterpolateImageFunction,其他選項:NearestNeighborInterpolateImageFunction(對于二進制蒙版和其他具有少量可能像素值的圖像很有用)和 BSplineInterpolateImageFunction(提供更高階的插值)SetExtrapolator()
:設置/獲取外推器函數,默認值為 DefaultPixelValue,其他選項:NearestNeighborExtrapolateImageFunctionSetDefaultPixelValue()
:當設置/獲取變換后的像素位于圖像外部的像素值,默認為 0GetOutputStartIndex()
:獲取輸出最大可能區域的起始索引Set/GetSize()
:設置/獲取輸出圖像的大小Set/GetOutputDirection()
:設置/獲取輸出方向余弦矩陣Set/GetOutputOrigin()
:設置/獲取輸出圖像的原點Set/GetOutputSpacing()
:設置/獲取輸出圖像的像素間距SetOutputParametersFromImage()
:根據此圖像設置輸出參數的輔助方法Set/GetTransformInput()
:設置/獲取用于重采樣的坐標變換,注意,這必須在物理坐標中,并且它是輸出到輸入的變換,默認情況下,過濾器使用身份轉換,如果不想使用默認的Identity轉換,則在嘗試運行過濾器之前,必須在此處提供不同的轉換UseReferenceImageOn/Off()
:打開/關閉是否應使用指定的參考圖像來定義輸出信息Set/GetUseReferenceImage()
:同上true:On,false:OffSet/GetReferenceImage()
:設置用于定義輸出信息的參考圖像,默認情況下,輸出信息通過 SetOutputSpacing、SetOutputOrigin 和 SetOutputDirection 或 SetOutputParametersFromImage 方法指定,此方法可用于指定要從中復制像素信息的圖像,必須設置UseReferenceImageOn才能使用參考圖像
坐標變換:
恒等變換:輸出圖像點(x,y,x)的像素值 = 輸入圖像點(x,y,z)的像素值,點坐標均為同一個空間坐標。
非恒等:當輸入輸出圖像的原點Orign和間距Space相同時,從輸出空間到輸入空間的點映射,(-30,-50,-10)
的變換,指輸出圖像點(x,y,x)
的像素值=輸入圖像點(x-30,y-50,z-10)
的像素值。
恒等變換計算公式:
輸出圖像參數:
原點 orignOut
= {orignOut0,orignOut1,orignOut2}
間距 spaceOut
= {spaceOut0,spaceOut1,spaceOut2}
尺寸大小 size
= {sizeOut0,sizeOut1,sizeOut2}
輸出圖像參數:
原點 orignIn
= {orignIn0,orignIn1,orignIn2}
間距 spaceIn
={spaceIn0,space1In,spaceIn2}
尺寸大小sizeIn
={sizeIn0,sizeIn1,sizeIn2}
求像素值:
輸出圖像點I[3]
= {X, Y, Z}
的像素將于空間坐標點 P
相關聯
P
的坐標值:P[3]
= {X*spaceOut0+orignOut0, Y*spaceOut1+orignOut1, Z*spaceOut2+orignOut2}
P
點在輸入圖像對應的點 Q
坐標值為:Q[3]
= {(P[0]-orignIn0)/spaceIn0, (P[1]-orignIn1)/spaceIn1, (P[1]-orignIn1)/spaceIn1}
如果Q
不是整數坐標,那么輸出圖像點 I
的像素值 = 輸入圖像中圍繞非整數標記 Q
插入值來計算的。
示例代碼:
#include "itkImage.h"
#include "itkAffineTransform.h"
#include "itkNearestNeighborInterpolateImageFunction.h"
#include "itkResampleImageFilter.h"typedef itk::Image<short, 3> ShortImageType;
typedef itk::Image<float, 3> FloatImageType;bool resampleImageFilter(ShortImageType* image, FloatImageType* outImage)
{double space[3] = { 1,1,1 };double orign[3] = { 0,0,0 };ShortImageType::SizeType size = { 300,300,300 };typedef itk::ResampleImageFilter<ShortImageType, FloatImageType> ResampleFilterType;typename ResampleFilterType::Pointer resampleFilter = ResampleFilterType::New();resampleFilter->SetInput(image);//使用用來表示空間坐標類型和圖像維度來定義變換類型,默認的設置變換參數來表示恒等變換typedef itk::AffineTransform<double, 3> TransformType;typename TransformType::Pointer transform = TransformType::New();//typename TransformType::OutputVectorType translation;//translation[0] = -30;//translation[1] = -50;//translation[2] = -10;//transform->Translate(translation);transform->SetIdentity();resampleFilter->SetTransform(transform); //使用用來表示空間坐標類型和圖像類型來定義校對機類型typedef itk::NearestNeighborInterpolateImageFunction<ShortImageType, double> NNInterpolateType;typename NNInterpolateType::Pointer interpolate = NNInterpolateType::New();resampleFilter->SetInterpolator(interpolate);//輸出參數設置resampleFilter->SetDefaultPixelValue(0);resampleFilter->SetOutputSpacing(space);resampleFilter->SetOutputOrigin(orign);resampleFilter->SetSize(size);try{resampleFilter->Update();}catch (itk::ExceptionObject& ex){//讀取過程發生錯誤std::cerr << "Error: " << ex << std::endl;return false;}outImage = resampleFilter->GetOutput();return true;
}