VTK 三维轮廓等值面的提取

来源:互联网 发布:淘宝装修代码教程 编辑:程序博客网 时间:2024/05/26 02:54

目的: 学习VTK可视化


案例一

<span style="font-size:12px;">/************************************************************************//* 可视化基础算法-三维轮廓面(等值面)提取                                                                     *//************************************************************************//#include "stdafx.h"#include <vtkAutoInit.h>  VTK_MODULE_INIT(vtkRenderingOpenGL2);  VTK_MODULE_INIT(vtkInteractionStyle);  #include "vtkActor.h"#include "vtkCamera.h"#include "vtkPolyData.h"#include "vtkRenderer.h"#include "vtkRenderWindow.h"#include "vtkPolyDataMapper.h"#include "vtkRenderWindowInteractor.h"#include <vtkImageData.h>#include <vtkVectorNorm.h> #include <vtkMergePoints.h> #include <vtkSmartPointer.h>#include <vtkMarchingCubes.h> #include <vtkVolume16Reader.h>#include <vtkPolyDataMapper.h> int _tmain(int argc, _TCHAR* argv[]){vtkSmartPointer<vtkVolume16Reader> pVol16Read = vtkVolume16Reader::New();pVol16Read->SetDataDimensions(64, 64); // 设置每幅图像的像数尺寸pVol16Read->SetDataByteOrderToLittleEndian();  // 标识所读取文件的字节序,对于PC机,进行该设置pVol16Read->SetFilePrefix("F:\\ZYHWorkspace\\Data\\headsq\\quarter"); //设置读取的图像文件名前缀pVol16Read->SetImageRange(1, 93);pVol16Read->SetDataOrigin(0, 0, 0);pVol16Read->SetDataSpacing(3.2, 3.2, 1.5);// 归并重合的点vtkSmartPointer<vtkMergePoints> pMergePoints = vtkMergePoints::New();pMergePoints->SetDivisions(32, 32, 46);pMergePoints->SetNumberOfPointsPerBucket(100);// 定义移动立方体过滤器对象vtkSmartPointer<vtkMarchingCubes> pMarchCube = vtkMarchingCubes::New();pMarchCube->SetInputData((vtkDataObject *) pVol16Read->GetOutput()); // 处理体数据pMarchCube->SetValue(0, 1150); // 设置提取的等值面的值pMarchCube->ComputeGradientsOn();pMarchCube->ComputeScalarsOff();pMarchCube->SetLocator(pMergePoints); // 生成等值面单元、顶点的标量值vtkSmartPointer<vtkVectorNorm> pVectorNormal = vtkVectorNorm::New();pVectorNormal->SetInputData(pMarchCube->GetOutput());double Rang[2];pVectorNormal->GetOutput()->GetScalarRange(Rang);// 得到生成的标量值范围vtkSmartPointer<vtkPolyDataMapper> pMapper = vtkPolyDataMapper::New();pMapper->SetInputData((vtkPolyData *) pVectorNormal->GetOutput());pMapper->ScalarVisibilityOn();pMapper->SetScalarRange(0, 1250);vtkSmartPointer<vtkActor> pActor = vtkActor::New();pActor->SetMapper(pMapper);vtkSmartPointer<vtkRenderer> pRen = vtkRenderer::New();pRen->AddActor(pActor);pRen->ResetCamera();pRen->SetBackground(1, 1, 1);vtkSmartPointer<vtkRenderWindow> pRenWin = vtkRenderWindow::New();pRenWin->AddRenderer(pRen);pRenWin->SetSize(500, 500);pRenWin->Render();vtkSmartPointer<vtkRenderWindowInteractor> pRenI = vtkRenderWindowInteractor::New();pRenI->SetRenderWindow(pRenWin);pRenI->Start();return 0;}</span><span style="font-size:14px;"></span>



案例二

