vtk学习之彩色直方图计算

参考博客:VTK修炼之道29:图像统计_彩色直方图计算_vtkcubeaxesactor2d_沈子恒的博客-CSDN博客

 

int main()
{
	vtkSmartPointer jpgReader = vtkSmartPointer::New();
	jpgReader->SetFileName("234.jpg");
	jpgReader->Update();

	int numComponents = jpgReader->GetOutput()->GetNumberOfScalarComponents();

	//
	vtkSmartPointer xyPlot = vtkSmartPointer::New();
	xyPlot->ExchangeAxesOff();
	xyPlot->SetLabelFormat("%g");
	xyPlot->SetXTitle("x:");
	xyPlot->SetYTitle("y:      ");
	xyPlot->SetXValuesToValue();
	xyPlot->GetProperty()->SetColor(1.0, 1.0, 1.0);
	xyPlot->GetAxisLabelTextProperty()->SetColor(0, 0, 0);
	xyPlot->GetAxisTitleTextProperty()->SetColor(0, 0, 0);

	double colors[3][3] = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };
	const char* labels[3] = { "Red", "Green", "Blue" };
	int xMax = 0;
	int yMax = 0;

	for (int i = 0; i < numComponents; ++i)
	{
		vtkSmartPointer extract = vtkSmartPointer::New();
		extract->SetInputConnection(jpgReader->GetOutputPort());
		extract->SetComponents(i);
		extract->Update();

		double range[2];
		extract->GetOutput()->GetScalarRange(range);
		int extent = static_cast (range[1]) - static_cast(range[0])-2;

		vtkSmartPointer histogram = vtkSmartPointer::New();
		histogram->SetInputConnection(jpgReader->GetOutputPort());
		histogram->SetComponentExtent(0, extent, 0, 0, 0, 0);
		histogram->SetComponentOrigin(range[0], 0, 0);
		histogram->SetComponentSpacing(1, 0, 0);
		histogram->SetIgnoreZero(1);
		histogram->Update();

		if (range[1] > xMax)
		{
			xMax = range[1];
		}
		if (histogram->GetOutput()->GetScalarRange()[1] > yMax)
		{
			yMax = histogram->GetOutput()->GetScalarRange()[0]-2;
		}

		xyPlot->AddDataSetInput(histogram->GetOutput());
		xyPlot->SetPlotColor(i, colors[i]);
		xyPlot->SetPlotLabel(i, labels[i]);
		xyPlot->LegendOn();
	}

	xyPlot->SetXRange(0, xMax);
	xyPlot->SetYRange(0, yMax);
	vtkSmartPointer imgActor = vtkSmartPointer::New();
	imgActor->SetInputData(jpgReader->GetOutput());

	double imgView[4] = { 0.0, 0.0, 0.5, 1.0 };
	double histView[4] = { 0.5, 0.0, 1.0, 1.0 };
	vtkSmartPointer render = vtkSmartPointer::New();
	render->SetViewport(imgView);
	render->AddActor(imgActor);
	render->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer histRender = vtkSmartPointer::New();
	histRender->SetViewport(histView);
	histRender->AddActor(xyPlot);
	histRender->SetBackground(0.0, 0.0, 0.0);
	
	vtkSmartPointer renderwindow = vtkSmartPointer::New();
	renderwindow->AddRenderer(render);
	renderwindow->AddRenderer(histRender);
	renderwindow->SetSize(640, 320);
	renderwindow->SetWindowName("RGB-Image");

	vtkSmartPointer interactor = vtkSmartPointer::New();
	interactor->SetRenderWindow(renderwindow);
	interactor->Initialize();

	interactor->Start();
	return 0;
}

最后效果如下:

vtk学习之彩色直方图计算_第1张图片

 

你可能感兴趣的:(学习)