1 #include "vtkAutoInit.h" 2 VTK_MODULE_INIT(vtkRenderingOpenGL2); 3 VTK_MODULE_INIT(vtkInteractionStyle); 4 5 #include <vtkActor.h> 6 #include <vtkBarChartActor.h> 7 #include <vtkFieldData.h> 8 #include <vtkImageAccumulate.h> 9 #include <vtkImageData.h> 10 #include <vtkIntArray.h> 11 #include <vtkJPEGReader.h> 12 #include <vtkLegendBoxActor.h> 13 #include <vtkProperty2D.h> 14 #include <vtkRenderer.h> 15 #include <vtkRenderWindow.h> 16 #include <vtkRenderWindowInteractor.h> 17 #include <vtkSmartPointer.h> 18 #include <vtkTextProperty.h> 19 20 //测试图像:../data/lena-gray.jpg 21 int main(int argc, char* argv[]) 22 { 23 24 vtkSmartPointer<vtkJPEGReader> reader = 25 vtkSmartPointer<vtkJPEGReader>::New(); 26 reader->SetFileName ( "lena-gray.jpg" ); 27 reader->Update(); 28 29 int bins = 16; 30 int comps = 1; 31 32 vtkSmartPointer<vtkImageAccumulate> histogram = 33 vtkSmartPointer<vtkImageAccumulate>::New(); 34 histogram->SetInputData(reader->GetOutput()); 35 histogram->SetComponentExtent(0, bins-1, 0, 0, 0, 0); 36 histogram->SetComponentOrigin(0, 0, 0); 37 histogram->SetComponentSpacing(256.0/bins, 0, 0); 38 histogram->Update(); 39 40 int* output = static_cast<int*>(histogram->GetOutput()->GetScalarPointer()); 41 42 vtkSmartPointer<vtkIntArray> frequencies = 43 vtkSmartPointer<vtkIntArray>::New(); 44 frequencies->SetNumberOfComponents(1); 45 46 for(int j = 0; j < bins; ++j) 47 { 48 for(int i=0; i<comps; i++) 49 { 50 frequencies->InsertNextTuple1(*output++); 51 } 52 } 53 54 vtkSmartPointer<vtkDataObject> dataObject = 55 vtkSmartPointer<vtkDataObject>::New(); 56 dataObject->GetFieldData()->AddArray( frequencies ); 57 58 vtkSmartPointer<vtkBarChartActor> barChart = 59 vtkSmartPointer<vtkBarChartActor>::New(); 60 barChart->SetInput(dataObject); 61 barChart->SetTitle("Histogram"); 62 barChart->GetPositionCoordinate()->SetValue(0.05,0.05,0.0); 63 barChart->GetPosition2Coordinate()->SetValue(0.95,0.95,0.0); 64 barChart->GetProperty()->SetColor(0,0,0); 65 barChart->GetTitleTextProperty()->SetColor(0,0,0); 66 barChart->GetLabelTextProperty()->SetColor(0,0,0); 67 barChart->GetLegendActor()->SetNumberOfEntries(dataObject->GetFieldData()->GetArray(0)->GetNumberOfTuples()); 68 barChart->LegendVisibilityOff(); 69 barChart->LabelVisibilityOff(); 70 71 double colors[3][3] = { 72 { 1, 0, 0 }, 73 { 0, 1, 0 }, 74 { 0, 0, 1 } }; 75 76 int count = 0; 77 for( int i = 0; i < bins; ++i ) 78 { 79 for( int j = 0; j < comps; ++j ) 80 { 81 barChart->SetBarColor( count++, colors[j] ); 82 } 83 } 84 85 vtkSmartPointer<vtkRenderer> renderer = 86 vtkSmartPointer<vtkRenderer>::New(); 87 renderer->AddActor(barChart); 88 renderer->SetBackground(1.0, 1.0, 1.0); 89 90 vtkSmartPointer<vtkRenderWindow> renderWindow = 91 vtkSmartPointer<vtkRenderWindow>::New(); 92 renderWindow->AddRenderer(renderer); 93 renderWindow->SetSize(640, 480); 94 renderWindow->Render(); 95 renderWindow->SetWindowName("ImageAccumulateExample"); 96 97 vtkSmartPointer<vtkRenderWindowInteractor> interactor = 98 vtkSmartPointer<vtkRenderWindowInteractor>::New(); 99 interactor->SetRenderWindow(renderWindow); 100 101 interactor->Initialize(); 102 interactor->Start(); 103 104 return EXIT_SUCCESS; 105 }标签:int,VTK,histogram,vtkSmartPointer,barChart,直方图,灰度,New,include From: https://www.cnblogs.com/ybqjymy/p/17630916.html