1. 程式人生 > 其它 >VTK讀取序列jpg影象,進行面繪製

VTK讀取序列jpg影象,進行面繪製

#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkJPEGReader.h>
#include <vtkMarchingCubes.h>
#include <vtkStripper.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkProperty.h>
#include <vtkSmartPointer.h>
#include <vtkStringArray.h>
#include <vtkImageLuminance.h>

void main()
{
	//生成影象序列的檔名陣列  
	vtkSmartPointer<vtkStringArray > fileArray = vtkSmartPointer<vtkStringArray >::New();
	char fileName[128];
	for (int i = 1; i <= 200; i++) //幾個影象就迴圈幾次  
	{
		sprintf(fileName, "d:/jpg/img-%03d.jpg", i);
		vtkStdString fileStr = (vtkStdString)(fileName);
		fileArray->InsertNextValue(fileStr);
	}

	//讀取JPG序列影象  
	vtkSmartPointer<vtkJPEGReader>reader = vtkSmartPointer<vtkJPEGReader>::New();
	reader->SetFileNames(fileArray);
	reader->Update();

	//需要轉為灰度圖再進行Marching Cube
	vtkSmartPointer<vtkImageLuminance> lumFilter = vtkSmartPointer<vtkImageLuminance>::New();
	lumFilter->SetInputConnection(reader->GetOutputPort());
	lumFilter->Update();

	//抽取等值面資訊
	vtkSmartPointer<vtkMarchingCubes> mc = vtkSmartPointer<vtkMarchingCubes>::New();
	mc->SetInputConnection(lumFilter->GetOutputPort());
	mc->SetValue(0, 100); //設定提取的等值資訊
	mc->Update();

	//剔除舊的或廢除的資料單元,提高繪製的速度(可略去這一步)
	vtkSmartPointer<vtkStripper> stripper = vtkSmartPointer<vtkStripper>::New();
	stripper->SetInputData(mc->GetOutput());
	stripper->Update();

	vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
	mapper->SetInputData(stripper->GetOutput());
	mapper->ScalarVisibilityOff();

	vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
	actor->SetMapper(mapper);
	actor->GetProperty()->SetDiffuseColor(1, 0.19, 0.15);
	actor->GetProperty()->SetSpecular(0.1);
	actor->GetProperty()->SetSpecularPower(10);
	actor->GetProperty()->SetColor(0.5, 0.5, 0.5);

	vtkSmartPointer<vtkRenderer> render = vtkSmartPointer<vtkRenderer>::New();
	render->AddActor(actor);
	render->SetBackground(1.0, 1.0, 1.0);
	vtkSmartPointer<vtkRenderWindow> rw = vtkSmartPointer<vtkRenderWindow>::New();
	rw->AddRenderer(render);
	rw->SetSize(640, 480);
	rw->Render();
	rw->SetWindowName("Marching cube");
	vtkSmartPointer<vtkRenderWindowInteractor> rwi = vtkSmartPointer<vtkRenderWindowInteractor>::New();
	rwi->SetRenderWindow(rw);
	vtkSmartPointer<vtkInteractorStyleTrackballCamera> style = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
	rwi->SetInteractorStyle(style);
	rwi->Initialize();
	rwi->Start();
}