1.VTK中的图像重采样实现
图像重采样是指对采样后形成的由离散数据组成的数字图像按所需的像元位置或像元问距重新采样,以构成几何变换后的新图像。重采样过程本质上是图像恢复过程,它用输入的离散数字图像重建代表原始图像二维连续函数,再按新的像元间距和像元位置进行采样。其数学过程是根据重建的连续函数(曲面),用周围若干像元点的值估计或内插出新采样点的值。图像重采样在图像处理中应用非常广泛,如SIFT特征提取。
图像重采样后图像的维数会发生改变。当重采样图像小于原图像维数时,称为降采样;当重采样图像维数大于原图像时,称为升采样。VTK中可以方便的对图像进行重采样。vtkImageShrink3D类实现图像降采样。降采样需要设置每个方向的采样率,降采样率越大,图像越模糊。升采样的原理与降采样原理一致,只是增加采样点数来增加图像的维数。VTK中vtkImageMagnify来实现图像的升采样。实验代码如下所示:1 #include <vtkAutoInit.h> 2 VTK_MODULE_INIT(vtkRenderingOpenGL); 3 4 #include <vtkSmartPointer.h> 5 #include <vtkBMPReader.h> 6 #include <vtkImageData.h> 7 #include <vtkImageShrink3D.h> //降采样头文件 8 #include <vtkImageMagnify.h> //升采样头文件 9 #include <vtkImageActor.h> 10 #include <vtkRenderer.h> 11 #include <vtkRenderWindow.h> 12 #include <vtkRenderWindowInteractor.h> 13 #include <vtkInteractorStyleImage.h> //交互样式 14 15 16 int main() 17 { 18 vtkSmartPointer<vtkBMPReader> reader = 19 vtkSmartPointer<vtkBMPReader>::New(); 20 reader->SetFileName("lena.bmp"); 21 reader->Update(); 22 23 vtkSmartPointer<vtkImageShrink3D> shrinkFilter = 24 vtkSmartPointer<vtkImageShrink3D>::New(); 25 shrinkFilter->SetInputConnection(reader->GetOutputPort());//建立管道 26 shrinkFilter->SetShrinkFactors(16, 16, 1);//设置降采样率 27 shrinkFilter->Update(); 28 29 vtkSmartPointer<vtkImageMagnify> magnifyFilter = 30 vtkSmartPointer<vtkImageMagnify>::New(); 31 magnifyFilter->SetInputConnection(reader->GetOutputPort()); 32 magnifyFilter->SetMagnificationFactors(10, 10, 1); 33 magnifyFilter->Update(); 34 35 int originalDims[3]; 36 reader->GetOutput()->GetDimensions(originalDims); 37 double originalSpacing[3]; 38 reader->GetOutput()->GetSpacing(originalSpacing); 39 int shrinkDims[3]; 40 shrinkFilter->GetOutput()->GetDimensions(shrinkDims); 41 double shrinkSpacing[3]; 42 shrinkFilter->GetOutput()->GetSpacing(shrinkSpacing); 43 int magnifyDims[3]; 44 magnifyFilter->GetOutput()->GetDimensions(magnifyDims); 45 double magnifuSpacing[3]; 46 magnifyFilter->GetOutput()->GetSpacing(magnifuSpacing); 47 std::cout << "原始维度: " << originalDims[0] << "*" << originalDims[1] << "*" << originalDims[2] << std::endl; 48 std::cout << "原始像素间距: " << originalSpacing[0] << " " << originalSpacing[1] << " " << originalSpacing[2] << std::endl; 49 std::cout << "降采样维度: " << shrinkDims[0] << "*" << shrinkDims[1] << "*" << shrinkDims[2] << std::endl; 50 std::cout << "降采样像素间距:" << shrinkSpacing[0] << " " << shrinkSpacing[1] << " " << shrinkSpacing[2] << std::endl; 51 std::cout << "升采样维度: " << magnifyDims[0] << "*" << magnifyDims[1] << "*" << magnifyDims[2] << std::endl; 52 std::cout << "升采样像素间距:" << magnifuSpacing[0] << " " << magnifuSpacing[1] << " " << magnifuSpacing[2] << std::endl; 53 54 vtkSmartPointer<vtkImageActor> origActor = 55 vtkSmartPointer<vtkImageActor>::New(); 56 origActor->SetInputData(reader->GetOutput()); 57 vtkSmartPointer<vtkImageActor> shrinkActor = 58 vtkSmartPointer<vtkImageActor>::New(); 59 shrinkActor->SetInputData(shrinkFilter->GetOutput()); 60 vtkSmartPointer<vtkImageActor> magnifyActor = 61 vtkSmartPointer<vtkImageActor>::New(); 62 magnifyActor->SetInputData(magnifyFilter->GetOutput()); 63 /// 64 double origView[4] = { 0, 0, 0.3, 1.0 }; 65 double shrinkView[4] = { 0.3, 0, 0.6, 1.0 }; 66 double magnifyView[4] = { 0.6, 0, 1, 1 }; 67 vtkSmartPointer<vtkRenderer> origRender = 68 vtkSmartPointer<vtkRenderer>::New(); 69 origRender->SetViewport(origView); 70 origRender->AddActor(origActor); 71 origRender->ResetCamera(); 72 origRender->SetBackground(1, 0, 0); 73 vtkSmartPointer<vtkRenderer> shrinkRender = 74 vtkSmartPointer<vtkRenderer>::New(); 75 shrinkRender->SetViewport(shrinkView); 76 shrinkRender->AddActor(shrinkActor); 77 shrinkRender->ResetCamera(); 78 shrinkRender->SetBackground(0, 1, 0); 79 vtkSmartPointer<vtkRenderer> magnifyRender = 80 vtkSmartPointer<vtkRenderer>::New(); 81 magnifyRender->SetViewport(magnifyView); 82 magnifyRender->AddActor(magnifyActor); 83 magnifyRender->ResetCamera(); 84 magnifyRender->SetBackground(0, 0, 1); 85 / 86 vtkSmartPointer<vtkRenderWindow> rw = 87 vtkSmartPointer<vtkRenderWindow>::New(); 88 rw->AddRenderer(origRender); 89 rw->AddRenderer(shrinkRender); 90 rw->AddRenderer(magnifyRender); 91 rw->SetSize(720, 240); 92 rw->SetWindowName("Image-Shrink-Magnify"); 93 rw->Render(); 94 95 vtkSmartPointer<vtkRenderWindowInteractor> rwi = 96 vtkSmartPointer<vtkRenderWindowInteractor>::New(); 97 vtkSmartPointer<vtkInteractorStyleImage> style = 98 vtkSmartPointer<vtkInteractorStyleImage>::New(); 99 rwi->SetInteractorStyle(style); 100 rwi->SetRenderWindow(rw); 101 rwi->Initialize(); 102 rwi->Start(); 103 104 return 0; 105 }
vtkImageShrink3D通过SetShrinkFactors()设置X、Y和Z方向的采样率,参数为三个int类型数据。vtkImageMagnify通过SetMagnificationFactors设置相应的放大采样率。上面例子中分别对原图像维数缩小原来的1/16和放大为原来的10倍,执行结果如下:从图中可以看成,重采样图像已经变得十分模糊了。而升采样图像则变化不大。程序中输出了原图和重采样图像的维数和像素间隔,从输出来看,原图像的维数为512x512,缩小16倍后,图像维数变为32x32。需要注意的是,原图的像素间隔为(1,1,1),而重采样后像素间隔变为(16, 16, 1),这是因为采样是在世界坐标系下进行的,当采样图像的维数减小时,采样的像素间隔也相应的变大,这样保持图像的区域不会发生改变。同样的道理,当升采样后图像的维数变为原来的10倍,而像素间隔变为原来的十分之一。
另外,图像的重采样类有:vtkImageResample。