VTK读取序列JPG图片用MC算法进行三维重建,JPG图片是用底层代码插值生成
1 #include <vtkRenderer.h>
2 #include <vtkRenderWindow.h>
3 #include <vtkRenderWindowInteractor.h>
4 #include <vtkJPEGReader.h>
5 #include <vtkMarchingCubes.h>
6 #include <vtkStripper.h>
7 #include <vtkPolyDataMapper.h>
8 #include <vtkActor.h>
9 #include <vtkProperty.h>
10 #include <vtkOutlineFilter.h>
11 #include <vtkSmartPointer.h>
12 #include <vtkCamera.h>
13
14 void main()
15 {
16 vtkSmartPointer<vtkRenderer>ren=vtkSmartPointer<vtkRenderer>::New();
17 vtkSmartPointer<vtkRenderWindow>renWin=vtkSmartPointer<vtkRenderWindow>::New();
18 renWin->AddRenderer(ren);
19 vtkSmartPointer<vtkRenderWindowInteractor>iren=vtkSmartPointer<vtkRenderWindowInteractor>::New();
20 iren->SetRenderWindow(renWin);
21
22 vtkSmartPointer<vtkJPEGReader>Reader=vtkSmartPointer<vtkJPEGReader>::New();
23 Reader->SetDataScalarTypeToUnsignedChar();
24 Reader->SetFileDimensionality(3);
25 Reader->SetFilePrefix("E:\\JPG\\rgb");
26 Reader->SetFileNameSliceSpacing(1);
27 Reader->SetFilePattern("%s%d.jpg");
28 Reader->SetDataExtent( 0, 200, 0, 400, 1,9);
29 Reader->SetDataSpacing(1,1,10);
30 Reader->Update();
31
32 vtkSmartPointer<vtkMarchingCubes>marchingcube=vtkSmartPointer<vtkMarchingCubes>::New();
33 marchingcube->SetInput((vtkDataSet *)Reader->GetOutput());
34 //marchingcube->SetInputConnection(Reader->GetOutputPort()); //第二种读取数据的方法
35 marchingcube->SetValue(0,200);
36
37 vtkSmartPointer<vtkStripper>Stripper=vtkSmartPointer<vtkStripper>::New();
38 Stripper->SetInput( marchingcube->GetOutput());
39
40 vtkSmartPointer<vtkPolyDataMapper>polyMapper=vtkSmartPointer<vtkPolyDataMapper>::New();
41 polyMapper->SetInput(Stripper->GetOutput());
42 polyMapper->ScalarVisibilityOff();
43
44 vtkSmartPointer<vtkActor>actor=vtkSmartPointer<vtkActor>::New();
45 actor->SetMapper(polyMapper);
46 actor->GetProperty()->SetDiffuseColor(1,0.19,0.15);
47 actor->GetProperty()->SetSpecular(0.1);
48 actor->GetProperty()->SetSpecularPower(10);
49 actor->GetProperty()->SetColor(1,0,0);
50
51 vtkSmartPointer<vtkOutlineFilter>outlinefilter=vtkSmartPointer<vtkOutlineFilter>::New();
52 outlinefilter->SetInputConnection(Reader->GetOutputPort());
53
54 vtkSmartPointer<vtkPolyDataMapper>outlineMapper=vtkSmartPointer<vtkPolyDataMapper>::New();
55 outlineMapper->SetInputConnection(outlinefilter->GetOutputPort());
56
57 vtkSmartPointer<vtkActor>outlineActor=vtkSmartPointer<vtkActor>::New();
58 outlineActor->SetMapper(outlineMapper);
59 outlineActor->GetProperty()->SetColor(0,0,0);
60
61 vtkSmartPointer<vtkCamera>aCamera=vtkSmartPointer<vtkCamera>::New();
62 aCamera->SetViewUp(0,0,-1);
63 aCamera->SetPosition(0,1,0);
64 aCamera->SetFocalPoint(0,0,0);
65 aCamera->ComputeViewPlaneNormal();
66
67 ren->AddActor(actor);
68 ren->AddActor(outlineActor);
69 ren->SetActiveCamera(aCamera);
70 ren->ResetCamera();
71 aCamera->Dolly(1.5);
72
73 ren->SetBackground(1,1,1);
74 renWin->SetSize(640,640);
75
76 ren->ResetCameraClippingRange();
77 iren->Initialize();
78 iren->Start();
79 }
80