天天看點

VTK學習筆記-1

對于TIFF圖像的讀取時候,TIFF reader所讀取和存儲的是點格式,即point data in the "Tiff Scalars" array name; 對于體素需要轉化為cell data。對于volume image的可視化 in a sort of parametric dimensions (namely pixel width, pixel height, number of tiff images).。使用 transform filters to scale to physical dimensions. 


以下是使用VTK進行可視化的一個例子:

           
#include <vtkSmartPointer.h>
#include <vtkImageViewer2.h>
#include <vtkTIFFReader.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include<vtkImageStack.h>
#include<vtkImageReader.h>
#include<vtkImageMapper3D.h>
#include<vtkImageData.h>
#include<vtkVolumeProperty.h>
#include<vtkStructuredPointsReader.h>
#include<vtkPiecewiseFunction.h>
#include<vtkColorTransferFunction.h>
#include<vtkVolumeRayCastCompositeFunction.h>
#include<vtkVolumeRayCastMapper.h>
int main()
{
  vtkTIFFReader *reader =vtkTIFFReader::New();
  vtkImageData *image =vtkImageData::New();
  reader->SetFilePrefix ("E:\\VTK_examples\\testdata\\" );
  reader->SetDataExtent(0,2062,0,1490,0,13);
  reader->SetFilePattern("%s%01d.tif");
  reader->Update();
  //image=reader->GetOutput();
	 /* int dims[3];
	   reader->GetOutput()->GetDimensions(dims);
	   std::cout<<"圖像維數:"<<dims[0]<<" "<<dims[1]<<" "<<dims[2]<<std::endl;
	   
	   double origin[3];
	reader->GetOutput()->GetOrigin(origin);*/
	//std::cout<<"圖像原點:"<<origin[0]<<" "<<origin[1]<<" "<<origin[2]<<std::endl;
	//double spaceing[3];
	//reader->GetOutput()->GetSpacing(spaceing);
	//std::cout<<"像素間隔:"<<spaceing[0]<<" "<<spaceing[1]<<" "<<spaceing[2]<<std::endl;
	//int* dim_s = image->GetDimensions();
 // // int dims[3]; // can't do this
 //
 // std::cout << "維數: " << " x: " << dim_s[0] << " y: " << dim_s[1] << " z: " << dim_s[2] << std::endl;
 // std::cout << "Number of points: " << image->GetNumberOfPoints() << std::endl;//所有像素點的個數
 // std::cout << "Number of cells: " << image->GetNumberOfCells() << std::endl;//Standard vtkDataSet API methods. 
	  //image->CopyStructure(reader->GetOutput());
	  //stack->AddImage(reader)
	 /* reader->Update();
	  image->Update();*/
  //3D的顯示
   // 建立标準的渲染器與映射器。
  vtkRenderer *ren1=vtkRenderer::New();
  vtkRenderWindow *renWin=vtkRenderWindow::New();
    renWin->AddRenderer(ren1);
  vtkRenderWindowInteractor *iren=vtkRenderWindowInteractor::New();
    iren->SetRenderWindow(renWin);
  // 建立标量到不透明度的轉移函數。
  vtkPiecewiseFunction *opacityTransferFunction=vtkPiecewiseFunction::New();
    opacityTransferFunction->AddPoint( 0.0, 0.0);
    opacityTransferFunction->AddPoint(255, 1.8);

  // 建立标量的顔色的轉移函數。
  vtkColorTransferFunction *colorTransferFunction=vtkColorTransferFunction::New();
   /* colorTransferFunction->AddRGBPoint(  0.0, 0.0, 0.0, 0.0);
    colorTransferFunction->AddRGBPoint( 128.0, 1.0, 0.0, 0.0);*/
    colorTransferFunction->AddRGBPoint(128.0, 0.0, 0.0, 1.0);
    colorTransferFunction->AddRGBPoint(192.0, 0.0, 1.0, 0.0);
    colorTransferFunction->AddRGBPoint(255.0, 0.0, 0.2, 0.0);

  // 設定屬性,其中包括兩個映射函數。
  vtkVolumeProperty *volumeProperty=vtkVolumeProperty::New();
    volumeProperty->SetColor(colorTransferFunction);
    volumeProperty->SetScalarOpacity(opacityTransferFunction);
    volumeProperty->ShadeOn();
    volumeProperty->SetInterpolationTypeToLinear();

  // 光線投射映射器知道如何渲染資料。
  vtkVolumeRayCastCompositeFunction  *compositeFunction=
			vtkVolumeRayCastCompositeFunction::New();
  vtkVolumeRayCastMapper *volumeMapper=vtkVolumeRayCastMapper::New();
    volumeMapper->SetVolumeRayCastFunction(compositeFunction);
    volumeMapper->SetInputConnection(reader->GetOutputPort());

  // vtkVolume掌管映射器和屬性對象,可控制體資料的位置和朝向
  vtkVolume *volume=vtkVolume::New();
    volume->SetMapper(volumeMapper);
    volume->SetProperty(volumeProperty);

  ren1->AddVolume(volume);
  ren1->SetBackground(1, 1, 1);
  renWin->SetSize(600, 600);
  renWin->Render();

  iren->Initialize();
  iren->Start();
  
  return 0;

}
           

繼續閱讀