参考大神博客:VTK修炼之道30:图像重采样_降采样和升采样技术_vtk 重采样_沈子恒的博客-CSDN博客
上源码:
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkSmartPointer.h>
#include <vtkBMPReader.h>
#include <vtkImageData.h>
#include <vtkImageShrink3D.h> //降采样头文件
#include <vtkImageMagnify.h> //升采样头文件
#include <vtkImageActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h> //交互样式
void main()
{
vtkSmartPointer<vtkJPEGReader> reader = vtkSmartPointer<vtkJPEGReader>::New();
reader->SetFileName("234.jpg");
reader->Update();
//实现图像降采样
vtkSmartPointer<vtkImageShrink3D> vtkShrink = vtkSmartPointer<vtkImageShrink3D>::New();
vtkShrink->SetInputConnection(reader->GetOutputPort());//建立管道
vtkShrink->SetShrinkFactors(16, 16, 1);//设置降采样率,设置X,Y、Z方向的采样率,缩小至1/16
vtkShrink->Update();
//实现图像的升采样
vtkSmartPointer<vtkImageMagnify> vtkMagnify = vtkSmartPointer<vtkImageMagnify>::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<vtkImageActor> origActor = vtkSmartPointer<vtkImageActor>::New();
origActor->SetInputData(reader->GetOutput());
vtkSmartPointer<vtkImageActor> shrinkActor = vtkSmartPointer<vtkImageActor>::New();
shrinkActor->SetInputData(vtkShrink->GetOutput());
vtkSmartPointer<vtkImageActor> magnifyActor = vtkSmartPointer<vtkImageActor>::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<vtkRenderer> origRender = vtkSmartPointer<vtkRenderer>::New();
origRender->SetViewport(origView);
origRender->AddActor(origActor);
origRender->ResetCamera();
origRender->SetBackground(0, 0, 0);
vtkSmartPointer<vtkRenderer> shrinkRender = vtkSmartPointer<vtkRenderer>::New();
shrinkRender->SetViewport(shrinkView);
shrinkRender->AddActor(shrinkActor);
shrinkRender->ResetCamera();
shrinkRender->SetBackground(0, 0, 0);
vtkSmartPointer<vtkRenderer> magnifyRender = vtkSmartPointer<vtkRenderer>::New();
magnifyRender->SetViewport(magnifyView);
magnifyRender->AddActor(magnifyActor);
magnifyRender->ResetCamera();
magnifyRender->SetBackground(0, 0, 0);
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(origRender);
renderWindow->AddRenderer(shrinkRender);
renderWindow->AddRenderer(magnifyRender);
renderWindow->SetSize(720, 240);
renderWindow->Render();
renderWindow->SetWindowName("Shrink-Magnify_Image");
vtkSmartPointer<vtkRenderWindowInteractor> interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkInteractorStyleImage> style = vtkSmartPointer<vtkInteractorStyleImage>::New();
interactor->SetInteractorStyle(style);
interactor->SetRenderWindow(renderWindow);
interactor->Initialize();
interactor->Start();
}
看下效果:
最左边是原图,中间是降低采样率的效果,右边是升高采样率效果