1 #include <vtkAutoInit.h>
2 VTK_MODULE_INIT(vtkRenderingOpenGL);
3
4 #include <vtkSmartPointer.h>
5 #include <vtkBMPReader.h>
6 #include <vtkImageData.h>
7 #include <vtkXYPlotActor.h>
8 #include <vtkProperty2D.h>
9 #include <vtkTextProperty.h>
10 #include <vtkImageExtractComponents.h>
11 #include <vtkImageAccumulate.h>
12
13 #include <vtkImageActor.h>
14 #include <vtkRenderer.h>
15 #include <vtkRenderWindow.h>
16 #include <vtkRenderWindowInteractor.h>
17
18 int main()
19 {
20 vtkSmartPointer<vtkBMPReader> reader =
21 vtkSmartPointer<vtkBMPReader>::New();
22 reader->SetFileName("lena.bmp");
23 reader->Update();
24
25 int numComponents = reader->GetOutput()->GetNumberOfScalarComponents();
26
27 //
28 vtkSmartPointer<vtkXYPlotActor> plot =
29 vtkSmartPointer<vtkXYPlotActor>::New();
30 plot->ExchangeAxesOff();
31 plot->SetLabelFormat("%g");
32 plot->SetXTitle("灰度值");
33 plot->SetYTitle("像素频率");
34 plot->SetXValuesToValue();
35 plot->GetProperty()->SetColor(1.0, 1.0, 1.0);
36 plot->GetAxisLabelTextProperty()->SetColor(0, 0, 0);
37 plot->GetAxisTitleTextProperty()->SetColor(0, 0, 0);
38
39 double colors[3][3] = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };
40 const char* labels[3] = { "Red", "Green", "Blue" };
41 int xmax = 0;
42 int ymax = 0;
43 for (int i = 0; i < numComponents; ++i)
44 {
45 vtkSmartPointer<vtkImageExtractComponents> extract =
46 vtkSmartPointer<vtkImageExtractComponents>::New();
47 extract->SetInputConnection(reader->GetOutputPort());
48 extract->SetComponents(i);
49 extract->Update();
50
51 double range[2];
52 extract->GetOutput()->GetScalarRange(range);
53 int extent = static_cast<int> (range[1]) - static_cast<int>(range[0]) - 1;
54
55 vtkSmartPointer<vtkImageAccumulate> histogram =
56 vtkSmartPointer<vtkImageAccumulate>::New();
57 histogram->SetInputConnection(reader->GetOutputPort());
58 histogram->SetComponentExtent(0,extent, 0, 0, 0, 0);
59 histogram->SetComponentOrigin(range[0], 0, 0);
60 histogram->SetComponentSpacing(1, 0, 0);
61 histogram->SetIgnoreZero(1);
62 histogram->Update();
63
64 if (range[1] > xmax)
65 {
66 xmax = range[1];
67 }
68 if (histogram->GetOutput()->GetScalarRange()[1] > ymax)
69 {
70 ymax = histogram->GetOutput()->GetScalarRange()[1];
71 }
72 plot->AddDataSetInput(histogram->GetOutput());
73 plot->SetPlotColor(i, colors[i]);
74 plot->SetPlotLabel(i, labels[i]);
75 plot->LegendOn();
76 }
77 plot->SetXRange(0, xmax);
78 plot->SetYRange(0, ymax);
79 /
80 vtkSmartPointer<vtkImageActor> imgActor =
81 vtkSmartPointer<vtkImageActor>::New();
82 imgActor->SetInputData(reader->GetOutput());
83
84 double imgView[4] = { 0.0, 0.0, 0.5, 1.0 };
85 double histView[4] = { 0.5, 0.0, 1.0, 1.0 };
86 vtkSmartPointer<vtkRenderer> imgRender =
87 vtkSmartPointer<vtkRenderer>::New();
88 imgRender->SetViewport(imgView);
89 imgRender->AddActor(imgActor);
90 imgRender->SetBackground(1.0, 0.0, 0.0);
91
92 vtkSmartPointer<vtkRenderer> histRender =
93 vtkSmartPointer<vtkRenderer>::New();
94 histRender->SetViewport(histView);
95 histRender->AddActor(plot);
96 histRender->SetBackground(1.0, 1.0, 1.0);
97 /
98 vtkSmartPointer<vtkRenderWindow> rw =
99 vtkSmartPointer<vtkRenderWindow>::New();
100 rw->AddRenderer(imgRender);
101 rw->AddRenderer(histRender);
102 rw->SetSize(640, 320);
103 rw->SetWindowName("RGB-Image Histogram");
104
105 vtkSmartPointer<vtkRenderWindowInteractor> rwi =
106 vtkSmartPointer<vtkRenderWindowInteractor>::New();
107 rwi->SetRenderWindow(rw);
108 rwi->Initialize();
109
110 rwi->Start();
111 return 0;
112 }