1 #include <vtkAutoInit.h>
2 VTK_MODULE_INIT(vtkRenderingOpenGL);
3
4 #include <vtkSmartPointer.h>
5 #include <vtkJPEGReader.h>
6 #include <vtkImageAccumulate.h>
7 #include <vtkImageData.h>
8 #include <vtkIntArray.h>
9 #include <vtkDataObject.h> //
10 #include <vtkFieldData.h> //一起用
11 #include <vtkBarChartActor.h>
12 #include <vtkProperty2D.h>
13 #include <vtkTextProperty.h>
14 #include <vtkLegendBoxActor.h>
15 #include <vtkImageActor.h>
16 #include <vtkRenderer.h>
17 #include <vtkRenderWindow.h>
18 #include <vtkRenderWindowInteractor.h>
19
20 int main()
21 {
22 vtkSmartPointer<vtkJPEGReader> reader =
23 vtkSmartPointer<vtkJPEGReader>::New();
24 reader->SetFileName("lena-gray.jpg");
25 reader->Update();
26
27 int bins = 16;
28 int comps = 1;
29
30 vtkSmartPointer<vtkImageAccumulate> histogram =
31 vtkSmartPointer<vtkImageAccumulate>::New();
32 histogram->SetInputData(reader->GetOutput());
33 histogram->SetComponentExtent(0, bins - 1, 0, 0, 0, 0);
34 histogram->SetComponentOrigin(0, 0, 0);
35 histogram->SetComponentSpacing(256.0 / bins, 0, 0);
36 histogram->Update();
37
38 int* output = static_cast<int*>(histogram->GetOutput()->GetScalarPointer());
39
40 vtkSmartPointer<vtkIntArray> frequencies =
41 vtkSmartPointer<vtkIntArray>::New();
42 frequencies->SetNumberOfComponents(1);
43
44 for (int j = 0; j < bins; ++j)
45 {
46 for (int i = 0; i<comps; i++)
47 {
48 frequencies->InsertNextTuple1(*output++);
49 }
50 }
51
52 vtkSmartPointer<vtkDataObject> dataObject =
53 vtkSmartPointer<vtkDataObject>::New();
54 dataObject->GetFieldData()->AddArray(frequencies);
55
56 vtkSmartPointer<vtkBarChartActor> barChart =
57 vtkSmartPointer<vtkBarChartActor>::New();
58 barChart->SetInput(dataObject);
59 barChart->SetTitle("Histogram");
60 barChart->GetPositionCoordinate()->SetValue(0.05, 0.05, 0.0);
61 barChart->GetPosition2Coordinate()->SetValue(0.95, 0.95, 0.0);
62 barChart->GetProperty()->SetColor(0, 0, 0);
63 barChart->GetTitleTextProperty()->SetColor(0, 0, 0);
64 barChart->GetLabelTextProperty()->SetColor(0, 0, 0);
65 barChart->GetLegendActor()->SetNumberOfEntries(dataObject->GetFieldData()->GetArray(0)->GetNumberOfTuples());
66 barChart->LegendVisibilityOff();
67 barChart->LabelVisibilityOff();
68
69 double colors[3][3] = {
70 { 1, 0, 0 },
71 { 0, 1, 0 },
72 { 0, 0, 1 } };
73
74 int count = 0;
75 for (int i = 0; i < bins; ++i)
76 {
77 for (int j = 0; j < comps; ++j)
78 {
79 barChart->SetBarColor(count++, colors[j]); //单通道 红色
80 }
81 }
82 vtkSmartPointer<vtkImageActor> imgActor =
83 vtkSmartPointer<vtkImageActor>::New();
84 imgActor->SetInputData(reader->GetOutput());
85
86 double imgView[4] = { 0.0, 0.0, 0.5, 1.0 };
87 double barView[4] = { 0.5, 0.0, 1.0, 1.0 };
88 vtkSmartPointer<vtkRenderer> barRender =
89 vtkSmartPointer<vtkRenderer>::New();
90 barRender->SetViewport(barView);
91 barRender->AddActor(barChart);
92 barRender->SetBackground(1.0, 1.0, 1.0);
93
94 vtkSmartPointer<vtkRenderer> imgRender =
95 vtkSmartPointer<vtkRenderer>::New();
96 imgRender->SetViewport(imgView);
97 imgRender->AddActor(imgActor);
98 imgRender->SetBackground(1.0, 1.0, 1.0);
99
100 vtkSmartPointer<vtkRenderWindow> renderWindow =
101 vtkSmartPointer<vtkRenderWindow>::New();
102 renderWindow->AddRenderer(barRender);
103 renderWindow->AddRenderer(imgRender);
104 renderWindow->SetSize(640, 320);
105 renderWindow->Render();
106 renderWindow->SetWindowName("Gray-Image Histogram");
107
108 vtkSmartPointer<vtkRenderWindowInteractor> interactor =
109 vtkSmartPointer<vtkRenderWindowInteractor>::New();
110 interactor->SetRenderWindow(renderWindow);
111
112 interactor->Initialize();
113 interactor->Start();
114
115 return 0;
116 }