天天看点

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;

}
           

继续阅读