VTK学习之图像降采样、升采样(降低像素、提高像素)

参考大神博客:VTK修炼之道30:图像重采样_降采样和升采样技术_vtk 重采样_沈子恒的博客-CSDN博客

上源码:

#include 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

#include 
#include 
#include 
#include  //降采样头文件
#include   //升采样头文件
#include 
#include 
#include 
#include 
#include  //交互样式


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

	//实现图像降采样
	vtkSmartPointer vtkShrink = vtkSmartPointer::New();
	vtkShrink->SetInputConnection(reader->GetOutputPort());//建立管道
	vtkShrink->SetShrinkFactors(16, 16, 1);//设置降采样率,设置X,Y、Z方向的采样率,缩小至1/16
	vtkShrink->Update();

	//实现图像的升采样
	vtkSmartPointer vtkMagnify = vtkSmartPointer::New();
	vtkMagnify->SetInputConnection(reader->GetOutputPort());
	vtkMagnify->SetMagnificationFactors(10, 10, 1);//放大采样率,放大10倍
	vtkMagnify->Update();

	int originalDims[3];
	reader->GetOutput()->GetDimensions(originalDims);//原始维度
	double originalSpacing[3];
	reader->GetOutput()->GetSpacing(originalSpacing);//原始像素间距
	int shrinkDims[3];
	vtkShrink->GetOutput()->GetDimensions(shrinkDims);//降采样维度
	double shrinkSpacing[3];
	vtkShrink->GetOutput()->GetSpacing(shrinkSpacing);//降采样像素间距
	int magnifyDims[3];
	vtkMagnify->GetOutput()->GetDimensions(magnifyDims);//升采样维度
	double magnifuSpacing[3];
	vtkMagnify->GetOutput()->GetSpacing(magnifuSpacing);//升采样像素间距

	std::cout << "原始维度:      " << originalDims[0] << "*" << originalDims[1] << "*" << originalDims[2] << std::endl;
	std::cout << "原始像素间距:  " << originalSpacing[0] << " " << originalSpacing[1] << " " << originalSpacing[2] << std::endl;
	std::cout << "降采样维度:    " << shrinkDims[0] << "*" << shrinkDims[1] << "*" << shrinkDims[2] << std::endl;
	std::cout << "降采样像素间距:" << shrinkSpacing[0] << " " << shrinkSpacing[1] << " " << shrinkSpacing[2] << std::endl;
	std::cout << "升采样维度:    " << magnifyDims[0] << "*" << magnifyDims[1] << "*" << magnifyDims[2] << std::endl;
	std::cout << "升采样像素间距:" << magnifuSpacing[0] << " " << magnifuSpacing[1] << " " << magnifuSpacing[2] << std::endl;

	vtkSmartPointer origActor = vtkSmartPointer::New();
	origActor->SetInputData(reader->GetOutput());
	vtkSmartPointer shrinkActor = vtkSmartPointer::New();
	shrinkActor->SetInputData(vtkShrink->GetOutput());
	vtkSmartPointer magnifyActor = vtkSmartPointer::New();
	magnifyActor->SetInputData(vtkMagnify->GetOutput());
	
	///
	double origView[4] = { 0, 0, 0.3, 1.0 };
	double shrinkView[4] = { 0.3, 0, 0.6, 1.0 };
	double magnifyView[4] = { 0.6, 0, 1, 1 };
	vtkSmartPointer origRender = vtkSmartPointer::New();
	origRender->SetViewport(origView);
	origRender->AddActor(origActor);
	origRender->ResetCamera();
	origRender->SetBackground(0, 0, 0);
	vtkSmartPointer shrinkRender = vtkSmartPointer::New();
	shrinkRender->SetViewport(shrinkView);
	shrinkRender->AddActor(shrinkActor);
	shrinkRender->ResetCamera();
	shrinkRender->SetBackground(0, 0, 0);
	vtkSmartPointer magnifyRender = vtkSmartPointer::New();
	magnifyRender->SetViewport(magnifyView);
	magnifyRender->AddActor(magnifyActor);
	magnifyRender->ResetCamera();
	magnifyRender->SetBackground(0, 0, 0);
	
	vtkSmartPointer renderWindow = vtkSmartPointer::New();
	renderWindow->AddRenderer(origRender);
	renderWindow->AddRenderer(shrinkRender);
	renderWindow->AddRenderer(magnifyRender);
	renderWindow->SetSize(720, 240);
    renderWindow->Render();
    renderWindow->SetWindowName("Shrink-Magnify_Image");
	
	vtkSmartPointer interactor = vtkSmartPointer::New();
	vtkSmartPointer style = vtkSmartPointer::New();
	interactor->SetInteractorStyle(style);
	interactor->SetRenderWindow(renderWindow);
	interactor->Initialize();
	interactor->Start();
}

看下效果:

VTK学习之图像降采样、升采样(降低像素、提高像素)_第1张图片

 最左边是原图,中间是降低采样率的效果,右边是升高采样率效果

你可能感兴趣的:(vtk,学习,人工智能)