// MarchingCubeVTK2.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <vtkAutoInit.h>  VTK_MODULE_INIT(vtkRenderingOpenGL2);  VTK_MODULE_INIT(vtkInteractionStyle);  #include <vtkRenderer.h>#include <vtkRenderWindow.h>#include <vtkRenderWindowInteractor.h>#include <vtkVolume16Reader.h>#include <vtkPolyDataMapper.h>#include <vtkActor.h>#include <vtkOutlineFilter.h>#include <vtkCamera.h>#include <vtkProperty.h>#include <vtkPolyDataNormals.h>#include <vtkContourFilter.h>#include <vtkSmartPointer.h>int _tmain(int argc, _TCHAR* argv[]){// Create the renderer, the render window, and the interactor. The renderer// draws into the render window, the interactor enables mouse- and // keyboard-based interaction with the data within the render window.//vtkSmartPointer<vtkRenderer> aRenderer = vtkSmartPointer<vtkRenderer>::New();vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();renWin->AddRenderer(aRenderer);vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();iren->SetRenderWindow(renWin);// The following reader is used to read a series of 2D slices (images)// that compose the volume. The slice dimensions are set, and the// pixel spacing. The data Endianness must also be specified. The reader// uses the FilePrefix in combination with the slice number to construct// filenames using the format FilePrefix.%d. (In this case the FilePrefix// is the root name of the file: quarter.)vtkSmartPointer<vtkVolume16Reader> v16 = vtkSmartPointer<vtkVolume16Reader>::New();v16->SetDataDimensions (64,64);v16->SetImageRange (1,93);v16->SetDataByteOrderToLittleEndian();v16->SetFilePrefix ("F:\\ZYHWorkspace\\Data\\headsq\\quarter");v16->SetDataSpacing (3.2, 3.2, 1.5);// An isosurface, or contour value of 500 is known to correspond to the// skin of the patient. Once generated, a vtkPolyDataNormals filter is// is used to create normals for smooth surface shading during rendering.vtkSmartPointer<vtkContourFilter> skinExtractor = vtkSmartPointer<vtkContourFilter>::New();skinExtractor->SetInputConnection(v16->GetOutputPort());skinExtractor->SetValue(0, 500);vtkSmartPointer<vtkPolyDataNormals> skinNormals = vtkSmartPointer<vtkPolyDataNormals>::New();skinNormals->SetInputConnection(skinExtractor->GetOutputPort());skinNormals->SetFeatureAngle(60.0);vtkSmartPointer<vtkPolyDataMapper> skinMapper = vtkSmartPointer<vtkPolyDataMapper>::New();skinMapper->SetInputConnection(skinNormals->GetOutputPort());skinMapper->ScalarVisibilityOff();vtkSmartPointer<vtkActor> skin = vtkSmartPointer<vtkActor>::New();skin->SetMapper(skinMapper);// An outline provides context around the data.vtkSmartPointer<vtkOutlineFilter> outlineData = vtkSmartPointer<vtkOutlineFilter>::New();outlineData->SetInputConnection(v16->GetOutputPort());vtkSmartPointer<vtkPolyDataMapper> mapOutline = vtkSmartPointer<vtkPolyDataMapper>::New();mapOutline->SetInputConnection(outlineData->GetOutputPort());vtkSmartPointer<vtkActor> outline = vtkSmartPointer<vtkActor>::New();outline->SetMapper(mapOutline);outline->GetProperty()->SetColor(0,0,0);// It is convenient to create an initial view of the data. The FocalPoint// and Position form a vector direction. Later on (ResetCamera() method)// this vector is used to position the camera to look at the data in// this direction.vtkSmartPointer<vtkCamera> aCamera = vtkSmartPointer<vtkCamera>::New();aCamera->SetViewUp (0, 0, -1);aCamera->SetPosition (0, 1, 0);aCamera->SetFocalPoint (0, 0, 0);aCamera->ComputeViewPlaneNormal();aCamera->Azimuth(30.0);aCamera->Elevation(30.0);// Actors are added to the renderer. An initial camera view is created.// The Dolly() method moves the camera towards the FocalPoint,// thereby enlarging the image.aRenderer->AddActor(outline);aRenderer->AddActor(skin);aRenderer->SetActiveCamera(aCamera);aRenderer->ResetCamera ();aCamera->Dolly(2.5);// Set a background color for the renderer and set the size of the// render window (expressed in pixels).aRenderer->SetBackground(.2, .3, .4);renWin->SetSize(640, 480);// Note that when camera movement occurs (as it does in the Dolly()// method), the clipping planes often need adjusting. Clipping planes// consist of two planes: near and far along the view direction. The // near plane clips out objects in front of the plane; the far plane// clips out objects behind the plane. This way only what is drawn// between the planes is actually rendered.aRenderer->ResetCameraClippingRange ();// Initialize the event loop and then start it.iren->Initialize();iren->Start();return 0;}



不明白为什么有错结果出不来。。。


读取数据有问题吗? 不应该啊我都设置成绝对路径了,怎么还不对!

0 0
原创粉丝点击