vtkImageBlend:处理一个窗口中显示多个图像(图像融合技术),接收多个图像输入,输出为融合图像。
注:此文知识学习笔记,仅记录完整程序和实现结果,具体原理参见:
https://blog.csdn.net/www_doling_net/article/details/8541534
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);
#include <vtkSmartPointer.h>
#include <vtkJPEGReader.h>
//#include <vtkImageCast.h>
#include <vtkImageData.h>
#include <vtkImageCanvasSource2D.h>
#include <vtkImageBlend.h>
#include <vtkImageActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
int main()
{
//数据管线
vtkSmartPointer<vtkJPEGReader> reader = vtkSmartPointer<vtkJPEGReader>::New();
reader->SetFileName("data/lena-gray.jpg");
reader->Update();
vtkSmartPointer<vtkImageCanvasSource2D> source = vtkSmartPointer<vtkImageCanvasSource2D>::New();
source->SetNumberOfScalarComponents(1);
source->SetScalarTypeToUnsignedChar();
source->SetExtent(0, 512, 0, 512, 0, 0);
source->SetDrawColor(0, 0, 0);
source->FillBox(0, 512, 0, 512);
source->SetDrawColor(255, 255, 255);
source->FillBox(100, 400, 100, 400);
source->Update();
vtkSmartPointer<vtkImageBlend> blend = vtkSmartPointer<vtkImageBlend>::New();
blend->AddInputData( reader->GetOutput()); //书中这里是SetInputData, 0,1 index删掉
blend->AddInputData( source->GetOutput()); //
blend->SetOpacity(0, 0.4);
blend->SetOpacity(1, 0.6);
blend->Update();
//渲染引擎
vtkSmartPointer<vtkImageActor> actor1 = vtkSmartPointer<vtkImageActor>::New();
actor1->SetInputData(reader->GetOutput());
vtkSmartPointer<vtkImageActor> actor2 = vtkSmartPointer<vtkImageActor>::New();
actor2->SetInputData(source->GetOutput());
vtkSmartPointer<vtkImageActor> blend_actor = vtkSmartPointer<vtkImageActor>::New();
blend_actor->SetInputData(blend->GetOutput());
//定义视窗大小(xmin.ymin,xmax,ymax)
//按window的尺寸进行比例分割
double leftViewport[4] = { 0, 0, 0.33, 1 };
double midViewport[4] = { 0.33, 0, 0.66, 1 };
double rightViewport[4] = { 0.66, 0, 1, 1 };
//render
vtkSmartPointer<vtkRenderer> render1 = vtkSmartPointer<vtkRenderer>::New();
render1->SetViewport(leftViewport);
render1->AddActor(actor1);
render1->ResetCamera();
render1->SetBackground(1, 1, 1);
vtkSmartPointer<vtkRenderer> render2 = vtkSmartPointer<vtkRenderer>::New();
render2->SetViewport(midViewport);
render2->AddActor(actor2);
render2->ResetCamera();
render2->SetBackground(1, 1, 1);
vtkSmartPointer<vtkRenderer> render3 = vtkSmartPointer<vtkRenderer>::New();
render3->SetViewport(rightViewport);
render3->AddActor(blend_actor);
render3->ResetCamera();
render3->SetBackground(1, 1, 1);
//window
vtkSmartPointer<vtkRenderWindow> renderwindow = vtkSmartPointer<vtkRenderWindow>::New();
renderwindow->AddRenderer(render1);
renderwindow->AddRenderer(render2);
renderwindow->AddRenderer(render3);
renderwindow->SetSize(640, 320);
renderwindow->SetWindowName("Image-Fusion");
renderwindow->Render();
//interactor
vtkSmartPointer<vtkRenderWindowInteractor> rwi = vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkInteractorStyleImage> style = vtkSmartPointer<vtkInteractorStyleImage>::New();
rwi->SetInteractorStyle(style);
rwi->SetRenderWindow(renderwindow);
rwi->Initialize();
rwi->Start();
return 0;
}
运行结果:
参考资料:
1.《The Visualization Toolkit – AnObject-Oriented Approach To 3D Graphics (4th Edition)》
2. 张晓东, 罗火灵. VTK图形图像开发进阶[M]. 机械工业出版社, 2015.