小言_互联网的博客

VTK学习之路——读取并显示raw体数据文件

444人阅读  评论(0)

数据比较难找,老板给了个网址,里面很多数据可以下载:
https://klacansky.com/open-scivis-datasets/sorted-by-size.html
数据文件类型(raw):

rawDataReader.cxx

#include <vtkImageData.h>
#include <vtkDICOMImageReader.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkVolumeProperty.h>
#include <vtkPiecewiseFunction.h>
#include <vtkColorTransferFunction.h>
#include <vtkGPUVolumeRayCastMapper.h>
#include <vtkPointData.h>
#include <vtkCamera.h>
#include <vtkNew.h>
#include <vtkFloatArray.h>
#include <vtkImageReader.h>
#include <fstream>
#include <string>
//#include <vtkAutoInit.h>

//VTK_MODULE_INIT( vtkRenderingOpenGL2 );
//VTK_MODULE_INIT( vtkRenderingVolumeOpenGL2 );
int main()
{

	//raw data reader
	
	vtkSmartPointer<vtkImageReader> reader = vtkSmartPointer<vtkImageReader>::New();
 
	reader->SetFileName("/home/wjm/code/p_volume_rendering/data/marschner_lobb_41x41x41_uint8.raw");
	reader->SetFileDimensionality(3);//设置显示图像的维数
	reader->SetDataScalarType(VTK_UNSIGNED_CHAR);//VTK_UNSIGNED_short将数据转换为unsigned char型
	reader->SetDataExtent(0, 40, 0, 40, 0, 40);
	reader->SetDataSpacing(1, 1, 1); //设置像素间间距
	//reader->SetDataOrigin(0.0, 0.0, 0.0);//设置基准点,(一般没有用)做虚拟切片时可能会用的上
	reader->Update();
	


	
	//visualize the raw data


	// properties options
  	vtkNew<vtkVolumeProperty> volumeProperty;
  	volumeProperty->ShadeOn();
  	volumeProperty->SetInterpolationType(VTK_LINEAR_INTERPOLATION);

  	// get the real range in hounsfield
  	vtkDataArray *arr = reader->GetOutput()->GetPointData()->GetScalars();
  	double range[2];
  	arr->GetRange(range);

  	// 1D transfer functions
  	vtkNew<vtkColorTransferFunction> colorTF;
  	colorTF->AddRGBPoint(-200, 0.0, 0.0, 0.0);
  	colorTF->AddRGBPoint(110, 0.4, 0.4, 1.0);
  	colorTF->AddRGBPoint(512, 1.0, 1.0, 1.0);
  	colorTF->AddRGBPoint(range[1], 0.9, 0.1, 0.3);

  	vtkNew<vtkPiecewiseFunction> scalarTF;
  	//scalarTF->AddPoint(-200, 0.00);
  	scalarTF->AddPoint(-255, 0.00);
  	scalarTF->AddPoint(110, 0.00);
  	scalarTF->AddPoint(512, 0.5);
  	scalarTF->AddPoint(range[1], 0.9);

  	vtkNew<vtkPiecewiseFunction> gradientTF;
  	gradientTF->AddPoint(-200, 0.0);
  	gradientTF->AddPoint(range[1] / 4.0, 1.0);

  	volumeProperty->SetScalarOpacity(scalarTF);
  	volumeProperty->SetGradientOpacity(gradientTF);
  	volumeProperty->SetColor(colorTF);

  	// setup rendering context
  	vtkNew<vtkRenderWindow> renderWindow;
  	renderWindow->SetSize(512, 512);
  	renderWindow->SetMultiSamples(0);

  	// mapping data
  	vtkNew<vtkGPUVolumeRayCastMapper> mapper;
  	mapper->SetInputConnection(reader->GetOutputPort());
  	mapper->SetBlendModeToComposite();
  	mapper->SetUseJittering(1);

  	// renderer and volume
  	vtkNew<vtkRenderer> renderer;
  	renderWindow->AddRenderer(renderer);
  	renderer->SetBackground(0.03, 0.33, 0.33);

  	vtkNew<vtkVolume> volume;
  	volume->SetMapper(mapper);
  	volume->SetProperty(volumeProperty);
  	renderer->AddVolume(volume);

  	renderer->ResetCamera();
  	renderer->GetActiveCamera()->Zoom(1.3);

  	vtkNew<vtkRenderWindowInteractor> interactor;
  	interactor->SetRenderWindow(renderWindow);

  	vtkNew<vtkInteractorStyleTrackballCamera> style;
  	interactor->SetInteractorStyle(style);

  	renderWindow->Render();
  
  	interactor->Start();


	return 0; 
	 
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3...3.12 FATAL_ERROR)


project (rawDataReader)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkCommonDataModel
  #vtkIOImage
  vtkIOGeometry
  vtkInteractionImage
  vtkInteractionStyle
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingVolume
  vtkRenderingOpenGL2
  vtkRenderingVolumeOpenGL2 QUIET
)
if (NOT VTK_FOUND)
  message("Skipping dataReader: ${VTK_NOT_FOUND_MESSAGE}")
  return ()
endif()
message (STATUS "VTK_VERSION: ${VTK_VERSION}")
if (VTK_VERSION VERSION_LESS "8.90.0")
  # old system
  include(${VTK_USE_FILE})
  add_executable(rawDataReader MACOSX_BUNDLE rawDataReader.cxx )
  target_link_libraries(rawDataReader PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(rawDataReader MACOSX_BUNDLE rawDataReader.cxx )
  target_link_libraries(rawDataReader PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS rawDataReader
    MODULES ${VTK_LIBRARIES}
    )
endif () 

转载:https://blog.csdn.net/sinat_38052999/article/details/102489249
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场