vtk读取bmp序列图像。
测试的序列"HJ_%d.bmp"图像采用RegionGrowing从CT里分割获得。
我上传在这里。http://pan.baidu.com/s/1mgyU0di
注意dataExtent最后的值是序列文件的数量,这里是66,setfilePrefix包含路径和HJ_文件前缀
Main.cxx
#include "vtkRenderer.h" #include "vtkRenderWindow.h" #include "vtkRenderWindowInteractor.h" #include "vtkPiecewiseFunction.h" #include "vtkColorTransferFunction.h" #include "vtkVolumeProperty.h" #include "vtkVolumeRayCastIsosurfaceFunction.h" #include "vtkVolumeRayCastCompositeFunction.h" #include "vtkVolumeRayCastMapper.h" #include "vtkVolume.h" #include "vtkImageCast.h" #include "vtkBMPReader.h" int main() { vtkRenderer *aRender = vtkRenderer::New(); vtkRenderWindow *renWin = vtkRenderWindow::New(); renWin->AddRenderer(aRender); vtkRenderWindowInteractor *iRen = vtkRenderWindowInteractor::New(); iRen->SetRenderWindow(renWin); vtkBMPReader *reader = vtkBMPReader::New(); reader->SetDataExtent(0,128,0,128,1,66); reader->SetFilePrefix("D:\bmp\HJ_"); reader->SetFilePattern("%s%d.bmp"); reader->SetDataSpacing (1, 1, 1);//像素间的间隔 reader->SetAllow8BitBMP(16) ;//很重要 //reader->Allow8BitBMPOff(); vtkImageCast *readerImageCast = vtkImageCast::New(); //readerImageCast->SetInput((vtkDataObject *)reader->GetOutput()); readerImageCast->SetInputConnection(reader->GetOutputPort()); readerImageCast->SetOutputScalarTypeToUnsignedChar(); //readerImageCast->ClampOverflowOn(); vtkPiecewiseFunction *opacityTransferFunction = vtkPiecewiseFunction::New(); opacityTransferFunction->AddPoint(0, 0.0); opacityTransferFunction->AddPoint(20, 0.0); opacityTransferFunction->AddPoint(100, 0.2); opacityTransferFunction->AddPoint(255, 0.4); opacityTransferFunction->ClampingOn(); vtkColorTransferFunction *colorTransferFunction = vtkColorTransferFunction::New(); colorTransferFunction->AddRGBPoint(0.0, 0.0, 0.0, 0.0); colorTransferFunction->AddRGBPoint(20.0, 0.0, 0.0, 0.2); colorTransferFunction->AddRGBPoint(60.0, 0.2, 0.1, 0.3); colorTransferFunction->AddRGBPoint(128.0, 0.8, 0.4, 0.0); colorTransferFunction->AddRGBPoint(196.0, 0.27, 0.21, 0.1); colorTransferFunction->AddRGBPoint(255.0, 0.8, 0.8, 0.8); vtkVolumeProperty *volumeProperty = vtkVolumeProperty::New(); volumeProperty->SetColor(colorTransferFunction); volumeProperty->SetScalarOpacity(opacityTransferFunction); volumeProperty->ShadeOn(); volumeProperty->SetInterpolationTypeToLinear(); volumeProperty->SetAmbient(0.2); volumeProperty->SetDiffuse(0.9); volumeProperty->SetSpecular(0.2); volumeProperty->SetSpecularPower(10); vtkVolumeRayCastCompositeFunction *compositeFunction = vtkVolumeRayCastCompositeFunction::New(); vtkVolumeRayCastMapper *volumeMapper = vtkVolumeRayCastMapper::New(); volumeMapper->SetVolumeRayCastFunction(compositeFunction); volumeMapper->SetInputConnection(readerImageCast->GetOutputPort()); vtkVolume *volume = vtkVolume::New(); volume->SetMapper(volumeMapper); volume->SetProperty(volumeProperty); volume->RotateX(130); // volume->RotateY(35); // volume->RotateZ(-35); aRender->AddVolume(volume); aRender->SetBackground(1, 1, 1); renWin->SetSize(600, 600); renWin->Render(); iRen->Initialize(); renWin->Render(); iRen->Start(); aRender->Delete(); renWin->Delete(); iRen->Delete(); reader->Delete(); opacityTransferFunction->Delete(); colorTransferFunction->Delete(); volumeMapper->Delete(); volumeProperty->Delete(); compositeFunction->Delete(); volume->Delete(); return 0; }
CMakeLists.txt
cmake_minimum_required(VERSION 2.8) PROJECT(BMPconstruct) find_package(VTK REQUIRED) include(${VTK_USE_FILE}) add_executable(BMPconstruct MACOSX_BUNDLE Main.cxx) if(VTK_LIBRARIES) target_link_libraries(BMPconstruct ${VTK_LIBRARIES}) else() target_link_libraries(BMPconstruct vtkHybrid) endif()
结果: