VTK资料分享:张晓东的《VTK图形图像开发进阶》全套资料(包含书籍电子版,配套每章节所有源码、实现效果图和工程配置文件)
水灵VTK学习全套资料(包含全部视频、PPT、源码和每个项目对应的工程配置文件CmakeLists.txt)
水灵VT视频在线观看
本文参考的主要是张晓东的《VTK图形图像开发进阶》和水灵的视频Study VTK Together。使用的平台是VS2019+VTK8.2
VTK系列目录:
1 VTK基本概念
2 VTK图像处理
3 VTK图形处理
4 VTK体绘制
2 VTK图像处理
实例19:用vtkImageCanvasSource2D创建图像
实例20:用vtkImageViewer2显示三维医学图像mhd的某个切面
实例21:BMP图像信息的访问(图像维数、原点坐标和像素间隔)
实例22:JPG图像信息的访问(图像维数、原点坐标和像素间隔)
实例23:DCM医学图像信息的访问(图像维数、原点坐标和像素间隔)
实例24:BMP图像信息的修改(图像维数、原点坐标和像素间隔)
实例25:BMP图像像素值的访问与修改
实例26:将BMP类型的RGB图像转换为灰度图像
实例27:将BMP类型的RGB图像提取各个颜色(R、G、B)组分(提取颜色组分)
实例28:将JPG类型的灰度图像进行彩色映射(图像彩色映射)
实例29:多个灰度图像合成一个彩色图像(颜色合成)
实例30:提取BMP图像的感兴趣区域(区域提取)
实例31:灰度图像直方图
实例32:图像重采样(降采样)
实例33:图像重采样(降和升采样)
实例34:图像运算(数字运算)
实例35:图像运算(逻辑运算)
实例36:图像二值化
实例37:梯度算子(边缘检测)
实例38:Sobel梯度算子(边缘检测)
实例39:拉普拉斯算子(边缘检测)
实例40:均值滤波(图像光滑)
实例41:高斯平滑(图像光滑)
实例42:中值滤波(图像光滑)
实例43:各向异性滤波(图像光滑)
实例44:二维图像快速傅里叶变换(频域处理)
实例45:理想低通滤波器(频域处理)
实例46:巴特沃斯低通滤波器(频域处理)
实例47:理想高通滤波器(频域处理)
实例48:巴沃斯特高通滤波器(频域处理)
实例19:用vtkImageCanvasSource2D创建图像
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkSmartPointer.h>
#include <vtkImageData.h>
#include <vtkImageCanvasSource2D.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkRenderer.h>
#include <vtkImageActor.h>
int main()
{
//定义一个vtkImageCanvasSource2D类型对象canvas
vtkSmartPointer<vtkImageCanvasSource2D> canvas =
vtkSmartPointer<vtkImageCanvasSource2D>::New();
//设置画布的像素数据类型为UnsignedChar
canvas->SetScalarTypeToUnsignedChar();
//设置组分数目
canvas->SetNumberOfScalarComponents(1);
//设置画布的大小
canvas->SetExtent(0, 100, 0, 100, 0, 0);
//利用SetDrawColor方法设置绘制矩形颜色
canvas->SetDrawColor(0, 0, 0, 0);//黑色
//利用FillBox方法在画布中绘制矩形
canvas->FillBox(0,100,0,100);
canvas->SetDrawColor(255, 0, 0, 0);//白色
canvas->FillBox(20,40,20,40);
canvas->Update();
// Create actors
vtkSmartPointer<vtkImageActor> redActor =
vtkSmartPointer<vtkImageActor>::New();
//redActor->SetInput(canvas->GetOutput());
redActor->SetInputData(canvas->GetOutput());
// Define viewport ranges
// (xmin, ymin, xmax, ymax)
double redViewport[4] = {0.0, 0.0, 1.0, 1.0};
// Setup renderers
vtkSmartPointer<vtkRenderer> redRenderer =
vtkSmartPointer<vtkRenderer>::New();
redRenderer->SetViewport(redViewport);
redRenderer->AddActor(redActor);
redRenderer->ResetCamera();
redRenderer->SetBackground(1.0, 1.0, 1.0);
// Setup render window
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(redRenderer);
renderWindow->SetSize( 640, 480 );
renderWindow->Render();
renderWindow->SetWindowName("ImageCanvasSource2D");
// Setup render window interactor
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkInteractorStyleImage> style =
vtkSmartPointer<vtkInteractorStyleImage>::New();
renderWindowInteractor->SetInteractorStyle(style);
// Render and start interaction
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
实例20:用vtkImageViewer2显示三维医学图像mhd的某个切面
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkSmartPointer.h>
#include <vtkImageViewer2.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkMetaImageReader.h>//mhd类图像处理类
//测试图像:../data/brain.mhd
int main(int argc, char* argv[])
{
vtkSmartPointer<vtkMetaImageReader> reader =
vtkSmartPointer<vtkMetaImageReader>::New();
//注意:mhd和raw文件要放在同一个文件目录
reader->SetFileName("C:\\Users\\Administrator\\Desktop\\VTK2\\hellovtk\\vtk_图像处理学习\\第五章_图像处理\\data\\brain.mhd");
reader->Update();
vtkSmartPointer<vtkImageViewer2> imageViewer =
vtkSmartPointer<vtkImageViewer2>::New();
imageViewer->SetInputData(reader->GetOutput());
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
imageViewer->SetupInteractor(renderWindowInteractor);
imageViewer->SetColorLevel(500);//设置窗位为500
imageViewer->SetColorWindow(2000);//设置窗宽为2000
imageViewer->SetSlice(40);//设置切片索引
imageViewer->SetSliceOrientationToXY();//设置切片方向
imageViewer->Render();
imageViewer->GetRenderer()->SetBackground(1.0, 1.0, 1.0);
imageViewer->SetSize(640, 480);
imageViewer->GetRenderWindow()->SetWindowName("DisplayImageExample");
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
实例21:BMP图像信息的访问(图像维数、原点坐标和像素间隔)
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkSmartPointer.h>
#include <vtkImageViewer2.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkImageReader2Factory.h>
#include <vtkImageReader2.h>
#include <vtkBMPReader.h>
#include <vtkImageData.h>
//测试图像:../data/lena.bmp
int main(int argc, char* argv[])
{
/*if (argc < 2)
{
std::cout<<argv[0]<<" "<<"ImageFile(*.bmp)"<<std::endl;
return EXIT_FAILURE;
}*/
vtkSmartPointer<vtkBMPReader> reader =
vtkSmartPointer<vtkBMPReader>::New();
//reader->SetFileName ( argv[1] );
reader->SetFileName("1234.bmp");//读取图片
reader->Update();
int dims[3];
reader->GetOutput()->GetDimensions(dims);
std::cout<<"图像维数:" <<dims[0]<<" "<<dims[1]<<" "<<dims[2]<<std::endl;
double origin[3];
reader->GetOutput()->GetOrigin(origin);
std::cout<<"图像原点:" <<origin[0]<<" "<<origin[1]<<" "<<origin[2]<<std::endl;
double spaceing[3];
reader->GetOutput()->GetSpacing(spaceing);
std::cout<<"像素间隔:" <<spaceing[0]<<" "<<spaceing[1]<<" "<<spaceing[2]<<std::endl;
vtkSmartPointer<vtkImageViewer2> imageViewer =
vtkSmartPointer<vtkImageViewer2>::New();
imageViewer->SetInputConnection(reader->GetOutputPort());
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
imageViewer->SetupInteractor(renderWindowInteractor);
imageViewer->Render();
imageViewer->GetRenderer()->ResetCamera();
imageViewer->Render();
imageViewer->GetRenderer()->SetBackground(1.0, 1.0, 1.0);
imageViewer->SetSize(640, 480);
imageViewer->GetRenderWindow()->SetWindowName("GetImageInformationExample");
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
实例22:JPG图像信息的访问(图像维数、原点坐标和像素间隔)
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkSmartPointer.h>
#include <vtkImageViewer2.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkImageReader2Factory.h>
#include <vtkImageReader2.h>
#include <vtkJPEGReader.h>//JPEG图像读取类
#include <vtkImageData.h>
//测试图像:../data/lena.jpg
int main(int argc, char* argv[])
{
//实例化一个对象dcmReader
vtkJPEGReader* reader = vtkJPEGReader::New();
reader->SetFileName("lena.jpg");//读取图像
reader->Update();
int dims[3];
reader->GetOutput()->GetDimensions(dims);
std::cout << "图像维数:" << dims[0] << " " << dims[1] << " " << dims[2] << std::endl;
double origin[3];
reader->GetOutput()->GetOrigin(origin);
std::cout << "图像原点:" << origin[0] << " " << origin[1] << " " << origin[2] << std::endl;
double spaceing[3];
reader->GetOutput()->GetSpacing(spaceing);
std::cout << "像素间隔:" << spaceing[0] << " " << spaceing[1] << " " << spaceing[2] << std::endl;
vtkSmartPointer<vtkImageViewer2> imageViewer =
vtkSmartPointer<vtkImageViewer2>::New();
imageViewer->SetInputConnection(reader->GetOutputPort());
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
imageViewer->SetupInteractor(renderWindowInteractor);
imageViewer->Render();
imageViewer->GetRenderer()->ResetCamera();
imageViewer->Render();
imageViewer->GetRenderer()->SetBackground(1.0, 1.0, 1.0);
imageViewer->SetSize(640, 480);
imageViewer->GetRenderWindow()->SetWindowName("GetImageInformationExample");
renderWindowInteractor->Start();
return 0;
}
实例23:DCM医学图像信息的访问(图像维数、原点坐标和像素间隔)
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkSmartPointer.h>
#include <vtkImageViewer2.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkImageReader2Factory.h>
#include <vtkImageReader2.h>
//#include <vtkBMPReader.h>
#include <vtkImageData.h>
#include <vtkDICOMImageReader.h>//DCM医学文件读取类
//测试图像:../data/lena.bmp
int main(int argc, char* argv[])
{
/*vtkSmartPointer<vtkBMPReader> reader =
vtkSmartPointer<vtkBMPReader>::New();*/
//实例化一个对象dcmReader
vtkDICOMImageReader* reader = vtkDICOMImageReader::New();
reader->SetFileName("C:\\Users\\Administrator\\Desktop\\VTK2\\hellovtk\\CT\\123.dcm");//读单张切片
//reader->SetFileName("lena.bmp");//读取图片
reader->Update();
int dims[3];
reader->GetOutput()->GetDimensions(dims);
std::cout<<"图像维数:" <<dims[0]<<" "<<dims[1]<<" "<<dims[2]<<std::endl;
double origin[3];
reader->GetOutput()->GetOrigin(origin);
std::cout<<"图像原点:" <<origin[0]<<" "<<origin[1]<<" "<<origin[2]<<std::endl;
double spaceing[3];
reader->GetOutput()->GetSpacing(spaceing);
std::cout<<"像素间隔:" <<spaceing[0]<<" "<<spaceing[1]<<" "<<spaceing[2]<<std::endl;
vtkSmartPointer<vtkImageViewer2> imageViewer =
vtkSmartPointer<vtkImageViewer2>::New();
imageViewer->SetInputConnection(reader->GetOutputPort());
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
imageViewer->SetupInteractor(renderWindowInteractor);
imageViewer->Render();
imageViewer->GetRenderer()->ResetCamera();
imageViewer->Render();
imageViewer->GetRenderer()->SetBackground(1.0, 1.0, 1.0);
imageViewer->SetSize(640, 480);
imageViewer->GetRenderWindow()->SetWindowName("GetImageInformationExample");
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
实例24:BMP图像信息的修改(图像维数、原点坐标和像素间隔)
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkSmartPointer.h>
#include <vtkImageViewer2.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkBMPReader.h>
#include <vtkImageChangeInformation.h>
#include <vtkImageData.h>
//测试图像:../data/lena.bmp
int main(int argc, char* argv[])
{
/*if (argc < 2)
{
std::cout<<argv[0]<<" "<<"ImageFile(*.bmp)"<<std::endl;
return EXIT_FAILURE;
}*/
vtkSmartPointer<vtkBMPReader> reader =
vtkSmartPointer<vtkBMPReader>::New();
//reader->SetFileName(argv[1]);
reader->SetFileName("lena.bmp");
reader->Update();
int dims[3];
double origin[3];
double spaceing[3];
reader->GetOutput()->GetDimensions(dims);
std::cout<<"原图像维数:" <<dims[0]<<" "<<dims[1]<<" "<<dims[2]<<std::endl;
reader->GetOutput()->GetOrigin(origin);
std::cout<<"原图像原点:" <<origin[0]<<" "<<origin[1]<<" "<<origin[2]<<std::endl;
reader->GetOutput()->GetSpacing(spaceing);
std::cout<<"原像素间隔:" <<spaceing[0]<<" "<<spaceing[1]<<" "<<spaceing[2]<<std::endl<<std::endl;;
vtkSmartPointer<vtkImageChangeInformation> changer =
vtkSmartPointer<vtkImageChangeInformation>::New();
changer->SetInputData(reader->GetOutput());//
changer->SetOutputOrigin(100, 100, 0);
changer->SetOutputSpacing(5,5,1);
changer->SetCenterImage(1);
changer->Update();
changer->GetOutput()->GetDimensions(dims);
std::cout<<"修改后图像维数:" <<dims[0]<<" "<<dims[1]<<" "<<dims[2]<<std::endl;
changer->GetOutput()->GetOrigin(origin);
std::cout<<"修改后图像原点:" <<origin[0]<<" "<<origin[1]<<" "<<origin[2]<<std::endl;
changer->GetOutput()->GetSpacing(spaceing);
std::cout<<"修改后像素间隔:" <<spaceing[0]<<" "<<spaceing[1]<<" "<<spaceing[2]<<std::endl;
vtkSmartPointer<vtkImageViewer2> imageViewer =
vtkSmartPointer<vtkImageViewer2>::New();
imageViewer->SetInputConnection(changer->GetOutputPort());
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
imageViewer->SetupInteractor(renderWindowInteractor);
imageViewer->Render();
imageViewer->GetRenderer()->ResetCamera();
imageViewer->Render();
imageViewer->GetRenderer()->SetBackground(1.0, 1.0, 1.0);
imageViewer->SetSize(640, 480);
imageViewer->GetRenderWindow()->SetWindowName("ImageChangeInformationExample");
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
注:SetCenterImage函数作用是将(0,0,0)点置于图像的中心,所以本程序执行该函数时会从新调用SetOutputOrigin(),所以会覆盖上方设置的原点位置,如果想让本程序输出设定的原点位置为(100,100,0),只需屏蔽语句SetCenterImage(1)
实例25:BMP图像像素值的访问与修改
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkSmartPointer.h>
#include <vtkImageViewer2.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkBMPReader.h>
#include <vtkImageData.h>
#include <vtkImageIterator.h>
//测试图像:../data/lena.bmp
int main(int argc, char* argv[])
{
vtkSmartPointer<vtkBMPReader> reader =
vtkSmartPointer<vtkBMPReader>::New();
reader->SetFileName ( "lena.bmp" );
reader->Update();
int subRegion[6] = {0,300, 0, 300, 0, 0};
vtkImageIterator<unsigned char> it(reader->GetOutput(), subRegion);
while(!it.IsAtEnd())
{
unsigned char *inSI = it.BeginSpan();
unsigned char *inSIEnd = it.EndSpan();
while(inSI != inSIEnd)
{
*inSI = 255-*inSI;
++inSI;
}
it.NextSpan();
}
vtkSmartPointer<vtkImageViewer2> imageViewer =
vtkSmartPointer<vtkImageViewer2>::New();
imageViewer->SetInputConnection(reader->GetOutputPort());
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
imageViewer->SetupInteractor(renderWindowInteractor);
imageViewer->Render();
imageViewer->GetRenderer()->ResetCamera();
imageViewer->Render();
imageViewer->GetRenderer()->SetBackground(1.0, 1.0, 1.0);
imageViewer->SetSize(640, 480);
imageViewer->GetRenderWindow()->SetWindowName("VisitImagePixelIterativelyExample");
renderWindowInteractor->Start();
return 0;
}
实例26:将BMP类型的RGB图像转换为灰度图像
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkSmartPointer.h>
#include <vtkImageData.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkRenderer.h>
#include <vtkImageActor.h>
#include <vtkBMPReader.h>
#include <vtkImageLuminance.h>
//测试图像:../data/lena.bmp
int main(int argc, char* argv[])
{
vtkSmartPointer<vtkBMPReader> reader =
vtkSmartPointer<vtkBMPReader>::New();
reader->SetFileName ( "lena.bmp" );
vtkSmartPointer<vtkImageLuminance> luminanceFilter =
vtkSmartPointer<vtkImageLuminance>::New();
luminanceFilter->SetInputConnection(reader->GetOutputPort());
luminanceFilter->Update();
vtkSmartPointer<vtkImageActor> originalActor =
vtkSmartPointer<vtkImageActor>::New();
originalActor->SetInputData(reader->GetOutput());
vtkSmartPointer<vtkImageActor> shiftscaleActor =
vtkSmartPointer<vtkImageActor>::New();
shiftscaleActor->SetInputData(luminanceFilter->GetOutput());
double originalViewport[4] = {0.0, 0.0, 0.5, 1.0};
double shiftscaleViewport[4] = {0.5, 0.0, 1.0, 1.0};
vtkSmartPointer<vtkRenderer> originalRenderer =
vtkSmartPointer<vtkRenderer>::New();
originalRenderer->SetViewport(originalViewport);
originalRenderer->AddActor(originalActor);
originalRenderer->ResetCamera();
originalRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderer> shiftscaleRenderer =
vtkSmartPointer<vtkRenderer>::New();
shiftscaleRenderer->SetViewport(shiftscaleViewport);
shiftscaleRenderer->AddActor(shiftscaleActor);
shiftscaleRenderer->ResetCamera();
shiftscaleRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(originalRenderer);
renderWindow->AddRenderer(shiftscaleRenderer);
renderWindow->SetSize( 640, 320 );
renderWindow->Render();
renderWindow->SetWindowName("Color2GrayImageExample");
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkInteractorStyleImage> style =
vtkSmartPointer<vtkInteractorStyleImage>::New();
renderWindowInteractor->SetInteractorStyle(style);
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
实例27:将BMP类型的RGB图像提取各个颜色(R、G、B)组分(提取颜色组分)
VTK中利用vtkImageExtractComponents可以方便地提取彩色图像的各个颜色通道组分,提取出来的每一个组分数据就是一个灰度数据。
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkImageData.h>
#include <vtkSmartPointer.h>
#include <vtkImageExtractComponents.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkRenderer.h>
#include <vtkBMPReader.h>
#include <vtkImageActor.h>
//测试图像:../data/lena.bmp
int main(int argc, char* argv[])
{
vtkSmartPointer<vtkBMPReader> reader =
vtkSmartPointer<vtkBMPReader>::New();
reader->SetFileName ("lena.bmp");
vtkSmartPointer<vtkImageExtractComponents> extractRedFilter =
vtkSmartPointer<vtkImageExtractComponents>::New();
extractRedFilter->SetInputConnection(reader->GetOutputPort());
extractRedFilter->SetComponents(0);
extractRedFilter->Update();
vtkSmartPointer<vtkImageExtractComponents> extractGreenFilter =
vtkSmartPointer<vtkImageExtractComponents>::New();
extractGreenFilter->SetInputConnection(reader->GetOutputPort());
extractGreenFilter->SetComponents(1);
extractGreenFilter->Update();
vtkSmartPointer<vtkImageExtractComponents> extractBlueFilter =
vtkSmartPointer<vtkImageExtractComponents>::New();
extractBlueFilter->SetInputConnection(reader->GetOutputPort());
extractBlueFilter->SetComponents(2);
extractBlueFilter->Update();
// Create actors
vtkSmartPointer<vtkImageActor> inputActor =
vtkSmartPointer<vtkImageActor>::New();
inputActor->SetInputData(reader->GetOutput());
vtkSmartPointer<vtkImageActor> redActor =
vtkSmartPointer<vtkImageActor>::New();
redActor->SetInputData(extractRedFilter->GetOutput());
vtkSmartPointer<vtkImageActor> greenActor =
vtkSmartPointer<vtkImageActor>::New();
greenActor->SetInputData(extractGreenFilter->GetOutput());
vtkSmartPointer<vtkImageActor> blueActor =
vtkSmartPointer<vtkImageActor>::New();
blueActor->SetInputData(extractBlueFilter->GetOutput());
double inputViewport[4] = {0.0, 0.0, 0.25, 1.0};
double redViewport[4] = {0.25, 0.0, 0.5, 1.0};
double greenViewport[4] = {0.5, 0.0, 0.75, 1.0};
double blueViewport[4] = {0.75, 0.0, 1.0, 1.0};
vtkSmartPointer<vtkRenderer> inputRenderer =
vtkSmartPointer<vtkRenderer>::New();
inputRenderer->SetViewport(inputViewport);
inputRenderer->AddActor(inputActor);
inputRenderer->ResetCamera();
inputRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderer> redRenderer =
vtkSmartPointer<vtkRenderer>::New();
redRenderer->SetViewport(redViewport);
redRenderer->AddActor(redActor);
redRenderer->ResetCamera();
redRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderer> greenRenderer =
vtkSmartPointer<vtkRenderer>::New();
greenRenderer->SetViewport(greenViewport);
greenRenderer->AddActor(greenActor);
greenRenderer->ResetCamera();
greenRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderer> blueRenderer =
vtkSmartPointer<vtkRenderer>::New();
blueRenderer->SetViewport(blueViewport);
blueRenderer->AddActor(blueActor);
blueRenderer->ResetCamera();
blueRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(inputRenderer);
renderWindow->AddRenderer(redRenderer);
renderWindow->AddRenderer(greenRenderer);
renderWindow->AddRenderer(blueRenderer);
renderWindow->SetSize(1200, 300);
renderWindow->Render();
renderWindow->SetWindowName("ImageExtractComponentsExample");
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkInteractorStyleImage> style =
vtkSmartPointer<vtkInteractorStyleImage>::New();
renderWindowInteractor->SetInteractorStyle(style);
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
实例28:将JPG类型的灰度图像进行彩色映射(图像彩色映射)
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkSmartPointer.h>
#include <vtkImageData.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkRenderer.h>
#include <vtkImageActor.h>
#include <vtkJPEGReader.h>
#include <vtkImageLuminance.h>
#include <vtkLookupTable.h>
#include <vtkImageMapToColors.h>
//测试图像:../data/lena-gray.jpg
int main(int argc, char* argv[])
{
vtkSmartPointer<vtkJPEGReader> reader =
vtkSmartPointer<vtkJPEGReader>::New();
reader->SetFileName ( "C:\\Users\\Administrator\\Desktop\\VTK2\\hellovtk\\vtk_图像处理学习\\第五章_图像处理\\data\\lena-gray.jpg" );
vtkSmartPointer<vtkLookupTable> colorTable =
vtkSmartPointer<vtkLookupTable>::New();
colorTable->SetRange( 0.0, 255.0 );
colorTable->SetHueRange( 0.1, 0.5 );
colorTable->SetValueRange( 0.6, 1.0 );
colorTable->Build();
vtkSmartPointer<vtkImageMapToColors> colorMap =
vtkSmartPointer<vtkImageMapToColors>::New();
colorMap->SetInputConnection( reader->GetOutputPort() );
colorMap->SetLookupTable( colorTable );
colorMap->Update();
vtkSmartPointer<vtkImageActor> originalActor =
vtkSmartPointer<vtkImageActor>::New();
originalActor->SetInputData(reader->GetOutput());
vtkSmartPointer<vtkImageActor> shiftscaleActor =
vtkSmartPointer<vtkImageActor>::New();
shiftscaleActor->SetInputData(colorMap->GetOutput());
double originalViewport[4] = {0.0, 0.0, 0.5, 1.0};
double shiftscaleViewport[4] = {0.5, 0.0, 1.0, 1.0};
vtkSmartPointer<vtkRenderer> originalRenderer =
vtkSmartPointer<vtkRenderer>::New();
originalRenderer->SetViewport(originalViewport);
originalRenderer->AddActor(originalActor);
originalRenderer->ResetCamera();
originalRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderer> shiftscaleRenderer =
vtkSmartPointer<vtkRenderer>::New();
shiftscaleRenderer->SetViewport(shiftscaleViewport);
shiftscaleRenderer->AddActor(shiftscaleActor);
shiftscaleRenderer->ResetCamera();
shiftscaleRenderer->SetBackground(1.0, 1.0, 0.8);
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(originalRenderer);
renderWindow->AddRenderer(shiftscaleRenderer);
renderWindow->SetSize(900, 300);
renderWindow->Render();
renderWindow->SetWindowName("Gray2ColorImageExample");
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkInteractorStyleImage> style =
vtkSmartPointer<vtkInteractorStyleImage>::New();
renderWindowInteractor->SetInteractorStyle(style);
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
实例29:多个灰度图像合成一个彩色图像(颜色合成)
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkSmartPointer.h>
#include <vtkImageData.h>
#include <vtkImageAppendComponents.h>
#include <vtkImageCanvasSource2D.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkRenderer.h>
#include <vtkJPEGReader.h>
#include <vtkImageActor.h>
int main(int, char *[])
{
vtkSmartPointer<vtkImageCanvasSource2D> red =
vtkSmartPointer<vtkImageCanvasSource2D>::New();
red->SetScalarTypeToUnsignedChar();
red->SetNumberOfScalarComponents(1);
red->SetExtent(0, 100, 0, 100, 0, 0);
red->SetDrawColor(0, 0, 0, 0);
red->FillBox(0,100,0,100);
red->SetDrawColor(255, 0, 0, 0);
red->FillBox(20,40,20,40);
red->Update();
vtkSmartPointer<vtkImageCanvasSource2D> green =
vtkSmartPointer<vtkImageCanvasSource2D>::New();
green->SetScalarTypeToUnsignedChar();
green->SetNumberOfScalarComponents(1);
green->SetExtent(0, 100, 0, 100, 0, 0);
green->SetDrawColor(0, 0, 0, 0);
green->FillBox(0,100,0,100);
green->SetDrawColor(255, 0, 0, 0);
green->FillBox(30,50,30,50);
green->Update();
vtkSmartPointer<vtkImageCanvasSource2D> blue =
vtkSmartPointer<vtkImageCanvasSource2D>::New();
blue->SetScalarTypeToUnsignedChar();
blue->SetNumberOfScalarComponents(1);
blue->SetExtent(0, 100, 0, 100, 0, 0);
blue->SetDrawColor(0, 0, 0, 0);
blue->FillBox(0,100,0,100);
blue->SetDrawColor(255, 0, 0, 0);
blue->FillBox(40,60,40,60);
blue->Update();
vtkSmartPointer<vtkImageAppendComponents> appendFilter =
vtkSmartPointer<vtkImageAppendComponents>::New();
appendFilter->SetInputConnection(0, red->GetOutputPort());
appendFilter->AddInputConnection(0, green->GetOutputPort());
appendFilter->AddInputConnection(0, blue->GetOutputPort());
appendFilter->Update();
vtkSmartPointer<vtkImageActor> redActor =
vtkSmartPointer<vtkImageActor>::New();
redActor->SetInputData(red->GetOutput());
vtkSmartPointer<vtkImageActor> greenActor =
vtkSmartPointer<vtkImageActor>::New();
greenActor->SetInputData(green->GetOutput());
vtkSmartPointer<vtkImageActor> blueActor =
vtkSmartPointer<vtkImageActor>::New();
blueActor->SetInputData(blue->GetOutput());
vtkSmartPointer<vtkImageActor> combinedActor =
vtkSmartPointer<vtkImageActor>::New();
combinedActor->SetInputData(appendFilter->GetOutput());
// Define viewport ranges
// (xmin, ymin, xmax, ymax)
double redViewport[4] = {0.0, 0.0, 0.25, 1.0};
double greenViewport[4] = {0.25, 0.0, 0.5, 1.0};
double blueViewport[4] = {0.5, 0.0, 0.75, 1.0};
double combinedViewport[4] = {0.75, 0.0, 1.0, 1.0};
// Setup renderers
vtkSmartPointer<vtkRenderer> redRenderer =
vtkSmartPointer<vtkRenderer>::New();
redRenderer->SetViewport(redViewport);
redRenderer->AddActor(redActor);
redRenderer->ResetCamera();
redRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderer> greenRenderer =
vtkSmartPointer<vtkRenderer>::New();
greenRenderer->SetViewport(greenViewport);
greenRenderer->AddActor(greenActor);
greenRenderer->ResetCamera();
greenRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderer> blueRenderer =
vtkSmartPointer<vtkRenderer>::New();
blueRenderer->SetViewport(blueViewport);
blueRenderer->AddActor(blueActor);
blueRenderer->ResetCamera();
blueRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderer> combinedRenderer =
vtkSmartPointer<vtkRenderer>::New();
combinedRenderer->SetViewport(combinedViewport);
combinedRenderer->AddActor(combinedActor);
combinedRenderer->ResetCamera();
combinedRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(redRenderer);
renderWindow->AddRenderer(greenRenderer);
renderWindow->AddRenderer(blueRenderer);
renderWindow->AddRenderer(combinedRenderer);
renderWindow->SetSize(1200, 300);
renderWindow->Render();
renderWindow->SetWindowName("ImageAppendComponentsExample");
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkInteractorStyleImage> style =
vtkSmartPointer<vtkInteractorStyleImage>::New();
renderWindowInteractor->SetInteractorStyle(style);
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
实例30:提取BMP图像的感兴趣区域(区域提取)
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkSmartPointer.h>
#include <vtkImageData.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkRenderer.h>
#include <vtkImageActor.h>
#include <vtkBMPReader.h>
#include <vtkExtractVOI.h>
//测试图像:../data/lena.bmp
int main(int argc, char* argv[])
{
vtkSmartPointer<vtkBMPReader> reader =
vtkSmartPointer<vtkBMPReader>::New();
reader->SetFileName ("lena.bmp");
reader->Update();
int dims[3];
reader->GetOutput()->GetDimensions(dims);
vtkSmartPointer<vtkExtractVOI> extractVOI =
vtkSmartPointer<vtkExtractVOI>::New();
extractVOI->SetInputConnection(reader->GetOutputPort());
extractVOI->SetVOI(dims[0]/4.,3.*dims[0]/4.,dims[1]/4.,3.*dims[1]/4., 0, 0);
extractVOI->Update();
vtkSmartPointer<vtkImageActor> originalActor =
vtkSmartPointer<vtkImageActor>::New();
originalActor->SetInputData(reader->GetOutput());
vtkSmartPointer<vtkImageActor> voiActor =
vtkSmartPointer<vtkImageActor>::New();
voiActor->SetInputData(extractVOI->GetOutput());
double originalViewport[4] = {0.0, 0.0, 0.5, 1.0};
double voiviewport[4] = {0.5, 0.0, 1.0, 1.0};
vtkSmartPointer<vtkRenderer> originalRenderer =
vtkSmartPointer<vtkRenderer>::New();
originalRenderer->SetViewport(originalViewport);
originalRenderer->AddActor(originalActor);
originalRenderer->ResetCamera();
originalRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderer> shiftscaleRenderer =
vtkSmartPointer<vtkRenderer>::New();
shiftscaleRenderer->SetViewport(voiviewport);
shiftscaleRenderer->AddActor(voiActor);
shiftscaleRenderer->ResetCamera();
shiftscaleRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(originalRenderer);
renderWindow->AddRenderer(shiftscaleRenderer);
renderWindow->SetSize(900, 300);
renderWindow->Render();
renderWindow->SetWindowName("ExtractVOIExample");
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkInteractorStyleImage> style =
vtkSmartPointer<vtkInteractorStyleImage>::New();
renderWindowInteractor->SetInteractorStyle(style);
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
实例31:灰度图像直方图
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkActor.h>
#include <vtkBarChartActor.h>
#include <vtkFieldData.h>
#include <vtkImageAccumulate.h>
#include <vtkImageData.h>
#include <vtkIntArray.h>
#include <vtkJPEGReader.h>
#include <vtkLegendBoxActor.h>
#include <vtkProperty2D.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSmartPointer.h>
#include <vtkTextProperty.h>
//测试图像:../data/lena-gray.jpg
int main(int argc, char* argv[])
{
vtkSmartPointer<vtkJPEGReader> reader =
vtkSmartPointer<vtkJPEGReader>::New();
reader->SetFileName ( "lena-gray.jpg" );
reader->Update();
int bins = 16;
int comps = 1;
vtkSmartPointer<vtkImageAccumulate> histogram =
vtkSmartPointer<vtkImageAccumulate>::New();
histogram->SetInputData(reader->GetOutput());
histogram->SetComponentExtent(0, bins-1, 0, 0, 0, 0);
histogram->SetComponentOrigin(0, 0, 0);
histogram->SetComponentSpacing(256.0/bins, 0, 0);
histogram->Update();
int* output = static_cast<int*>(histogram->GetOutput()->GetScalarPointer());
vtkSmartPointer<vtkIntArray> frequencies =
vtkSmartPointer<vtkIntArray>::New();
frequencies->SetNumberOfComponents(1);
for(int j = 0; j < bins; ++j)
{
for(int i=0; i<comps; i++)
{
frequencies->InsertNextTuple1(*output++);
}
}
vtkSmartPointer<vtkDataObject> dataObject =
vtkSmartPointer<vtkDataObject>::New();
dataObject->GetFieldData()->AddArray( frequencies );
vtkSmartPointer<vtkBarChartActor> barChart =
vtkSmartPointer<vtkBarChartActor>::New();
barChart->SetInput(dataObject);
barChart->SetTitle("Histogram");
barChart->GetPositionCoordinate()->SetValue(0.05,0.05,0.0);
barChart->GetPosition2Coordinate()->SetValue(0.95,0.95,0.0);
barChart->GetProperty()->SetColor(0,0,0);
barChart->GetTitleTextProperty()->SetColor(0,0,0);
barChart->GetLabelTextProperty()->SetColor(0,0,0);
barChart->GetLegendActor()->SetNumberOfEntries(dataObject->GetFieldData()->GetArray(0)->GetNumberOfTuples());
barChart->LegendVisibilityOff();
barChart->LabelVisibilityOff();
double colors[3][3] = {
{ 1, 0, 0 },
{ 0, 1, 0 },
{ 0, 0, 1 } };
int count = 0;
for( int i = 0; i < bins; ++i )
{
for( int j = 0; j < comps; ++j )
{
barChart->SetBarColor( count++, colors[j] );
}
}
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(barChart);
renderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
renderWindow->SetSize(640, 480);
renderWindow->Render();
renderWindow->SetWindowName("ImageAccumulateExample");
vtkSmartPointer<vtkRenderWindowInteractor> interactor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
interactor->SetRenderWindow(renderWindow);
interactor->Initialize();
interactor->Start();
return EXIT_SUCCESS;
}
实例32:图像重采样(降采样)
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkSmartPointer.h>
#include <vtkImageData.h>
#include <vtkImageShiftScale.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkRenderer.h>
#include <vtkImageActor.h>
#include <vtkBMPReader.h>
#include <vtkImageShrink3D.h>
//测试图像:../data/lena.bmp
int main(int argc, char* argv[])
{
vtkSmartPointer<vtkBMPReader> reader =
vtkSmartPointer<vtkBMPReader>::New();
reader->SetFileName("lena.bmp");
reader->Update();
vtkSmartPointer<vtkImageShrink3D> shrinkFilter =
vtkSmartPointer<vtkImageShrink3D>::New();
shrinkFilter->SetInputConnection(reader->GetOutputPort());
shrinkFilter->SetShrinkFactors(20,20,1);
shrinkFilter->Update();
int originalDims[3];
reader->GetOutput()->GetDimensions(originalDims);
double originalSpace[3];
reader->GetOutput()->GetSpacing(originalSpace);
int shrinkDims[3];
shrinkFilter->GetOutput()->GetDimensions(shrinkDims);
double shrinkSpace[3];
shrinkFilter->GetOutput()->GetSpacing(shrinkSpace);
std::cout<<"原图图像维数 :"<<originalDims[0] << " "<<originalDims[1]<<" "<<originalDims[2]<<std::endl;
std::cout<<"原图图像像素间隔 :"<<originalSpace[0] << " "<<originalSpace[1]<<" "<<originalSpace[2]<<std::endl;
std::cout<<"重采样图像维数 :"<<shrinkDims[0] << " "<<shrinkDims[1]<<" "<<shrinkDims[2]<<std::endl;
std::cout<<"重采样图像像素间隔:"<<shrinkSpace[0] << " "<<shrinkSpace[1]<<" "<<shrinkSpace[2]<<std::endl;
vtkSmartPointer<vtkImageActor> originalActor =
vtkSmartPointer<vtkImageActor>::New();
originalActor->SetInputData( reader->GetOutput());
vtkSmartPointer<vtkImageActor> shrinkActor =
vtkSmartPointer<vtkImageActor>::New();
shrinkActor->SetInputData(shrinkFilter->GetOutput());
double originalViewport[4] = {0.0, 0.0, 0.5, 1.0};
double shrinkViewport[4] = {0.5, 0.0, 1.0, 1.0};
vtkSmartPointer<vtkRenderer> originalRenderer =
vtkSmartPointer<vtkRenderer>::New();
originalRenderer->SetViewport(originalViewport);
originalRenderer->AddActor(originalActor);
originalRenderer->ResetCamera();
originalRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderer> shrinkRenderer =
vtkSmartPointer<vtkRenderer>::New();
shrinkRenderer->SetViewport(shrinkViewport);
shrinkRenderer->AddActor(shrinkActor);
shrinkRenderer->ResetCamera();
shrinkRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(originalRenderer);
renderWindow->AddRenderer(shrinkRenderer);
renderWindow->SetSize(640, 480);
renderWindow->Render();
renderWindow->SetWindowName("ImageShrink3DExample");
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkInteractorStyleImage> style =
vtkSmartPointer<vtkInteractorStyleImage>::New();
renderWindowInteractor->SetInteractorStyle(style);
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
实例33:图像重采样(降和升采样)
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkSmartPointer.h>
#include <vtkImageData.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkRenderer.h>
#include <vtkImageActor.h>
#include <vtkBMPReader.h>
#include <vtkImageShrink3D.h>
#include <vtkImageMagnify.h>
//测试图像:../data/lena.bmp
int main(int argc, char* argv[])
{
vtkSmartPointer<vtkBMPReader> reader =
vtkSmartPointer<vtkBMPReader>::New();
reader->SetFileName ( "lena.bmp" );
reader->Update();
vtkSmartPointer<vtkImageShrink3D> shrinkFilter =
vtkSmartPointer<vtkImageShrink3D>::New();
shrinkFilter->SetInputConnection(reader->GetOutputPort());
shrinkFilter->SetShrinkFactors(16,16,1);
shrinkFilter->Update();
vtkSmartPointer<vtkImageMagnify> magnifyFilter =
vtkSmartPointer<vtkImageMagnify>::New();
magnifyFilter->SetInputConnection(reader->GetOutputPort());
magnifyFilter->SetMagnificationFactors(10,10,1);
magnifyFilter->Update();
int originalDims[3];
reader->GetOutput()->GetDimensions(originalDims);
double originalSpace[3];
reader->GetOutput()->GetSpacing(originalSpace);
int shrinkDims[3];
shrinkFilter->GetOutput()->GetDimensions(shrinkDims);
double shrinkSpace[3];
shrinkFilter->GetOutput()->GetSpacing(shrinkSpace);
int magnifyDims[3];
magnifyFilter->GetOutput()->GetDimensions(magnifyDims);
double magnifySpace[3];
magnifyFilter->GetOutput()->GetSpacing(magnifySpace);
std::cout<<"原图图像维数 :"<<originalDims[0] << " "<<originalDims[1]<<" "<<originalDims[2]<<std::endl;
std::cout<<"原图图像像素间隔 :"<<originalSpace[0] << " "<<originalSpace[1]<<" "<<originalSpace[2]<<std::endl;
std::cout<<"降采样图像维数 :"<<shrinkDims[0] << " "<<shrinkDims[1]<<" "<<shrinkDims[2]<<std::endl;
std::cout<<"降采样图像像素间隔:"<<shrinkSpace[0] << " "<<shrinkSpace[1]<<" "<<shrinkSpace[2]<<std::endl;
std::cout<<"升采样图像维数 :"<<magnifyDims[0] << " "<<magnifyDims[1]<<" "<<magnifyDims[2]<<std::endl;
std::cout<<"升采样图像像素间隔:"<<magnifySpace[0] << " "<<magnifySpace[1]<<" "<<magnifySpace[2]<<std::endl;
vtkSmartPointer<vtkImageActor> originalActor =
vtkSmartPointer<vtkImageActor>::New();
originalActor->SetInputData(reader->GetOutput());
vtkSmartPointer<vtkImageActor> shrinkActor =
vtkSmartPointer<vtkImageActor>::New();
shrinkActor->SetInputData(shrinkFilter->GetOutput());
vtkSmartPointer<vtkImageActor> magnifyActor =
vtkSmartPointer<vtkImageActor>::New();
magnifyActor->SetInputData(magnifyFilter->GetOutput());
double originalViewport[4] = {0.0, 0.0, 0.33, 1.0};
double shrinkViewport[4] = {0.33, 0.0, 0.66, 1.0};
double magnifyViewport[4] = {0.66, 0.0, 1.0, 1.0};
vtkSmartPointer<vtkRenderer> originalRenderer =
vtkSmartPointer<vtkRenderer>::New();
originalRenderer->SetViewport(originalViewport);
originalRenderer->AddActor(originalActor);
originalRenderer->ResetCamera();
originalRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderer> shrinkRenderer =
vtkSmartPointer<vtkRenderer>::New();
shrinkRenderer->SetViewport(shrinkViewport);
shrinkRenderer->AddActor(shrinkActor);
shrinkRenderer->ResetCamera();
shrinkRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderer> magnifyRenderer =
vtkSmartPointer<vtkRenderer>::New();
magnifyRenderer->SetViewport(magnifyViewport);
magnifyRenderer->AddActor(magnifyActor);
magnifyRenderer->ResetCamera();
magnifyRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(originalRenderer);
renderWindow->AddRenderer(shrinkRenderer);
renderWindow->AddRenderer(magnifyRenderer);
renderWindow->SetSize(640, 320);
renderWindow->Render();
renderWindow->SetWindowName("ImageShrinkMagnifyExample");
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkInteractorStyleImage> style =
vtkSmartPointer<vtkInteractorStyleImage>::New();
renderWindowInteractor->SetInteractorStyle(style);
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
实例34:图像运算(数字运算)
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkMath.h>
#include <vtkSmartPointer.h>
#include <vtkImageCanvasSource2D.h>
#include <vtkImageMathematics.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkRenderer.h>
#include <vtkImageActor.h>
int main(int, char *[])
{
vtkSmartPointer<vtkImageCanvasSource2D> imageSource =
vtkSmartPointer<vtkImageCanvasSource2D>::New();
imageSource->SetNumberOfScalarComponents(3);
imageSource->SetScalarTypeToUnsignedChar();
imageSource->SetExtent(0, 4, 0, 4, 0, 0);
imageSource->SetDrawColor(100.0, 0, 0);
imageSource->FillBox(0, 4, 0, 4);
imageSource->Update();
vtkSmartPointer<vtkImageMathematics> imageMath =
vtkSmartPointer<vtkImageMathematics>::New();
imageMath->SetOperationToMultiplyByK();
imageMath->SetConstantK(2.0);
imageMath->SetInputConnection(imageSource->GetOutputPort());
imageMath->Update();
vtkSmartPointer<vtkImageActor> originalActor =
vtkSmartPointer<vtkImageActor>::New();
originalActor->SetInputData(imageSource->GetOutput());
vtkSmartPointer<vtkImageActor> mathActor =
vtkSmartPointer<vtkImageActor>::New();
mathActor->SetInputData(imageMath->GetOutput());
double leftViewport[4] = {0.0, 0.0, 0.5, 1.0};
double rightViewport[4] = {0.5, 0.0, 1.0, 1.0};
vtkSmartPointer<vtkRenderer> originalRenderer =
vtkSmartPointer<vtkRenderer>::New();
originalRenderer->SetViewport(leftViewport);
originalRenderer->AddActor(originalActor);
originalRenderer->ResetCamera();
originalRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderer> gradientMagnitudeRenderer =
vtkSmartPointer<vtkRenderer>::New();
gradientMagnitudeRenderer->SetViewport(rightViewport);
gradientMagnitudeRenderer->AddActor(mathActor);
gradientMagnitudeRenderer->ResetCamera();
gradientMagnitudeRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(originalRenderer);
renderWindow->AddRenderer(gradientMagnitudeRenderer);
renderWindow->SetSize(640, 480);
renderWindow->Render();
renderWindow->SetWindowName("ImageMathematicsExample");
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkInteractorStyleImage> style =
vtkSmartPointer<vtkInteractorStyleImage>::New();
renderWindowInteractor->SetInteractorStyle(style);
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
实例35:图像运算(逻辑运算)
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkSmartPointer.h>
#include <vtkMath.h>
#include <vtkImageData.h>
#include <vtkImageCanvasSource2D.h>
#include <vtkImageLogic.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkRenderer.h>
#include <vtkImageActor.h>
int main(int, char *[])
{
vtkSmartPointer<vtkImageCanvasSource2D> imageSource1 =
vtkSmartPointer<vtkImageCanvasSource2D>::New();
imageSource1->SetScalarTypeToUnsignedChar();
imageSource1->SetNumberOfScalarComponents(1);
imageSource1->SetExtent(0, 100, 0, 100, 0, 0);
imageSource1->SetDrawColor(0.0);
imageSource1->FillBox(0,100,0,100);
imageSource1->SetDrawColor(255);
imageSource1->FillBox(20,60,20,60);
imageSource1->Update();
vtkSmartPointer<vtkImageCanvasSource2D> imageSource2 =
vtkSmartPointer<vtkImageCanvasSource2D>::New();
imageSource2->SetNumberOfScalarComponents(1);
imageSource2->SetScalarTypeToUnsignedChar();
imageSource2->SetExtent(0, 100, 0, 100, 0, 0);
imageSource2->SetDrawColor(0.0);
imageSource2->FillBox(0,100,0,100);
imageSource2->SetDrawColor(255.0);
imageSource2->FillBox(40,80,40,80);
imageSource2->Update();
vtkSmartPointer<vtkImageLogic> imageLogic =
vtkSmartPointer<vtkImageLogic>::New();
imageLogic->SetInputData(imageSource1->GetOutput());
imageLogic->SetInputData(imageSource2->GetOutput());
imageLogic->SetOperationToXor();
imageLogic->SetOutputTrueValue(128);
imageLogic->Update();
vtkSmartPointer<vtkImageActor> originalActor1 =
vtkSmartPointer<vtkImageActor>::New();
originalActor1->SetInputData(imageSource1->GetOutput());
vtkSmartPointer<vtkImageActor> originalActor2 =
vtkSmartPointer<vtkImageActor>::New();
originalActor2->SetInputData(imageSource2->GetOutput());
vtkSmartPointer<vtkImageActor> logicActor =
vtkSmartPointer<vtkImageActor>::New();
logicActor->SetInputData(imageLogic->GetOutput());
double leftViewport[4] = {0.0, 0.0, 0.33, 1.0};
double midViewport[4] = {0.33, 0.0, 0.66, 1.0};
double rightViewport[4] = {0.66, 0.0, 1.0, 1.0};
vtkSmartPointer<vtkRenderer> originalRenderer1 =
vtkSmartPointer<vtkRenderer>::New();
originalRenderer1->SetViewport(leftViewport);
originalRenderer1->AddActor(originalActor1);
originalRenderer1->ResetCamera();
originalRenderer1->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderer> originalRenderer2 =
vtkSmartPointer<vtkRenderer>::New();
originalRenderer2->SetViewport(midViewport);
originalRenderer2->AddActor(originalActor2);
originalRenderer2->ResetCamera();
originalRenderer2->SetBackground(0.8, 0.8, 0.8);
vtkSmartPointer<vtkRenderer> logicRenderer =
vtkSmartPointer<vtkRenderer>::New();
logicRenderer->SetViewport(rightViewport);
logicRenderer->AddActor(logicActor);
logicRenderer->ResetCamera();
logicRenderer->SetBackground(0.6, 0.6, 0.6);
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(originalRenderer1);
renderWindow->AddRenderer(originalRenderer2);
renderWindow->AddRenderer(logicRenderer);
renderWindow->SetSize(640, 320);
renderWindow->Render();
renderWindow->SetWindowName("ImageLogicExample");
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkInteractorStyleImage> style =
vtkSmartPointer<vtkInteractorStyleImage>::New();
renderWindowInteractor->SetInteractorStyle(style);
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
实例36:图像二值化
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkSmartPointer.h>
#include <vtkImageData.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkRenderer.h>
#include <vtkImageActor.h>
#include <vtkJPEGReader.h>
#include <vtkImageThreshold.h>
#include <vtkImageCast.h>
//测试图像:../data/lena-gray.jpg
int main(int argc, char* argv[])
{
vtkSmartPointer<vtkJPEGReader> reader =
vtkSmartPointer<vtkJPEGReader>::New();
reader->SetFileName("lena-gray.jpg");
reader->Update();
vtkSmartPointer<vtkImageThreshold> thresholdFilter =
vtkSmartPointer<vtkImageThreshold>::New();
thresholdFilter->SetInputConnection(reader->GetOutputPort());
thresholdFilter->ThresholdByUpper(100);//设置阈值为100
thresholdFilter->SetInValue(255);//大于100的设置为255
thresholdFilter->SetOutValue(0);//小于100的设置为0
thresholdFilter->Update();//算法执行后必须添加更新消息
vtkSmartPointer<vtkImageActor> originalActor =
vtkSmartPointer<vtkImageActor>::New();
originalActor->SetInputData(reader->GetOutput());
vtkSmartPointer<vtkImageActor> binaryActor =
vtkSmartPointer<vtkImageActor>::New();
binaryActor->SetInputData(thresholdFilter->GetOutput());
double originalViewport[4] = {0.0, 0.0, 0.5, 1.0};
double binaryviewport[4] = {0.5, 0.0, 1.0, 1.0};
vtkSmartPointer<vtkRenderer> originalRenderer =
vtkSmartPointer<vtkRenderer>::New();
originalRenderer->SetViewport(originalViewport);
originalRenderer->AddActor(originalActor);
originalRenderer->ResetCamera();
originalRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderer> binaryRenderer =
vtkSmartPointer<vtkRenderer>::New();
binaryRenderer->SetViewport(binaryviewport);
binaryRenderer->AddActor(binaryActor);
binaryRenderer->ResetCamera();
binaryRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(originalRenderer);
renderWindow->AddRenderer(binaryRenderer);
renderWindow->SetSize(640, 320);
renderWindow->Render();
renderWindow->SetWindowName("ImageBinaryExample");
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkInteractorStyleImage> style =
vtkSmartPointer<vtkInteractorStyleImage>::New();
renderWindowInteractor->SetInteractorStyle(style);
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
实例37:梯度算子(边缘检测)
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkSmartPointer.h>
#include <vtkImageData.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkRenderer.h>
#include <vtkImageActor.h>
#include <vtkJPEGReader.h>
#include <vtkImageGradient.h>
#include <vtkImageMagnitude.h>
#include <vtkImageCast.h>
#include <vtkImageShiftScale.h>
//测试图像:../data/lena-gray.jpg
int main(int argc, char* argv[])
{
vtkSmartPointer<vtkJPEGReader> reader =
vtkSmartPointer<vtkJPEGReader>::New();
reader->SetFileName("lena-gray.jpg");
reader->Update();
vtkSmartPointer<vtkImageGradient> gradientFilter =
vtkSmartPointer<vtkImageGradient>::New();
gradientFilter->SetInputConnection(reader->GetOutputPort());
gradientFilter->SetDimensionality(2);
vtkSmartPointer<vtkImageMagnitude> magnitudeFilter =
vtkSmartPointer<vtkImageMagnitude>::New();
magnitudeFilter->SetInputConnection(gradientFilter->GetOutputPort());
magnitudeFilter->Update();
double range[2];
magnitudeFilter->GetOutput()->GetScalarRange(range);
vtkSmartPointer<vtkImageShiftScale> ShiftScale =
vtkSmartPointer<vtkImageShiftScale>::New();
ShiftScale->SetOutputScalarTypeToUnsignedChar();
ShiftScale->SetScale( 255 / range[1] );
ShiftScale->SetInputConnection(magnitudeFilter->GetOutputPort());
ShiftScale->Update();
vtkSmartPointer<vtkImageActor> originalActor =
vtkSmartPointer<vtkImageActor>::New();
originalActor->SetInputData(reader->GetOutput());
vtkSmartPointer<vtkImageActor> gradActor =
vtkSmartPointer<vtkImageActor>::New();
gradActor->SetInputData(ShiftScale->GetOutput());
double originalViewport[4] = {0.0, 0.0, 0.5, 1.0};
double gradviewport[4] = {0.5, 0.0, 1.0, 1.0};
vtkSmartPointer<vtkRenderer> originalRenderer =
vtkSmartPointer<vtkRenderer>::New();
originalRenderer->SetViewport(originalViewport);
originalRenderer->AddActor(originalActor);
originalRenderer->ResetCamera();
originalRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderer> gradRenderer =
vtkSmartPointer<vtkRenderer>::New();
gradRenderer->SetViewport(gradviewport);
gradRenderer->AddActor(gradActor);
gradRenderer->ResetCamera();
gradRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(originalRenderer);
renderWindow->AddRenderer(gradRenderer);
renderWindow->SetSize( 640, 320 );
renderWindow->Render();
renderWindow->SetWindowName("ImageGradientExample");
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkInteractorStyleImage> style =
vtkSmartPointer<vtkInteractorStyleImage>::New();
renderWindowInteractor->SetInteractorStyle(style);
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
实例38:Sobel梯度算子(边缘检测)
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkSmartPointer.h>
#include <vtkImageMathematics.h>
#include <vtkImageData.h>
#include <vtkImageSobel2D.h>
#include <vtkImageMagnitude.h>
#include <vtkImageExtractComponents.h>
#include <vtkImageShiftScale.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkRenderer.h>
#include <vtkImageActor.h>
#include <vtkJPEGReader.h>
//测试图像:../data/lena-gray.jpg
int main(int argc, char* argv[])
{
vtkSmartPointer<vtkJPEGReader> reader =
vtkSmartPointer<vtkJPEGReader>::New();
reader->SetFileName("lena-gray.jpg");
reader->Update();
vtkSmartPointer<vtkImageSobel2D> sobelFilter =
vtkSmartPointer<vtkImageSobel2D>::New();
sobelFilter->SetInputConnection(reader->GetOutputPort());
vtkSmartPointer<vtkImageExtractComponents> extractXFilter =
vtkSmartPointer<vtkImageExtractComponents>::New();
extractXFilter->SetComponents(0);
extractXFilter->SetInputConnection(sobelFilter->GetOutputPort());
extractXFilter->Update();
double xRange[2];
extractXFilter->GetOutput()->GetScalarRange(xRange);
vtkSmartPointer<vtkImageMathematics> xImageAbs =
vtkSmartPointer<vtkImageMathematics>::New();
xImageAbs->SetOperationToAbsoluteValue();
xImageAbs->SetInputConnection(extractXFilter->GetOutputPort());
xImageAbs->Update();
vtkSmartPointer<vtkImageShiftScale> xShiftScale =
vtkSmartPointer<vtkImageShiftScale>::New();
xShiftScale->SetOutputScalarTypeToUnsignedChar();
xShiftScale->SetScale( 255 / xRange[1] );
xShiftScale->SetInputConnection(xImageAbs->GetOutputPort());
xShiftScale->Update();
vtkSmartPointer<vtkImageExtractComponents> extractYFilter =
vtkSmartPointer<vtkImageExtractComponents>::New();
extractYFilter->SetComponents(1);
extractYFilter->SetInputConnection(sobelFilter->GetOutputPort());
extractYFilter->Update();
double yRange[2];
extractYFilter->GetOutput()->GetScalarRange(yRange);
vtkSmartPointer<vtkImageMathematics> yImageAbs =
vtkSmartPointer<vtkImageMathematics>::New();
yImageAbs->SetOperationToAbsoluteValue();
yImageAbs->SetInputConnection(extractYFilter->GetOutputPort());
yImageAbs->Update();
vtkSmartPointer<vtkImageShiftScale> yShiftScale =
vtkSmartPointer<vtkImageShiftScale>::New();
yShiftScale->SetOutputScalarTypeToUnsignedChar();
yShiftScale->SetScale( 255 / yRange[1] );
yShiftScale->SetInputConnection(yImageAbs->GetOutputPort());
yShiftScale->Update();
vtkSmartPointer<vtkImageActor> originalActor =
vtkSmartPointer<vtkImageActor>::New();
originalActor->SetInputData(reader->GetOutput());
vtkSmartPointer<vtkImageActor> xActor =
vtkSmartPointer<vtkImageActor>::New();
xActor->SetInputData(xShiftScale->GetOutput());
vtkSmartPointer<vtkImageActor> yActor =
vtkSmartPointer<vtkImageActor>::New();
yActor->SetInputData(yShiftScale->GetOutput());
double originalViewport[4] = {0.0, 0.0, 0.33, 1.0};
double xViewport[4] = {0.33, 0.0, 0.66, 1.0};
double yViewport[4] = {0.66, 0.0, 1.0, 1.0};
vtkSmartPointer<vtkRenderer> originalRenderer =
vtkSmartPointer<vtkRenderer>::New();
originalRenderer->SetViewport(originalViewport);
originalRenderer->AddActor(originalActor);
originalRenderer->ResetCamera();
originalRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderer> xRenderer =
vtkSmartPointer<vtkRenderer>::New();
xRenderer->SetViewport(xViewport);
xRenderer->AddActor(xActor);
xRenderer->ResetCamera();
xRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderer> yRenderer =
vtkSmartPointer<vtkRenderer>::New();
yRenderer->SetViewport(yViewport);
yRenderer->AddActor(yActor);
yRenderer->ResetCamera();
yRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->SetSize(1200, 300);
renderWindow->AddRenderer(originalRenderer);
renderWindow->AddRenderer(xRenderer);
renderWindow->AddRenderer(yRenderer);
renderWindow->Render();
renderWindow->SetWindowName("SobelExample");
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkInteractorStyleImage> style =
vtkSmartPointer<vtkInteractorStyleImage>::New();
renderWindowInteractor->SetInteractorStyle(style);
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
实例39:拉普拉斯算子(边缘检测)
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkSmartPointer.h>
#include <vtkImageData.h>
#include <vtkImageCanvasSource2D.h>
#include <vtkImageShiftScale.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkRenderer.h>
#include <vtkImageActor.h>
#include <vtkJPEGReader.h>
#include <vtkImageLaplacian.h>
//测试图像:../data/lena-gray.jpg
int main(int argc, char* argv[])
{
vtkSmartPointer<vtkJPEGReader> reader =
vtkSmartPointer<vtkJPEGReader>::New();
reader->SetFileName("lena-gray.jpg");
reader->Update();
vtkSmartPointer<vtkImageLaplacian> lapFilter =
vtkSmartPointer<vtkImageLaplacian>::New();
lapFilter->SetInputConnection(reader->GetOutputPort());
lapFilter->SetDimensionality(2);
double range[2];
lapFilter->GetOutput()->GetScalarRange(range);
vtkSmartPointer<vtkImageShiftScale> ShiftScale =
vtkSmartPointer<vtkImageShiftScale>::New();
ShiftScale->SetOutputScalarTypeToUnsignedChar();
ShiftScale->SetScale( 255 / (range[1]-range[0]) );
ShiftScale->SetShift(-range[0]);
ShiftScale->SetInputConnection(lapFilter->GetOutputPort());
ShiftScale->Update();
vtkSmartPointer<vtkImageActor> originalActor =
vtkSmartPointer<vtkImageActor>::New();
originalActor->SetInputData(reader->GetOutput());
vtkSmartPointer<vtkImageActor> gradActor =
vtkSmartPointer<vtkImageActor>::New();
gradActor->SetInputData(ShiftScale->GetOutput());
double originalViewport[4] = {0.0, 0.0, 0.5, 1.0};
double gradviewport[4] = {0.5, 0.0, 1.0, 1.0};
vtkSmartPointer<vtkRenderer> originalRenderer =
vtkSmartPointer<vtkRenderer>::New();
originalRenderer->SetViewport(originalViewport);
originalRenderer->AddActor(originalActor);
originalRenderer->ResetCamera();
originalRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderer> gradRenderer =
vtkSmartPointer<vtkRenderer>::New();
gradRenderer->SetViewport(gradviewport);
gradRenderer->AddActor(gradActor);
gradRenderer->ResetCamera();
gradRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(originalRenderer);
renderWindow->AddRenderer(gradRenderer);
renderWindow->SetSize(640, 320);
renderWindow->Render();
renderWindow->SetWindowName("LaplacianExample");
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkInteractorStyleImage> style =
vtkSmartPointer<vtkInteractorStyleImage>::New();
renderWindowInteractor->SetInteractorStyle(style);
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
实例40:均值滤波(图像光滑)
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkSmartPointer.h>
#include <vtkImageData.h>
#include <vtkImageConvolve.h>
#include <vtkImageMandelbrotSource.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkRenderer.h>
#include <vtkImageActor.h>
#include <vtkImageCast.h>
#include <vtkJPEGReader.h>
#include <vtkImageShiftScale.h>
//测试图像:../data/lena-gray.jpg
int main(int argc, char* argv[])
{
vtkSmartPointer<vtkJPEGReader> reader =
vtkSmartPointer<vtkJPEGReader>::New();
reader->SetFileName("lena-gray.jpg");
reader->Update();
vtkSmartPointer<vtkImageCast> originalCastFilter =
vtkSmartPointer<vtkImageCast>::New();
originalCastFilter->SetInputConnection(reader->GetOutputPort());
originalCastFilter->SetOutputScalarTypeToFloat();
originalCastFilter->Update();
vtkSmartPointer<vtkImageConvolve> convolveFilter =
vtkSmartPointer<vtkImageConvolve>::New();
convolveFilter->SetInputConnection(originalCastFilter->GetOutputPort());
double kernel[25] = {0.04,0.04,0.04,0.04,0.04,
0.04,0.04,0.04,0.04,0.04,
0.04,0.04,0.04,0.04,0.04,
0.04,0.04,0.04,0.04,0.04,
0.04,0.04,0.04,0.04,0.04 };
convolveFilter->SetKernel5x5(kernel);
convolveFilter->Update();
vtkSmartPointer<vtkImageCast> convCastFilter =
vtkSmartPointer<vtkImageCast>::New();
convCastFilter->SetInputData(convolveFilter->GetOutput());
convCastFilter->SetOutputScalarTypeToUnsignedChar();
convCastFilter->Update();
vtkSmartPointer<vtkImageActor> originalActor =
vtkSmartPointer<vtkImageActor>::New();
originalActor->SetInputData(reader->GetOutput());
vtkSmartPointer<vtkImageActor> convolvedActor =
vtkSmartPointer<vtkImageActor>::New();
convolvedActor->SetInputData(convCastFilter->GetOutput());
double leftViewport[4] = {0.0, 0.0, 0.5, 1.0};
double rightViewport[4] = {0.5, 0.0, 1.0, 1.0};
vtkSmartPointer<vtkRenderer> originalRenderer =
vtkSmartPointer<vtkRenderer>::New();
originalRenderer->SetViewport(leftViewport);
originalRenderer->AddActor(originalActor);
originalRenderer->SetBackground(1.0, 1.0, 1.0);
originalRenderer->ResetCamera();
vtkSmartPointer<vtkRenderer> convolvedRenderer =
vtkSmartPointer<vtkRenderer>::New();
convolvedRenderer->SetViewport(rightViewport);
convolvedRenderer->AddActor(convolvedActor);
convolvedRenderer->SetBackground(1.0, 1.0, 1.0);
convolvedRenderer->ResetCamera();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();;
renderWindow->AddRenderer(originalRenderer);
renderWindow->AddRenderer(convolvedRenderer);
renderWindow->SetSize(640, 320);
renderWindow->Render();
renderWindow->SetWindowName("MeanFilterExample");
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkInteractorStyleImage> style =
vtkSmartPointer<vtkInteractorStyleImage>::New();
renderWindowInteractor->SetInteractorStyle(style);
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
实例41:高斯平滑(图像光滑)
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkSmartPointer.h>
#include <vtkImageData.h>
#include <vtkImageGaussianSmooth.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkRenderer.h>
#include <vtkImageActor.h>
#include <vtkImageEllipsoidSource.h>
#include <vtkImageCast.h>
#include <vtkJPEGReader.h>
//测试图像:../data/lena-gray.jpg
int main(int argc, char* argv[])
{
vtkSmartPointer<vtkJPEGReader> reader =
vtkSmartPointer<vtkJPEGReader>::New();
reader->SetFileName("lena-gray.jpg");
reader->Update();
vtkSmartPointer<vtkImageGaussianSmooth> gaussianSmoothFilter =
vtkSmartPointer<vtkImageGaussianSmooth>::New();
gaussianSmoothFilter->SetInputConnection(reader->GetOutputPort());
gaussianSmoothFilter->SetDimensionality(2);
gaussianSmoothFilter->SetRadiusFactor(5);
gaussianSmoothFilter->SetStandardDeviation(3);
gaussianSmoothFilter->Update();
vtkSmartPointer<vtkImageActor> originalActor =
vtkSmartPointer<vtkImageActor>::New();
originalActor->SetInputData(reader->GetOutput());
vtkSmartPointer<vtkImageActor> smoothedActor =
vtkSmartPointer<vtkImageActor>::New();
smoothedActor->SetInputData(gaussianSmoothFilter->GetOutput());
double originalViewport[4] = {0.0, 0.0, 0.5, 1.0};
double smoothedViewport[4] = {0.5, 0.0, 1.0, 1.0};
vtkSmartPointer<vtkRenderer> originalRenderer =
vtkSmartPointer<vtkRenderer>::New();
originalRenderer->SetViewport(originalViewport);
originalRenderer->AddActor(originalActor);
originalRenderer->ResetCamera();
originalRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderer> gradientMagnitudeRenderer =
vtkSmartPointer<vtkRenderer>::New();
gradientMagnitudeRenderer->SetViewport(smoothedViewport);
gradientMagnitudeRenderer->AddActor(smoothedActor);
gradientMagnitudeRenderer->ResetCamera();
gradientMagnitudeRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(originalRenderer);
renderWindow->AddRenderer(gradientMagnitudeRenderer);
renderWindow->SetSize(640, 320);
renderWindow->Render();
renderWindow->SetWindowName("GaussianFilterExample");
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkInteractorStyleImage> style =
vtkSmartPointer<vtkInteractorStyleImage>::New();
renderWindowInteractor->SetInteractorStyle(style);
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
实例42:中值滤波(图像光滑)
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkSmartPointer.h>
#include <vtkImageData.h>
#include <vtkImageConvolve.h>
#include <vtkImageMandelbrotSource.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkRenderer.h>
#include <vtkImageActor.h>
#include <vtkImageCast.h>
#include <vtkJPEGReader.h>
#include <vtkImageShiftScale.h>
//测试图像:../data/lena-gray.jpg
int main(int argc, char* argv[])
{
vtkSmartPointer<vtkJPEGReader> reader =
vtkSmartPointer<vtkJPEGReader>::New();
reader->SetFileName("lena-gray.jpg");
reader->Update();
vtkSmartPointer<vtkImageCast> originalCastFilter =
vtkSmartPointer<vtkImageCast>::New();
originalCastFilter->SetInputConnection(reader->GetOutputPort());
originalCastFilter->SetOutputScalarTypeToFloat();
originalCastFilter->Update();
vtkSmartPointer<vtkImageConvolve> convolveFilter =
vtkSmartPointer<vtkImageConvolve>::New();
convolveFilter->SetInputConnection(originalCastFilter->GetOutputPort());
double kernel[25] = {0.04,0.04,0.04,0.04,0.04,
0.04,0.04,0.04,0.04,0.04,
0.04,0.04,0.04,0.04,0.04,
0.04,0.04,0.04,0.04,0.04,
0.04,0.04,0.04,0.04,0.04 };
convolveFilter->SetKernel5x5(kernel);
convolveFilter->Update();
vtkSmartPointer<vtkImageCast> convCastFilter =
vtkSmartPointer<vtkImageCast>::New();
convCastFilter->SetInputData(convolveFilter->GetOutput());
convCastFilter->SetOutputScalarTypeToUnsignedChar();
convCastFilter->Update();
vtkSmartPointer<vtkImageActor> originalActor =
vtkSmartPointer<vtkImageActor>::New();
originalActor->SetInputData(reader->GetOutput());
vtkSmartPointer<vtkImageActor> convolvedActor =
vtkSmartPointer<vtkImageActor>::New();
convolvedActor->SetInputData(convCastFilter->GetOutput());
double leftViewport[4] = {0.0, 0.0, 0.5, 1.0};
double rightViewport[4] = {0.5, 0.0, 1.0, 1.0};
vtkSmartPointer<vtkRenderer> originalRenderer =
vtkSmartPointer<vtkRenderer>::New();
originalRenderer->SetViewport(leftViewport);
originalRenderer->AddActor(originalActor);
originalRenderer->SetBackground(1.0, 1.0, 1.0);
originalRenderer->ResetCamera();
vtkSmartPointer<vtkRenderer> convolvedRenderer =
vtkSmartPointer<vtkRenderer>::New();
convolvedRenderer->SetViewport(rightViewport);
convolvedRenderer->AddActor(convolvedActor);
convolvedRenderer->SetBackground(1.0, 1.0, 1.0);
convolvedRenderer->ResetCamera();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();;
renderWindow->AddRenderer(originalRenderer);
renderWindow->AddRenderer(convolvedRenderer);
renderWindow->SetSize(640, 320);
renderWindow->Render();
renderWindow->SetWindowName("MeanFilterExample");
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkInteractorStyleImage> style =
vtkSmartPointer<vtkInteractorStyleImage>::New();
renderWindowInteractor->SetInteractorStyle(style);
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
实例43:各向异性滤波(图像光滑)
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkSmartPointer.h>
#include <vtkCamera.h>
#include <vtkImageActor.h>
#include <vtkImageAnisotropicDiffusion2D.h>
#include <vtkImageAnisotropicDiffusion3D.h>
#include <vtkImageCast.h>
#include <vtkImageData.h>
#include <vtkImageMandelbrotSource.h>
#include <vtkInteractorStyleImage.h>
#include <vtkJPEGReader.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
//测试图像:../data/lena-gray.jpg
int main(int argc, char* argv[])
{
vtkSmartPointer<vtkJPEGReader> reader =
vtkSmartPointer<vtkJPEGReader>::New();
reader->SetFileName("lena-gray.jpg");
vtkSmartPointer<vtkImageAnisotropicDiffusion2D> diffusion =
vtkSmartPointer<vtkImageAnisotropicDiffusion2D>::New();
diffusion->SetInputConnection(reader->GetOutputPort());
diffusion->SetNumberOfIterations(10);
diffusion->SetDiffusionThreshold(20);
diffusion->Update();
vtkSmartPointer<vtkImageActor> originalActor =
vtkSmartPointer<vtkImageActor>::New();
originalActor->SetInputData(reader->GetOutput());
vtkSmartPointer<vtkImageActor> diffusionActor =
vtkSmartPointer<vtkImageActor>::New();
diffusionActor->SetInputData(diffusion->GetOutput());
double leftViewport[4] = {0.0, 0.0, 0.5, 1.0};
double rightViewport[4] = {0.5, 0.0, 1.0, 1.0};
vtkSmartPointer<vtkCamera> camera =
vtkSmartPointer<vtkCamera>::New();
vtkSmartPointer<vtkRenderer> leftRenderer =
vtkSmartPointer<vtkRenderer>::New();
leftRenderer->SetViewport(leftViewport);
leftRenderer->AddActor(originalActor);
leftRenderer->SetBackground(1.0, 1.0, 1.0);
leftRenderer->SetActiveCamera(camera);
leftRenderer->ResetCamera();
vtkSmartPointer<vtkRenderer> rightRenderer =
vtkSmartPointer<vtkRenderer>::New();
rightRenderer->SetViewport(rightViewport);
rightRenderer->SetBackground(1.0, 1.0, 1.0);
rightRenderer->AddActor(diffusionActor);
rightRenderer->SetActiveCamera(camera);
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(leftRenderer);
renderWindow->AddRenderer(rightRenderer);
renderWindow->SetSize(640, 320);
renderWindow->Render();
renderWindow->SetWindowName("AnistropicFilteringExample");
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkInteractorStyleImage> style =
vtkSmartPointer<vtkInteractorStyleImage>::New();
renderWindowInteractor->SetInteractorStyle(style);
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
实例44:二维图像快速傅里叶变换(频域处理)
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkSmartPointer.h>
#include <vtkImageData.h>
#include <vtkImageFFT.h>
#include <vtkImageRFFT.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkRenderer.h>
#include <vtkImageActor.h>
#include <vtkImageCast.h>
#include <vtkJPEGReader.h>
#include <vtkImageExtractComponents.h>
#include <vtkImageFourierCenter.h>
#include <vtkImageMagnitude.h>
#include <vtkImageShiftScale.h>
//测试图像:../data/lena-gray.jpg
int main(int argc, char* argv[])
{
vtkSmartPointer<vtkJPEGReader> reader =
vtkSmartPointer<vtkJPEGReader>::New();
reader->SetFileName ("lena-gray.jpg");
reader->Update();
vtkSmartPointer<vtkImageFFT> fftFilter =
vtkSmartPointer<vtkImageFFT>::New();
fftFilter->SetInputConnection(reader->GetOutputPort());
fftFilter->SetDimensionality(2);
fftFilter->Update();
vtkSmartPointer<vtkImageExtractComponents> fftExtractReal =
vtkSmartPointer<vtkImageExtractComponents>::New();
fftExtractReal->SetInputConnection(fftFilter->GetOutputPort());
fftExtractReal->SetComponents(0);
double range[2];
fftExtractReal->GetOutput()->GetScalarRange(range);
vtkSmartPointer<vtkImageShiftScale> ShiftScale =
vtkSmartPointer<vtkImageShiftScale>::New();
ShiftScale->SetOutputScalarTypeToUnsignedChar();
ShiftScale->SetScale( 255.0 / (range[1]-range[0]) );
ShiftScale->SetShift(-range[0]);
ShiftScale->SetInputConnection(fftExtractReal->GetOutputPort());
ShiftScale->Update();
vtkSmartPointer<vtkImageRFFT> rfftFilter =
vtkSmartPointer<vtkImageRFFT>::New();
rfftFilter->SetInputConnection(fftFilter->GetOutputPort());
rfftFilter->SetDimensionality(2);
rfftFilter->Update();
vtkSmartPointer<vtkImageExtractComponents> ifftExtractReal =
vtkSmartPointer<vtkImageExtractComponents>::New();
ifftExtractReal->SetInputConnection(rfftFilter->GetOutputPort());
ifftExtractReal->SetComponents(0);
vtkSmartPointer<vtkImageCast> rfftCastFilter =
vtkSmartPointer<vtkImageCast>::New();
rfftCastFilter->SetInputConnection(ifftExtractReal->GetOutputPort());
rfftCastFilter->SetOutputScalarTypeToUnsignedChar();
rfftCastFilter->Update();
vtkSmartPointer<vtkImageActor> originalActor =
vtkSmartPointer<vtkImageActor>::New();
originalActor->SetInputData(reader->GetOutput());
vtkSmartPointer<vtkImageActor> fftActor =
vtkSmartPointer<vtkImageActor>::New();
fftActor->SetInputData(ShiftScale->GetOutput());
vtkSmartPointer<vtkImageActor> rfftActor =
vtkSmartPointer<vtkImageActor>::New();
rfftActor->SetInputData(rfftCastFilter->GetOutput());
double originalViewport[4] = {0.0, 0.0, 0.33, 1.0};
double fftViewport[4] = {0.33, 0.0, 0.66, 1.0};
double rfftViewport[4] = {0.66, 0.0, 1.0, 1.0};
vtkSmartPointer<vtkRenderer> originalRenderer =
vtkSmartPointer<vtkRenderer>::New();
originalRenderer->SetViewport(originalViewport);
originalRenderer->AddActor(originalActor);
originalRenderer->ResetCamera();
originalRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderer> fftRenderer =
vtkSmartPointer<vtkRenderer>::New();
fftRenderer->SetViewport(fftViewport);
fftRenderer->AddActor(fftActor);
fftRenderer->ResetCamera();
fftRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderer> rfftRenderer =
vtkSmartPointer<vtkRenderer>::New();
rfftRenderer->SetViewport(rfftViewport);
rfftRenderer->AddActor(rfftActor);
rfftRenderer->ResetCamera();
rfftRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(originalRenderer);
renderWindow->AddRenderer(fftRenderer);
renderWindow->AddRenderer(rfftRenderer);
renderWindow->SetSize(640, 320);
renderWindow->Render();
renderWindow->SetWindowName("FFTAndRFFTExample");
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<vtkInteractorStyleImage> style =
vtkSmartPointer<vtkInteractorStyleImage>::New();
renderWindowInteractor->SetInteractorStyle(style);
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
实例45:理想低通滤波器(频域处理)
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkSmartPointer.h>
#include <vtkImageData.h>
#include <vtkImageIdealLowPass.h>
#include <vtkImageActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkJPEGReader.h>
#include <vtkImageFFT.h>
#include <vtkImageRFFT.h>
#include <vtkImageExtractComponents.h>
#include <vtkImageCast.h>
//测试图像:../data/lena-gray.jpg
int main(int argc, char* argv[])
{
vtkSmartPointer<vtkJPEGReader> reader =
vtkSmartPointer<vtkJPEGReader>::New();
reader->SetFileName("lena-gray.jpg");
reader->Update();
vtkSmartPointer<vtkImageFFT> fftFilter =
vtkSmartPointer<vtkImageFFT>::New();
fftFilter->SetInputConnection(reader->GetOutputPort());
fftFilter->Update();
vtkSmartPointer<vtkImageIdealLowPass> lowPassFilter =
vtkSmartPointer<vtkImageIdealLowPass>::New();
lowPassFilter->SetInputConnection(fftFilter->GetOutputPort());
lowPassFilter->SetXCutOff(0.05);
lowPassFilter->SetYCutOff(0.05);
lowPassFilter->Update();
vtkSmartPointer<vtkImageRFFT> rfftFilter =
vtkSmartPointer<vtkImageRFFT>::New();
rfftFilter->SetInputConnection(lowPassFilter->GetOutputPort());
rfftFilter->Update();
vtkSmartPointer<vtkImageExtractComponents> ifftExtractReal =
vtkSmartPointer<vtkImageExtractComponents>::New();
ifftExtractReal->SetInputConnection(rfftFilter->GetOutputPort());
ifftExtractReal->SetComponents(0);
vtkSmartPointer<vtkImageCast> castFilter =
vtkSmartPointer<vtkImageCast>::New();
castFilter->SetInputConnection(ifftExtractReal->GetOutputPort());
castFilter->SetOutputScalarTypeToUnsignedChar();
castFilter->Update();
vtkSmartPointer<vtkImageActor> originalActor =
vtkSmartPointer<vtkImageActor>::New();
originalActor->SetInputData(reader->GetOutput());
vtkSmartPointer<vtkImageActor> erodedActor =
vtkSmartPointer<vtkImageActor>::New();
erodedActor->SetInputData(castFilter->GetOutput());
double leftViewport[4] = {0.0, 0.0, 0.5, 1.0};
double rightViewport[4] = {0.5, 0.0, 1.0, 1.0};
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->SetSize(640, 320);
renderWindow->Render();
renderWindow->SetWindowName("IdealLowPassExample");
vtkSmartPointer<vtkRenderWindowInteractor> interactor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
interactor->SetRenderWindow(renderWindow);
vtkSmartPointer<vtkRenderer> leftRenderer =
vtkSmartPointer<vtkRenderer>::New();
renderWindow->AddRenderer(leftRenderer);
leftRenderer->SetViewport(leftViewport);
leftRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderer> rightRenderer =
vtkSmartPointer<vtkRenderer>::New();
renderWindow->AddRenderer(rightRenderer);
rightRenderer->SetViewport(rightViewport);
rightRenderer->SetBackground(1.0, 1.0, 1.0);
leftRenderer->AddActor(originalActor);
rightRenderer->AddActor(erodedActor);
leftRenderer->ResetCamera();
rightRenderer->ResetCamera();
renderWindow->Render();
interactor->Start();
return EXIT_SUCCESS;
}
实例46:巴特沃斯低通滤波器(频域处理)
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkSmartPointer.h>
#include <vtkImageData.h>
#include <vtkImageButterworthLowPass.h>
#include <vtkImageActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkJPEGReader.h>
#include <vtkImageFFT.h>
#include <vtkImageRFFT.h>
#include <vtkImageExtractComponents.h>
#include <vtkImageCast.h>
//测试图像:../data/lena-gray.jpg
int main(int argc, char* argv[])
{
vtkSmartPointer<vtkJPEGReader> reader =
vtkSmartPointer<vtkJPEGReader>::New();
reader->SetFileName("lena-gray.jpg");
reader->Update();
vtkSmartPointer<vtkImageFFT> fftFilter =
vtkSmartPointer<vtkImageFFT>::New();
fftFilter->SetInputConnection(reader->GetOutputPort());
fftFilter->Update();
vtkSmartPointer<vtkImageButterworthLowPass> lowPassFilter =
vtkSmartPointer<vtkImageButterworthLowPass>::New();
lowPassFilter->SetInputConnection(fftFilter->GetOutputPort());
lowPassFilter->SetXCutOff(0.05);
lowPassFilter->SetYCutOff(0.05);
lowPassFilter->Update();
vtkSmartPointer<vtkImageRFFT> rfftFilter =
vtkSmartPointer<vtkImageRFFT>::New();
rfftFilter->SetInputConnection(lowPassFilter->GetOutputPort());
rfftFilter->Update();
vtkSmartPointer<vtkImageExtractComponents> ifftExtractReal =
vtkSmartPointer<vtkImageExtractComponents>::New();
ifftExtractReal->SetInputConnection(rfftFilter->GetOutputPort());
ifftExtractReal->SetComponents(0);
vtkSmartPointer<vtkImageCast> castFilter =
vtkSmartPointer<vtkImageCast>::New();
castFilter->SetInputConnection(ifftExtractReal->GetOutputPort());
castFilter->SetOutputScalarTypeToUnsignedChar();
castFilter->Update();
vtkSmartPointer<vtkImageActor> originalActor =
vtkSmartPointer<vtkImageActor>::New();
originalActor->SetInputData(reader->GetOutput());
vtkSmartPointer<vtkImageActor> erodedActor =
vtkSmartPointer<vtkImageActor>::New();
erodedActor->SetInputData(castFilter->GetOutput());
double leftViewport[4] = {0.0, 0.0, 0.5, 1.0};
double rightViewport[4] = {0.5, 0.0, 1.0, 1.0};
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->SetSize(640, 320);
renderWindow->Render();
renderWindow->SetWindowName("ButterworthLowPassExample");
vtkSmartPointer<vtkRenderWindowInteractor> interactor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
interactor->SetRenderWindow(renderWindow);
vtkSmartPointer<vtkRenderer> leftRenderer =
vtkSmartPointer<vtkRenderer>::New();
renderWindow->AddRenderer(leftRenderer);
leftRenderer->SetViewport(leftViewport);
leftRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderer> rightRenderer =
vtkSmartPointer<vtkRenderer>::New();
renderWindow->AddRenderer(rightRenderer);
rightRenderer->SetViewport(rightViewport);
rightRenderer->SetBackground(1.0, 1.0, 1.0);
leftRenderer->AddActor(originalActor);
rightRenderer->AddActor(erodedActor);
leftRenderer->ResetCamera();
rightRenderer->ResetCamera();
renderWindow->Render();
interactor->Start();
return EXIT_SUCCESS;
}
实例47:理想高通滤波器(频域处理)
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkSmartPointer.h>
#include <vtkImageData.h>
#include <vtkImageIdealHighPass.h>
#include <vtkImageActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkJPEGReader.h>
#include <vtkImageFFT.h>
#include <vtkImageRFFT.h>
#include <vtkImageExtractComponents.h>
#include <vtkImageCast.h>
//测试图像:../data/lena-gray.jpg
int main(int argc, char* argv[])
{
vtkSmartPointer<vtkJPEGReader> reader =
vtkSmartPointer<vtkJPEGReader>::New();
reader->SetFileName("lena-gray.jpg");
reader->Update();
vtkSmartPointer<vtkImageFFT> fftFilter =
vtkSmartPointer<vtkImageFFT>::New();
fftFilter->SetInputConnection(reader->GetOutputPort());
fftFilter->Update();
vtkSmartPointer<vtkImageIdealHighPass> highPassFilter =
vtkSmartPointer<vtkImageIdealHighPass>::New();
highPassFilter->SetInputConnection(fftFilter->GetOutputPort());
highPassFilter->SetXCutOff(0.1);
highPassFilter->SetYCutOff(0.1);
highPassFilter->Update();
vtkSmartPointer<vtkImageRFFT> rfftFilter =
vtkSmartPointer<vtkImageRFFT>::New();
rfftFilter->SetInputConnection(highPassFilter->GetOutputPort());
rfftFilter->Update();
vtkSmartPointer<vtkImageExtractComponents> ifftExtractReal =
vtkSmartPointer<vtkImageExtractComponents>::New();
ifftExtractReal->SetInputConnection(rfftFilter->GetOutputPort());
ifftExtractReal->SetComponents(0);
vtkSmartPointer<vtkImageCast> castFilter =
vtkSmartPointer<vtkImageCast>::New();
castFilter->SetInputConnection(ifftExtractReal->GetOutputPort());
castFilter->SetOutputScalarTypeToUnsignedChar();
castFilter->Update();
vtkSmartPointer<vtkImageActor> originalActor =
vtkSmartPointer<vtkImageActor>::New();
originalActor->SetInputData(reader->GetOutput());
vtkSmartPointer<vtkImageActor> erodedActor =
vtkSmartPointer<vtkImageActor>::New();
erodedActor->SetInputData(castFilter->GetOutput());
double leftViewport[4] = {0.0, 0.0, 0.5, 1.0};
double rightViewport[4] = {0.5, 0.0, 1.0, 1.0};
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->SetSize(640, 320);
renderWindow->Render();
renderWindow->SetWindowName("IdealHighPassExample");
vtkSmartPointer<vtkRenderWindowInteractor> interactor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
interactor->SetRenderWindow(renderWindow);
vtkSmartPointer<vtkRenderer> leftRenderer =
vtkSmartPointer<vtkRenderer>::New();
renderWindow->AddRenderer(leftRenderer);
leftRenderer->SetViewport(leftViewport);
leftRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderer> rightRenderer =
vtkSmartPointer<vtkRenderer>::New();
renderWindow->AddRenderer(rightRenderer);
rightRenderer->SetViewport(rightViewport);
rightRenderer->SetBackground(1.0, 1.0, 1.0);
leftRenderer->AddActor(originalActor);
rightRenderer->AddActor(erodedActor);
leftRenderer->ResetCamera();
rightRenderer->ResetCamera();
renderWindow->Render();
interactor->Start();
return EXIT_SUCCESS;
}
实例48:巴沃斯特高通滤波器(频域处理)
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkSmartPointer.h>
#include <vtkImageData.h>
#include <vtkImageButterworthHighPass.h>
#include <vtkImageActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkJPEGReader.h>
#include <vtkImageFFT.h>
#include <vtkImageRFFT.h>
#include <vtkImageExtractComponents.h>
#include <vtkImageCast.h>
//测试图像:../data/lena-gray.jpg
int main(int argc, char* argv[])
{
vtkSmartPointer<vtkJPEGReader> reader =
vtkSmartPointer<vtkJPEGReader>::New();
reader->SetFileName("lena-gray.jpg");
reader->Update();
vtkSmartPointer<vtkImageFFT> fftFilter =
vtkSmartPointer<vtkImageFFT>::New();
fftFilter->SetInputConnection(reader->GetOutputPort());
fftFilter->Update();
vtkSmartPointer<vtkImageButterworthHighPass> highPassFilter =
vtkSmartPointer<vtkImageButterworthHighPass>::New();
highPassFilter->SetInputConnection(fftFilter->GetOutputPort());
highPassFilter->SetXCutOff(0.1);
highPassFilter->SetYCutOff(0.1);
highPassFilter->Update();
vtkSmartPointer<vtkImageRFFT> rfftFilter =
vtkSmartPointer<vtkImageRFFT>::New();
rfftFilter->SetInputConnection(highPassFilter->GetOutputPort());
rfftFilter->Update();
vtkSmartPointer<vtkImageExtractComponents> ifftExtractReal =
vtkSmartPointer<vtkImageExtractComponents>::New();
ifftExtractReal->SetInputConnection(rfftFilter->GetOutputPort());
ifftExtractReal->SetComponents(0);
vtkSmartPointer<vtkImageCast> castFilter =
vtkSmartPointer<vtkImageCast>::New();
castFilter->SetInputConnection(ifftExtractReal->GetOutputPort());
castFilter->SetOutputScalarTypeToUnsignedChar();
castFilter->Update();
vtkSmartPointer<vtkImageActor> originalActor =
vtkSmartPointer<vtkImageActor>::New();
originalActor->SetInputData(reader->GetOutput());
vtkSmartPointer<vtkImageActor> erodedActor =
vtkSmartPointer<vtkImageActor>::New();
erodedActor->SetInputData(castFilter->GetOutput());
double leftViewport[4] = {0.0, 0.0, 0.5, 1.0};
double rightViewport[4] = {0.5, 0.0, 1.0, 1.0};
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->SetSize(640, 320);
renderWindow->Render();
renderWindow->SetWindowName("ButterworthHighPassExample");
vtkSmartPointer<vtkRenderWindowInteractor> interactor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
interactor->SetRenderWindow(renderWindow);
vtkSmartPointer<vtkRenderer> leftRenderer =
vtkSmartPointer<vtkRenderer>::New();
renderWindow->AddRenderer(leftRenderer);
leftRenderer->SetViewport(leftViewport);
leftRenderer->SetBackground(1.0, 1.0, 1.0);
vtkSmartPointer<vtkRenderer> rightRenderer =
vtkSmartPointer<vtkRenderer>::New();
renderWindow->AddRenderer(rightRenderer);
rightRenderer->SetViewport(rightViewport);
rightRenderer->SetBackground(1.0, 1.0, 1.0);
leftRenderer->AddActor(originalActor);
rightRenderer->AddActor(erodedActor);
leftRenderer->ResetCamera();
rightRenderer->ResetCamera();
renderWindow->Render();
interactor->Start();
return EXIT_SUCCESS;
}