#include <vtkRenderer.h> #include <vtkRenderWindow.h> #include <vtkRenderWindowInteractor.h> #include <vtkInteractorStyleTrackballCamera.h> #include <vtkRayCastImageDisplayHelper.h> #include <vtkSmartPointer.h> #include <vtkImageReader.h> #include <vtkImageCast.h> #include <vtkMarchingCubes.h> #include <vtkStripper.h> #include <vtkDataObject.h> #include <vtkPolyDataMapper.h> #include <vtkProperty.h> #include <vtkOutlineFilter.h> void main() { vtkSmartPointer<vtkRenderer> ren = vtkSmartPointer<vtkRenderer>::New();//设置绘制者(绘制对象指针) vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();//设置绘制窗口 renWin->AddRenderer(ren);//将绘制者加入绘制窗口 vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();//设置绘制交互操作窗口的 iren->SetRenderWindow(renWin);//将绘制窗口添加到交互窗口 vtkSmartPointer<vtkInteractorStyleTrackballCamera> style = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();//交互摄像机 iren->SetInteractorStyle(style);//style为交互模式 vtkSmartPointer<vtkImageReader> reader = vtkSmartPointer<vtkImageReader>::New(); reader->SetFileName("C:\\Users\\HuangWang\\Desktop\\Head_256x256x256.raw"); reader->SetFileDimensionality(3);//设置显示图像的维数 reader->SetDataScalarType(VTK_UNSIGNED_CHAR);//VTK_UNSIGNED_short将数据转换为unsigned char型 reader->SetDataExtent(0, 255, 0, 255, 0, 255); reader->SetDataSpacing(0.9, 0.9, 0.9); //设置像素间间距 reader->SetDataOrigin(0.0, 0.0, 0.0);//设置基准点,(一般没有用)做虚拟切片时可能会用的上 reader->Update(); vtkSmartPointer<vtkImageCast> readerImageCast = vtkSmartPointer<vtkImageCast>::New();//数据类型转换 readerImageCast->SetInputConnection(reader->GetOutputPort()); readerImageCast->SetOutputScalarTypeToUnsignedChar(); readerImageCast->ClampOverflowOn();//阀值 vtkSmartPointer<vtkMarchingCubes>marchingcube=vtkSmartPointer<vtkMarchingCubes>::New(); marchingcube->SetInput((vtkDataObject*)readerImageCast->GetOutput()); //marchingcube->SetInputConnection(Reader->GetOutputPort()); //第二种读取数据的方法 marchingcube->SetValue(0,160); vtkSmartPointer<vtkStripper>Stripper=vtkSmartPointer<vtkStripper>::New(); Stripper->SetInput( marchingcube->GetOutput()); vtkSmartPointer<vtkPolyDataMapper>polyMapper=vtkSmartPointer<vtkPolyDataMapper>::New(); polyMapper->SetInput(Stripper->GetOutput()); polyMapper->ScalarVisibilityOff(); vtkSmartPointer<vtkActor>actor=vtkSmartPointer<vtkActor>::New(); actor->SetMapper(polyMapper); actor->GetProperty()->SetDiffuseColor(1,0.19,0.15); actor->GetProperty()->SetSpecular(0.1); actor->GetProperty()->SetSpecularPower(10); actor->GetProperty()->SetColor(1,0,0); vtkSmartPointer<vtkOutlineFilter>outlinefilter=vtkSmartPointer<vtkOutlineFilter>::New(); outlinefilter->SetInputConnection(reader->GetOutputPort()); vtkSmartPointer<vtkPolyDataMapper>outlineMapper=vtkSmartPointer<vtkPolyDataMapper>::New(); outlineMapper->SetInputConnection(outlinefilter->GetOutputPort()); vtkSmartPointer<vtkActor>outlineActor=vtkSmartPointer<vtkActor>::New(); outlineActor->SetMapper(outlineMapper); outlineActor->GetProperty()->SetColor(0,0,0); ren->AddActor(actor); ren->AddActor(outlineActor); ren->SetBackground(1,1,1); renWin->SetSize(640,640); iren->Initialize(); iren->Start(); ren->ResetCameraClippingRange(); }