VTK圖像處理之vtkImageReslice

  1. VTK圖像處理之vtkImageReslice

  2. vtk類之vtkImageReslice:基本算法,對體數據沿着軸進行切片

  3. VTK/Tutorials/Extents

三維圖像切面提取

切片(Slice)或切面是三維圖像比較常用的概念,尤其在醫學圖像中。通過提取切面可以方便地瀏覽和分析圖像內部組織結構。VTK中vtkImageReSlice類可以實現圖像切面的提取。在實際開發中,四視圖中冠狀視面、矢狀面和橫斷面(顯示過圖像內部一點且平行於XY、YZ、XZ平面的平面),需要用到此類。

在顯示圖像時,很多時候我們需要的不是矢狀面,冠狀面或者橫斷面這種與座標軸平行的正交面,而是一些斜切面。這時候就可以利用vtkImageReslice 這個類來實現。

這個類的輸入最主要的是SetResliceAxes()這個函數,這個函數的輸入是vtkMatrix4x4 其代表的是新的座標系在世界座標系下的座標值。可以把vtkMatrix4x4看成是4列,其中第一,二,三列分別爲新的座標系在世界座標系下的向量座標,第四列代表的是新的座標原點在世界座標系下的座標值。如下面的四列值就表示新的座標系原點爲(100,200,10),x方向向量座標爲(1,0,0),y方向向量座標爲(0,0,-1),z方向向量座標爲(0,1,0)。
1,0,0,100
0,0,1,200
0,-1,0,10
0,0,0,1
要注意的是這三個法向量要滿足右手法則,即X叉乘Y爲Z,Y叉乘Z爲X,Z叉乘X爲Y。如果不滿足這個規則,得到的座標系就不符合右手法則,得不到想要的切面。
另外通過reslice->SetSlabModeToSum();可以得到類似於DRR的圖像效果。
最後通過GetOutput函數獲得vtkImageData
 

示例說明

CMakeLists.txt文件代碼如下:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(ImageResliceExample)
FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})
ADD_EXECUTABLE(ImageResliceExample  ImageResliceExample.cpp)
TARGET_LINK_LIBRARIES(ImageResliceExample ${VTK_LIBRARIES})    

ImageResliceExample.cpp文件代碼如下:

#include <vtkSmartPointer.h>
#include <vtkImageReader2.h>
#include <vtkMatrix4x4.h>
#include <vtkImageReslice.h>
#include <vtkLookupTable.h>
#include <vtkImageMapToColors.h>
#include <vtkImageActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkCommand.h>
#include <vtkImageData.h>
#include <vtkMetaImageReader.h>
#include <vtkImageCast.h>

class vtkImageInteractionCallback : public vtkCommand
{
public:
    static vtkImageInteractionCallback *New()
    {
        return new vtkImageInteractionCallback; 
    }

    vtkImageInteractionCallback() 
    {
        this->Slicing = 0;
        this->ImageReslice = 0;
        this->Interactor = 0; 
    }

    void SetImageReslice(vtkImageReslice *reslice) 
    {
        this->ImageReslice = reslice; 
    }

    void SetImageMapToColors(vtkImageMapToColors *mapToColors)
    {
        this->mapToColors = mapToColors;
    }

    vtkImageReslice *GetImageReslice() 
    {
        return this->ImageReslice;
    }

    void SetInteractor(vtkRenderWindowInteractor *interactor) 
    {
        this->Interactor = interactor;
    }

    vtkRenderWindowInteractor *GetInteractor() 
    {
        return this->Interactor; 
    }

    virtual void Execute(vtkObject *, unsigned long event, void *)
    {
        vtkRenderWindowInteractor *interactor = this->GetInteractor();

        int lastPos[2];
        interactor->GetLastEventPosition(lastPos);
        int currPos[2];
        interactor->GetEventPosition(currPos);

        if (event == vtkCommand::LeftButtonPressEvent)
        {
            this->Slicing = 1;
        }
        else if (event == vtkCommand::LeftButtonReleaseEvent)
        {
            this->Slicing = 0;
        }
        else if (event == vtkCommand::MouseMoveEvent)
        {
            if (this->Slicing)
            {
                vtkImageReslice *reslice = this->ImageReslice;

                // Increment slice position by deltaY of mouse
                int deltaY = lastPos[1] - currPos[1];

                reslice->Update();
                double sliceSpacing = reslice->GetOutput()->GetSpacing()[2];
                vtkMatrix4x4 *matrix = reslice->GetResliceAxes();
                // move the center point that we are slicing through
                double point[4];
                double center[4];
                point[0] = 0.0;
                point[1] = 0.0;
                point[2] = sliceSpacing * deltaY;
                point[3] = 1.0;
                matrix->MultiplyPoint(point, center);
                matrix->SetElement(0, 3, center[0]);
                matrix->SetElement(1, 3, center[1]);
                matrix->SetElement(2, 3, center[2]);
                mapToColors->Update();
                interactor->Render();
            }
            else
            {
                vtkInteractorStyle *style = vtkInteractorStyle::SafeDownCast(
                    interactor->GetInteractorStyle());
                if (style)
                {
                    style->OnMouseMove();
                }
            }
        }
    }

private:
    int Slicing;
    vtkImageReslice *ImageReslice;
    vtkRenderWindowInteractor *Interactor;
    vtkImageMapToColors *mapToColors;
};

int main()
{
    vtkSmartPointer<vtkMetaImageReader> reader =
        vtkSmartPointer<vtkMetaImageReader>::New();
    reader->SetFileName ( "E:\\TestData\\brain.mhd" );
    reader->Update();

    int extent[6];
    double spacing[3];
    double origin[3];

    reader->GetOutput()->GetExtent(extent);
    reader->GetOutput()->GetSpacing(spacing);
    reader->GetOutput()->GetOrigin(origin);

    double center[3];
    center[0] = origin[0] + spacing[0] * 0.5 * (extent[0] + extent[1]);
    center[1] = origin[1] + spacing[1] * 0.5 * (extent[2] + extent[3]);
    center[2] = origin[2] + spacing[2] * 0.5 * (extent[4] + extent[5]);

    static double axialElements[16] = {
        1, 0, 0, 0,
        0, 1, 0, 0,
        0, 0, 1, 0,
        0, 0, 0, 1 
    };

    vtkSmartPointer<vtkMatrix4x4> resliceAxes =
        vtkSmartPointer<vtkMatrix4x4>::New();
    resliceAxes->DeepCopy(axialElements);

    resliceAxes->SetElement(0, 3, center[0]);
    resliceAxes->SetElement(1, 3, center[1]);
    resliceAxes->SetElement(2, 3, center[2]);

    vtkSmartPointer<vtkImageReslice> reslice =
        vtkSmartPointer<vtkImageReslice>::New();
    reslice->SetInputConnection(reader->GetOutputPort());
    reslice->SetOutputDimensionality(2);
    reslice->SetResliceAxes(resliceAxes);
    reslice->SetInterpolationModeToLinear();

    vtkSmartPointer<vtkLookupTable> colorTable =
        vtkSmartPointer<vtkLookupTable>::New();
    colorTable->SetRange(0, 1000); 
    colorTable->SetValueRange(0.0, 1.0);
    colorTable->SetSaturationRange(0.0, 0.0);
    colorTable->SetRampToLinear();
    colorTable->Build();

    vtkSmartPointer<vtkImageMapToColors> colorMap =
        vtkSmartPointer<vtkImageMapToColors>::New();
    colorMap->SetLookupTable(colorTable);
    colorMap->SetInputConnection(reslice->GetOutputPort());
    colorMap->Update();

    vtkSmartPointer<vtkImageActor> imgActor =
        vtkSmartPointer<vtkImageActor>::New();
    imgActor->SetInputData(colorMap->GetOutput());

    vtkSmartPointer<vtkRenderer> renderer =
        vtkSmartPointer<vtkRenderer>::New();
    renderer->AddActor(imgActor);
    renderer->SetBackground(1, 1, 1);

    vtkSmartPointer<vtkRenderWindow> renderWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->SetSize(500, 500);
    renderWindow->AddRenderer(renderer);

    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
    vtkSmartPointer<vtkInteractorStyleImage> imagestyle =
        vtkSmartPointer<vtkInteractorStyleImage>::New();

    renderWindowInteractor->SetInteractorStyle(imagestyle);
    renderWindowInteractor->SetRenderWindow(renderWindow);
    renderWindowInteractor->Initialize();

    vtkSmartPointer<vtkImageInteractionCallback> callback =
        vtkSmartPointer<vtkImageInteractionCallback>::New();
    callback->SetImageReslice(reslice);
    callback->SetInteractor(renderWindowInteractor);
    callback->SetImageMapToColors(colorMap);

    imagestyle->AddObserver(vtkCommand::MouseMoveEvent, callback);
    imagestyle->AddObserver(vtkCommand::LeftButtonPressEvent, callback);
    imagestyle->AddObserver(vtkCommand::LeftButtonReleaseEvent, callback);

    renderWindowInteractor->Start();
    return EXIT_SUCCESS;
}

運行結果:

按下鼠標左鍵,移動鼠標時的gif圖片: 
這裏寫圖片描述

代碼解釋:

  1. 先通過vtkMetaImageReader讀取一副三維圖像,獲取圖像範圍、原點和像素間隔,由這三個參數可以計算圖像的中心位置。
  2. 接下來定義了切面的變換矩陣axialElements,該矩陣的前三列分別表示X、Y和Z方向矢量,第四列爲切面座標系原點。通過修改切面座標系原點,可以得到不同位置的切面圖像。
  3. 然後將讀取的圖像作爲vtkImageReslice的輸入,通過函數SetResliceAxes()設置變換矩陣resliceAxis。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章