基于C++的水灵VTK8学习笔记2(VTK图像处理)

VTK资料分享:张晓东的《VTK图形图像开发进阶》全套资料(包含书籍电子版,配套每章节所有源码、实现效果图和工程配置文件):
https://download.csdn.net/download/qq_32809093/12550945
水灵VTK学习全套资料(包含全部视频、PPT、源码和每个项目对应的工程配置文件CmakeLists.txt):
https://download.csdn.net/download/qq_32809093/12550933
水灵VT视频在线观看:https://www.ixigua.com/6834676787503432203

本文参考的主要是张晓东的《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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main()
{
	//定义一个vtkImageCanvasSource2D类型对象canvas
	vtkSmartPointer canvas =
		vtkSmartPointer::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 redActor =
		vtkSmartPointer::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 redRenderer =
		vtkSmartPointer::New();
	redRenderer->SetViewport(redViewport);
	redRenderer->AddActor(redActor);
	redRenderer->ResetCamera();
	redRenderer->SetBackground(1.0, 1.0, 1.0);

	// Setup render window
	vtkSmartPointer renderWindow =
		vtkSmartPointer::New();
	renderWindow->AddRenderer(redRenderer);
	renderWindow->SetSize( 640, 480 );
	renderWindow->Render();
	renderWindow->SetWindowName("ImageCanvasSource2D");

	// Setup render window interactor
	vtkSmartPointer renderWindowInteractor =
		vtkSmartPointer::New();
	vtkSmartPointer style =
		vtkSmartPointer::New();

	renderWindowInteractor->SetInteractorStyle(style);
	// Render and start interaction
	renderWindowInteractor->SetRenderWindow(renderWindow);
	renderWindowInteractor->Initialize();
	renderWindowInteractor->Start();

	return EXIT_SUCCESS;
}

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第1张图片

实例20:用vtkImageViewer2显示三维医学图像mhd的某个切面

#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

#include 
#include 
#include 
#include 
#include 
#include //mhd类图像处理类

//测试图像:../data/brain.mhd
int main(int argc, char* argv[])
{
	vtkSmartPointer reader =
		vtkSmartPointer::New();
	//注意:mhd和raw文件要放在同一个文件目录
	reader->SetFileName("C:\\Users\\Administrator\\Desktop\\VTK2\\hellovtk\\vtk_图像处理学习\\第五章_图像处理\\data\\brain.mhd");
	reader->Update();

	vtkSmartPointer imageViewer =
		vtkSmartPointer::New();
	imageViewer->SetInputData(reader->GetOutput());

	vtkSmartPointer renderWindowInteractor =
		vtkSmartPointer::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;
}

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第2张图片基于C++的水灵VTK8学习笔记2(VTK图像处理)_第3张图片                           基于C++的水灵VTK8学习笔记2(VTK图像处理)_第4张图片

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第5张图片

实例21:BMP图像信息的访问(图像维数、原点坐标和像素间隔)

#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

//测试图像:../data/lena.bmp
int main(int argc, char* argv[])
{
	/*if (argc < 2)
	{
		std::cout< reader =
		vtkSmartPointer::New();
	//reader->SetFileName ( argv[1] );
	reader->SetFileName("1234.bmp");//读取图片
	reader->Update();

	int dims[3];
	reader->GetOutput()->GetDimensions(dims);
	std::cout<<"图像维数:" <GetOutput()->GetOrigin(origin);
	std::cout<<"图像原点:" <GetOutput()->GetSpacing(spaceing);
	std::cout<<"像素间隔:" < imageViewer =
		vtkSmartPointer::New();
	imageViewer->SetInputConnection(reader->GetOutputPort());

	vtkSmartPointer renderWindowInteractor =
		vtkSmartPointer::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;
}

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第6张图片     

实例22:JPG图像信息的访问(图像维数、原点坐标和像素间隔)

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

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include //JPEG图像读取类
#include 

//测试图像:../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 imageViewer =
		vtkSmartPointer::New();
	imageViewer->SetInputConnection(reader->GetOutputPort());

	vtkSmartPointer renderWindowInteractor =
		vtkSmartPointer::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;
}

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第7张图片  基于C++的水灵VTK8学习笔记2(VTK图像处理)_第8张图片

实例23:DCM医学图像信息的访问(图像维数、原点坐标和像素间隔)

#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

#include 
#include 
#include 
#include 
#include 
#include 
#include 
//#include 
#include 
#include //DCM医学文件读取类

//测试图像:../data/lena.bmp
int main(int argc, char* argv[])
{
	/*vtkSmartPointer reader =
		vtkSmartPointer::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<<"图像维数:" <GetOutput()->GetOrigin(origin);
	std::cout<<"图像原点:" <GetOutput()->GetSpacing(spaceing);
	std::cout<<"像素间隔:" < imageViewer =
		vtkSmartPointer::New();
	imageViewer->SetInputConnection(reader->GetOutputPort());

	vtkSmartPointer renderWindowInteractor =
		vtkSmartPointer::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;
}

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第9张图片          

实例24:BMP图像信息的修改(图像维数、原点坐标和像素间隔)

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第10张图片

#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

//测试图像:../data/lena.bmp
int main(int argc, char* argv[])
{
	/*if (argc < 2)
	{
		std::cout< reader =
		vtkSmartPointer::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<<"原图像维数:" <GetOutput()->GetOrigin(origin);
	std::cout<<"原图像原点:" <GetOutput()->GetSpacing(spaceing);
	std::cout<<"原像素间隔:" < changer =
		vtkSmartPointer::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<<"修改后图像维数:" <GetOutput()->GetOrigin(origin);
	std::cout<<"修改后图像原点:" <GetOutput()->GetSpacing(spaceing);
	std::cout<<"修改后像素间隔:" < imageViewer =
		vtkSmartPointer::New();
	imageViewer->SetInputConnection(changer->GetOutputPort());
	vtkSmartPointer renderWindowInteractor =
		vtkSmartPointer::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;
}

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第11张图片    基于C++的水灵VTK8学习笔记2(VTK图像处理)_第12张图片

实例25:BMP图像像素值的访问与修改

#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

//测试图像:../data/lena.bmp
int main(int argc, char* argv[])
{
	vtkSmartPointer reader =
	vtkSmartPointer::New();
	reader->SetFileName ( "lena.bmp" );
	reader->Update();

	int subRegion[6] = {0,300, 0, 300, 0, 0};
	vtkImageIterator 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 imageViewer =
		vtkSmartPointer::New();
	imageViewer->SetInputConnection(reader->GetOutputPort());

	vtkSmartPointer renderWindowInteractor =
		vtkSmartPointer::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;
}

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第13张图片

实例26:将BMP类型的RGB图像转换为灰度图像

#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

//测试图像:../data/lena.bmp
int main(int argc, char* argv[])
{
	vtkSmartPointer reader =
    vtkSmartPointer::New();
	reader->SetFileName ( "lena.bmp" );

	vtkSmartPointer luminanceFilter = 
		vtkSmartPointer::New();
	luminanceFilter->SetInputConnection(reader->GetOutputPort());
	luminanceFilter->Update();

	vtkSmartPointer originalActor =
		vtkSmartPointer::New();
	originalActor->SetInputData(reader->GetOutput());

	vtkSmartPointer shiftscaleActor =
		vtkSmartPointer::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 originalRenderer =
		vtkSmartPointer::New();
	originalRenderer->SetViewport(originalViewport);
	originalRenderer->AddActor(originalActor);
	originalRenderer->ResetCamera();
	originalRenderer->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer shiftscaleRenderer =
		vtkSmartPointer::New();
	shiftscaleRenderer->SetViewport(shiftscaleViewport);
	shiftscaleRenderer->AddActor(shiftscaleActor);
	shiftscaleRenderer->ResetCamera();
	shiftscaleRenderer->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer renderWindow =
		vtkSmartPointer::New();
	renderWindow->AddRenderer(originalRenderer);
	renderWindow->AddRenderer(shiftscaleRenderer);
	renderWindow->SetSize( 640, 320 );
	renderWindow->Render();
	renderWindow->SetWindowName("Color2GrayImageExample");

	vtkSmartPointer renderWindowInteractor =
		vtkSmartPointer::New();
	vtkSmartPointer style =
		vtkSmartPointer::New();

	renderWindowInteractor->SetInteractorStyle(style);
	renderWindowInteractor->SetRenderWindow(renderWindow);
	renderWindowInteractor->Initialize();

	renderWindowInteractor->Start();

	return EXIT_SUCCESS;
}

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第14张图片

实例27:将BMP类型的RGB图像提取各个颜色(R、G、B)组分(提取颜色组分)

VTK中利用vtkImageExtractComponents可以方便地提取彩色图像的各个颜色通道组分,提取出来的每一个组分数据就是一个灰度数据。

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第15张图片

#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
 
//测试图像:../data/lena.bmp
int main(int argc, char* argv[])
{
	vtkSmartPointer reader =
		vtkSmartPointer::New();
	reader->SetFileName ("lena.bmp");
 
	vtkSmartPointer extractRedFilter =
		vtkSmartPointer::New();
	extractRedFilter->SetInputConnection(reader->GetOutputPort());
	extractRedFilter->SetComponents(0);
	extractRedFilter->Update();
 
	vtkSmartPointer extractGreenFilter =
		vtkSmartPointer::New();
	extractGreenFilter->SetInputConnection(reader->GetOutputPort());
	extractGreenFilter->SetComponents(1);
	extractGreenFilter->Update();
 
	vtkSmartPointer extractBlueFilter =
		vtkSmartPointer::New();
	extractBlueFilter->SetInputConnection(reader->GetOutputPort());
	extractBlueFilter->SetComponents(2);
	extractBlueFilter->Update();
 
	// Create actors
	vtkSmartPointer inputActor =
		vtkSmartPointer::New();
	inputActor->SetInputData(reader->GetOutput());
 
	vtkSmartPointer redActor =
		vtkSmartPointer::New();
	redActor->SetInputData(extractRedFilter->GetOutput());
 
	vtkSmartPointer greenActor =
		vtkSmartPointer::New();
	greenActor->SetInputData(extractGreenFilter->GetOutput());
 
	vtkSmartPointer blueActor =
		vtkSmartPointer::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 inputRenderer =
		vtkSmartPointer::New();
	inputRenderer->SetViewport(inputViewport);
	inputRenderer->AddActor(inputActor);
	inputRenderer->ResetCamera();
	inputRenderer->SetBackground(1.0, 1.0, 1.0);
 
	vtkSmartPointer redRenderer =
		vtkSmartPointer::New();
	redRenderer->SetViewport(redViewport);
	redRenderer->AddActor(redActor);
	redRenderer->ResetCamera();
	redRenderer->SetBackground(1.0, 1.0, 1.0);
 
	vtkSmartPointer greenRenderer =
		vtkSmartPointer::New();
	greenRenderer->SetViewport(greenViewport);
	greenRenderer->AddActor(greenActor);
	greenRenderer->ResetCamera();
	greenRenderer->SetBackground(1.0, 1.0, 1.0);
 
	vtkSmartPointer blueRenderer =
		vtkSmartPointer::New();
	blueRenderer->SetViewport(blueViewport);
	blueRenderer->AddActor(blueActor);
	blueRenderer->ResetCamera();
	blueRenderer->SetBackground(1.0, 1.0, 1.0);
 
	vtkSmartPointer renderWindow =
		vtkSmartPointer::New();
	renderWindow->AddRenderer(inputRenderer);
	renderWindow->AddRenderer(redRenderer);
	renderWindow->AddRenderer(greenRenderer);
	renderWindow->AddRenderer(blueRenderer);

	renderWindow->SetSize(1200, 300);
	renderWindow->Render();
	renderWindow->SetWindowName("ImageExtractComponentsExample");
 
	vtkSmartPointer renderWindowInteractor =
		vtkSmartPointer::New();
	vtkSmartPointer style =
		vtkSmartPointer::New();
 
	renderWindowInteractor->SetInteractorStyle(style);
	renderWindowInteractor->SetRenderWindow(renderWindow);
	renderWindowInteractor->Initialize();
	renderWindowInteractor->Start();

	return EXIT_SUCCESS;
}

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第16张图片

实例28:将JPG类型的灰度图像进行彩色映射(图像彩色映射)

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第17张图片

#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

//测试图像:../data/lena-gray.jpg
int main(int argc, char* argv[])
{
	vtkSmartPointer reader =
		vtkSmartPointer::New();
	reader->SetFileName ( "C:\\Users\\Administrator\\Desktop\\VTK2\\hellovtk\\vtk_图像处理学习\\第五章_图像处理\\data\\lena-gray.jpg" );

	vtkSmartPointer colorTable = 
		vtkSmartPointer::New(); 
	colorTable->SetRange( 0.0, 255.0 );
	colorTable->SetHueRange( 0.1, 0.5 );
	colorTable->SetValueRange( 0.6, 1.0 );
	colorTable->Build();

	vtkSmartPointer colorMap = 
		vtkSmartPointer::New();
	colorMap->SetInputConnection( reader->GetOutputPort() );
	colorMap->SetLookupTable( colorTable );
	colorMap->Update();

	vtkSmartPointer originalActor =
		vtkSmartPointer::New();
	originalActor->SetInputData(reader->GetOutput());

	vtkSmartPointer shiftscaleActor =
		vtkSmartPointer::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 originalRenderer =
		vtkSmartPointer::New();
	originalRenderer->SetViewport(originalViewport);
	originalRenderer->AddActor(originalActor);
	originalRenderer->ResetCamera();
	originalRenderer->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer shiftscaleRenderer =
		vtkSmartPointer::New();
	shiftscaleRenderer->SetViewport(shiftscaleViewport);
	shiftscaleRenderer->AddActor(shiftscaleActor);
	shiftscaleRenderer->ResetCamera();
	shiftscaleRenderer->SetBackground(1.0, 1.0, 0.8);

	vtkSmartPointer renderWindow =
		vtkSmartPointer::New();
	renderWindow->AddRenderer(originalRenderer);
	renderWindow->AddRenderer(shiftscaleRenderer);
	renderWindow->SetSize(900, 300);
	renderWindow->Render();
	renderWindow->SetWindowName("Gray2ColorImageExample");

	vtkSmartPointer renderWindowInteractor =
		vtkSmartPointer::New();
	vtkSmartPointer style =
		vtkSmartPointer::New();

	renderWindowInteractor->SetInteractorStyle(style);
	renderWindowInteractor->SetRenderWindow(renderWindow);
	renderWindowInteractor->Initialize();
	renderWindowInteractor->Start();

	return EXIT_SUCCESS;
}

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第18张图片

实例29:多个灰度图像合成一个彩色图像(颜色合成)

#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main(int, char *[])
{
	vtkSmartPointer red =
		vtkSmartPointer::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 green =
		vtkSmartPointer::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 blue =
		vtkSmartPointer::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 appendFilter =
		vtkSmartPointer::New();
	appendFilter->SetInputConnection(0, red->GetOutputPort());
	appendFilter->AddInputConnection(0, green->GetOutputPort());
	appendFilter->AddInputConnection(0, blue->GetOutputPort());
	appendFilter->Update();

	vtkSmartPointer redActor =
		vtkSmartPointer::New();
	redActor->SetInputData(red->GetOutput());

	vtkSmartPointer greenActor =
		vtkSmartPointer::New();
	greenActor->SetInputData(green->GetOutput());

	vtkSmartPointer blueActor =
		vtkSmartPointer::New();
	blueActor->SetInputData(blue->GetOutput());

	vtkSmartPointer combinedActor =
		vtkSmartPointer::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 redRenderer =
		vtkSmartPointer::New();
	redRenderer->SetViewport(redViewport);
	redRenderer->AddActor(redActor);
	redRenderer->ResetCamera();
	redRenderer->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer greenRenderer =
		vtkSmartPointer::New();
	greenRenderer->SetViewport(greenViewport);
	greenRenderer->AddActor(greenActor);
	greenRenderer->ResetCamera();
	greenRenderer->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer blueRenderer =
		vtkSmartPointer::New();
	blueRenderer->SetViewport(blueViewport);
	blueRenderer->AddActor(blueActor);
	blueRenderer->ResetCamera();
	blueRenderer->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer combinedRenderer =
		vtkSmartPointer::New();
	combinedRenderer->SetViewport(combinedViewport);
	combinedRenderer->AddActor(combinedActor);
	combinedRenderer->ResetCamera();
	combinedRenderer->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer renderWindow =
		vtkSmartPointer::New();
	renderWindow->AddRenderer(redRenderer);
	renderWindow->AddRenderer(greenRenderer);
	renderWindow->AddRenderer(blueRenderer);
	renderWindow->AddRenderer(combinedRenderer);
	renderWindow->SetSize(1200, 300);
	renderWindow->Render();
	renderWindow->SetWindowName("ImageAppendComponentsExample");

	vtkSmartPointer renderWindowInteractor =
		vtkSmartPointer::New();
	vtkSmartPointer style =
		vtkSmartPointer::New();

	renderWindowInteractor->SetInteractorStyle(style);
	renderWindowInteractor->SetRenderWindow(renderWindow);
	renderWindowInteractor->Initialize();
	renderWindowInteractor->Start();

	return EXIT_SUCCESS;
}

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第19张图片

实例30:提取BMP图像的感兴趣区域(区域提取)

#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

//测试图像:../data/lena.bmp
int main(int argc, char* argv[])
{
	vtkSmartPointer reader =
	vtkSmartPointer::New();
	reader->SetFileName ("lena.bmp");
	reader->Update();

	int dims[3];
	reader->GetOutput()->GetDimensions(dims);

	vtkSmartPointer extractVOI =
		vtkSmartPointer::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 originalActor =
		vtkSmartPointer::New();
	originalActor->SetInputData(reader->GetOutput());

	vtkSmartPointer voiActor =
		vtkSmartPointer::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 originalRenderer =
		vtkSmartPointer::New();
	originalRenderer->SetViewport(originalViewport);
	originalRenderer->AddActor(originalActor);
	originalRenderer->ResetCamera();
	originalRenderer->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer shiftscaleRenderer =
		vtkSmartPointer::New();
	shiftscaleRenderer->SetViewport(voiviewport);
	shiftscaleRenderer->AddActor(voiActor);
	shiftscaleRenderer->ResetCamera();
	shiftscaleRenderer->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer renderWindow =
		vtkSmartPointer::New();
	renderWindow->AddRenderer(originalRenderer);
	renderWindow->AddRenderer(shiftscaleRenderer);
	renderWindow->SetSize(900, 300);
	renderWindow->Render();
	renderWindow->SetWindowName("ExtractVOIExample");


	vtkSmartPointer renderWindowInteractor =
		vtkSmartPointer::New();
	vtkSmartPointer style =
		vtkSmartPointer::New();

	renderWindowInteractor->SetInteractorStyle(style);
	renderWindowInteractor->SetRenderWindow(renderWindow);
	renderWindowInteractor->Initialize();
	renderWindowInteractor->Start();

	return EXIT_SUCCESS;
}

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第20张图片

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第21张图片

实例31:灰度图像直方图

#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

//测试图像:../data/lena-gray.jpg
int main(int argc, char* argv[])
{

	vtkSmartPointer reader =
		vtkSmartPointer::New();
	reader->SetFileName ( "lena-gray.jpg" );
	reader->Update();

	int bins   = 16;
	int comps  = 1;

	vtkSmartPointer histogram =
		vtkSmartPointer::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(histogram->GetOutput()->GetScalarPointer());

	vtkSmartPointer frequencies = 
		vtkSmartPointer::New();
	frequencies->SetNumberOfComponents(1);

	for(int j = 0; j < bins; ++j)
	{
		for(int i=0; iInsertNextTuple1(*output++);
		}
	}

	vtkSmartPointer dataObject = 
		vtkSmartPointer::New();
	dataObject->GetFieldData()->AddArray( frequencies );

	vtkSmartPointer barChart = 
		vtkSmartPointer::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 renderer = 
		vtkSmartPointer::New();
	renderer->AddActor(barChart);
	renderer->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer renderWindow = 
		vtkSmartPointer::New();
	renderWindow->AddRenderer(renderer);
	renderWindow->SetSize(640, 480);
	renderWindow->Render();
	renderWindow->SetWindowName("ImageAccumulateExample");

	vtkSmartPointer interactor =
		vtkSmartPointer::New();
	interactor->SetRenderWindow(renderWindow);

	interactor->Initialize();
	interactor->Start();

	return EXIT_SUCCESS;
}

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第22张图片

实例32:图像重采样(降采样)

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第23张图片

#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

//测试图像:../data/lena.bmp
int main(int argc, char* argv[])
{
	vtkSmartPointer reader =
		vtkSmartPointer::New();
	reader->SetFileName("lena.bmp");
	reader->Update();

	vtkSmartPointer shrinkFilter = 
		vtkSmartPointer::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<<"原图图像维数      :"< originalActor =
		vtkSmartPointer::New();
	originalActor->SetInputData( reader->GetOutput());

	vtkSmartPointer shrinkActor =
		vtkSmartPointer::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 originalRenderer =
		vtkSmartPointer::New();
	originalRenderer->SetViewport(originalViewport);
	originalRenderer->AddActor(originalActor);
	originalRenderer->ResetCamera();
	originalRenderer->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer shrinkRenderer =
		vtkSmartPointer::New();
	shrinkRenderer->SetViewport(shrinkViewport);
	shrinkRenderer->AddActor(shrinkActor);
	shrinkRenderer->ResetCamera();
	shrinkRenderer->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer renderWindow =
		vtkSmartPointer::New();
	renderWindow->AddRenderer(originalRenderer);
	renderWindow->AddRenderer(shrinkRenderer);
	renderWindow->SetSize(640, 480);
	renderWindow->Render();
	renderWindow->SetWindowName("ImageShrink3DExample");

	vtkSmartPointer renderWindowInteractor =
		vtkSmartPointer::New();
	vtkSmartPointer style =
		vtkSmartPointer::New();

	renderWindowInteractor->SetInteractorStyle(style);
	renderWindowInteractor->SetRenderWindow(renderWindow);
	renderWindowInteractor->Initialize();
	renderWindowInteractor->Start();

	return EXIT_SUCCESS;
}

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第24张图片

实例33:图像重采样(降和升采样)

#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

//测试图像:../data/lena.bmp
int main(int argc, char* argv[])
{

	vtkSmartPointer reader =
		vtkSmartPointer::New();
	reader->SetFileName ( "lena.bmp" );
	reader->Update();

	vtkSmartPointer shrinkFilter = 
		vtkSmartPointer::New();
	shrinkFilter->SetInputConnection(reader->GetOutputPort());
	shrinkFilter->SetShrinkFactors(16,16,1);
	shrinkFilter->Update();

	vtkSmartPointer magnifyFilter =
		vtkSmartPointer::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<<"原图图像维数      :"< originalActor =
		vtkSmartPointer::New();
	originalActor->SetInputData(reader->GetOutput());

	vtkSmartPointer shrinkActor =
		vtkSmartPointer::New();
	shrinkActor->SetInputData(shrinkFilter->GetOutput());

	vtkSmartPointer magnifyActor =
		vtkSmartPointer::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 originalRenderer =
		vtkSmartPointer::New();
	originalRenderer->SetViewport(originalViewport);
	originalRenderer->AddActor(originalActor);
	originalRenderer->ResetCamera();
	originalRenderer->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer shrinkRenderer =
		vtkSmartPointer::New();
	shrinkRenderer->SetViewport(shrinkViewport);
	shrinkRenderer->AddActor(shrinkActor);
	shrinkRenderer->ResetCamera();
	shrinkRenderer->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer magnifyRenderer =
		vtkSmartPointer::New();
	magnifyRenderer->SetViewport(magnifyViewport);
	magnifyRenderer->AddActor(magnifyActor);
	magnifyRenderer->ResetCamera();
	magnifyRenderer->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer renderWindow =
		vtkSmartPointer::New();
	renderWindow->AddRenderer(originalRenderer);
	renderWindow->AddRenderer(shrinkRenderer);
	renderWindow->AddRenderer(magnifyRenderer);
	renderWindow->SetSize(640, 320);
	renderWindow->Render();
	renderWindow->SetWindowName("ImageShrinkMagnifyExample");

	vtkSmartPointer renderWindowInteractor =
		vtkSmartPointer::New();
	vtkSmartPointer style =
		vtkSmartPointer::New();

	renderWindowInteractor->SetInteractorStyle(style);
	renderWindowInteractor->SetRenderWindow(renderWindow);
	renderWindowInteractor->Initialize();
	renderWindowInteractor->Start();

	return EXIT_SUCCESS;
}

 

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第25张图片基于C++的水灵VTK8学习笔记2(VTK图像处理)_第26张图片

实例34:图像运算(数字运算)

#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main(int, char *[])
{
	vtkSmartPointer imageSource = 
		vtkSmartPointer::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 imageMath = 
		vtkSmartPointer::New();
	imageMath->SetOperationToMultiplyByK();
	imageMath->SetConstantK(2.0);
	imageMath->SetInputConnection(imageSource->GetOutputPort());
	imageMath->Update();

	vtkSmartPointer originalActor =
		vtkSmartPointer::New();
	originalActor->SetInputData(imageSource->GetOutput());

	vtkSmartPointer mathActor =
		vtkSmartPointer::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 originalRenderer =
		vtkSmartPointer::New();
	originalRenderer->SetViewport(leftViewport);
	originalRenderer->AddActor(originalActor);
	originalRenderer->ResetCamera();
	originalRenderer->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer gradientMagnitudeRenderer =
		vtkSmartPointer::New();
	gradientMagnitudeRenderer->SetViewport(rightViewport);
	gradientMagnitudeRenderer->AddActor(mathActor);
	gradientMagnitudeRenderer->ResetCamera();
	gradientMagnitudeRenderer->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer renderWindow =
		vtkSmartPointer::New();
	renderWindow->AddRenderer(originalRenderer);
	renderWindow->AddRenderer(gradientMagnitudeRenderer);
	renderWindow->SetSize(640, 480);
	renderWindow->Render();
	renderWindow->SetWindowName("ImageMathematicsExample");

	vtkSmartPointer renderWindowInteractor =
		vtkSmartPointer::New();
	vtkSmartPointer style =
		vtkSmartPointer::New();

	renderWindowInteractor->SetInteractorStyle(style);
	renderWindowInteractor->SetRenderWindow(renderWindow);
	renderWindowInteractor->Initialize();
	renderWindowInteractor->Start();

	return EXIT_SUCCESS;
}

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第27张图片

实例35:图像运算(逻辑运算)

#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main(int, char *[])
{
	vtkSmartPointer imageSource1 =
		vtkSmartPointer::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 imageSource2 = 
		vtkSmartPointer::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 imageLogic = 
		vtkSmartPointer::New();
	imageLogic->SetInputData(imageSource1->GetOutput());
	imageLogic->SetInputData(imageSource2->GetOutput());
	imageLogic->SetOperationToXor();
	imageLogic->SetOutputTrueValue(128);
	imageLogic->Update();

	vtkSmartPointer originalActor1 =
		vtkSmartPointer::New();
	originalActor1->SetInputData(imageSource1->GetOutput());

	vtkSmartPointer originalActor2 =
		vtkSmartPointer::New();
	originalActor2->SetInputData(imageSource2->GetOutput());

	vtkSmartPointer logicActor =
		vtkSmartPointer::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 originalRenderer1 =
		vtkSmartPointer::New();
	originalRenderer1->SetViewport(leftViewport);
	originalRenderer1->AddActor(originalActor1);
	originalRenderer1->ResetCamera();
	originalRenderer1->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer originalRenderer2 =
		vtkSmartPointer::New();
	originalRenderer2->SetViewport(midViewport);
	originalRenderer2->AddActor(originalActor2);
	originalRenderer2->ResetCamera();
	originalRenderer2->SetBackground(0.8, 0.8, 0.8);

	vtkSmartPointer logicRenderer =
		vtkSmartPointer::New();
	logicRenderer->SetViewport(rightViewport);
	logicRenderer->AddActor(logicActor);
	logicRenderer->ResetCamera();
	logicRenderer->SetBackground(0.6, 0.6, 0.6);

	vtkSmartPointer renderWindow =
		vtkSmartPointer::New();
	renderWindow->AddRenderer(originalRenderer1);
	renderWindow->AddRenderer(originalRenderer2);
	renderWindow->AddRenderer(logicRenderer);
	renderWindow->SetSize(640, 320);
	renderWindow->Render();
	renderWindow->SetWindowName("ImageLogicExample");

	vtkSmartPointer renderWindowInteractor =
		vtkSmartPointer::New();
	vtkSmartPointer style =
		vtkSmartPointer::New();

	renderWindowInteractor->SetInteractorStyle(style);
	renderWindowInteractor->SetRenderWindow(renderWindow);
	renderWindowInteractor->Initialize();
	renderWindowInteractor->Start();

	return EXIT_SUCCESS;
}

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第28张图片

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第29张图片

实例36:图像二值化

#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

//测试图像:../data/lena-gray.jpg
int main(int argc, char* argv[])
{
	vtkSmartPointer reader =
		vtkSmartPointer::New();
	reader->SetFileName("lena-gray.jpg");
	reader->Update();

	vtkSmartPointer thresholdFilter =
		vtkSmartPointer::New();
	thresholdFilter->SetInputConnection(reader->GetOutputPort());
	thresholdFilter->ThresholdByUpper(100);//设置阈值为100
	thresholdFilter->SetInValue(255);//大于100的设置为255
	thresholdFilter->SetOutValue(0);//小于100的设置为0
	thresholdFilter->Update();//算法执行后必须添加更新消息

	vtkSmartPointer originalActor =
		vtkSmartPointer::New();
	originalActor->SetInputData(reader->GetOutput());

	vtkSmartPointer binaryActor =
		vtkSmartPointer::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 originalRenderer =
		vtkSmartPointer::New();
	originalRenderer->SetViewport(originalViewport);
	originalRenderer->AddActor(originalActor);
	originalRenderer->ResetCamera();
	originalRenderer->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer binaryRenderer =
		vtkSmartPointer::New();
	binaryRenderer->SetViewport(binaryviewport);
	binaryRenderer->AddActor(binaryActor);
	binaryRenderer->ResetCamera();
	binaryRenderer->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer renderWindow =
		vtkSmartPointer::New();
	renderWindow->AddRenderer(originalRenderer);
	renderWindow->AddRenderer(binaryRenderer);
	renderWindow->SetSize(640, 320);
	renderWindow->Render();
	renderWindow->SetWindowName("ImageBinaryExample");

	vtkSmartPointer renderWindowInteractor =
		vtkSmartPointer::New();
	vtkSmartPointer style =
		vtkSmartPointer::New();

	renderWindowInteractor->SetInteractorStyle(style);
	renderWindowInteractor->SetRenderWindow(renderWindow);
	renderWindowInteractor->Initialize();
	renderWindowInteractor->Start();

	return EXIT_SUCCESS;
}

 

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第30张图片基于C++的水灵VTK8学习笔记2(VTK图像处理)_第31张图片

实例37:梯度算子(边缘检测)

#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

//测试图像:../data/lena-gray.jpg
int main(int argc, char* argv[])
{
	vtkSmartPointer reader =
		vtkSmartPointer::New();
	reader->SetFileName("lena-gray.jpg");
	reader->Update();

	vtkSmartPointer gradientFilter =
		vtkSmartPointer::New();
	gradientFilter->SetInputConnection(reader->GetOutputPort());
	gradientFilter->SetDimensionality(2);

	vtkSmartPointer magnitudeFilter =
		vtkSmartPointer::New();
	magnitudeFilter->SetInputConnection(gradientFilter->GetOutputPort());
	magnitudeFilter->Update();

	double range[2];
	magnitudeFilter->GetOutput()->GetScalarRange(range);

	vtkSmartPointer ShiftScale =
		vtkSmartPointer::New();
	ShiftScale->SetOutputScalarTypeToUnsignedChar();
	ShiftScale->SetScale( 255 / range[1] );
	ShiftScale->SetInputConnection(magnitudeFilter->GetOutputPort());
	ShiftScale->Update();

	vtkSmartPointer originalActor =
		vtkSmartPointer::New();
	originalActor->SetInputData(reader->GetOutput());

	vtkSmartPointer gradActor =
		vtkSmartPointer::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 originalRenderer =
		vtkSmartPointer::New();
	originalRenderer->SetViewport(originalViewport);
	originalRenderer->AddActor(originalActor);
	originalRenderer->ResetCamera();
	originalRenderer->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer gradRenderer =
		vtkSmartPointer::New();
	gradRenderer->SetViewport(gradviewport);
	gradRenderer->AddActor(gradActor);
	gradRenderer->ResetCamera();
	gradRenderer->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer renderWindow =
		vtkSmartPointer::New();
	renderWindow->AddRenderer(originalRenderer);
	renderWindow->AddRenderer(gradRenderer);
	renderWindow->SetSize( 640, 320 );
	renderWindow->Render();
	renderWindow->SetWindowName("ImageGradientExample");

	vtkSmartPointer renderWindowInteractor =
		vtkSmartPointer::New();
	vtkSmartPointer style =
		vtkSmartPointer::New();

	renderWindowInteractor->SetInteractorStyle(style);
	renderWindowInteractor->SetRenderWindow(renderWindow);
	renderWindowInteractor->Initialize();
	renderWindowInteractor->Start();

	return EXIT_SUCCESS;
}

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第32张图片

实例38:Sobel梯度算子(边缘检测)

                                                  基于C++的水灵VTK8学习笔记2(VTK图像处理)_第33张图片

#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

//测试图像:../data/lena-gray.jpg
int main(int argc, char* argv[])
{
	vtkSmartPointer reader =
		vtkSmartPointer::New();
	reader->SetFileName("lena-gray.jpg");
	reader->Update();

	vtkSmartPointer sobelFilter =
		vtkSmartPointer::New();
	sobelFilter->SetInputConnection(reader->GetOutputPort());

	vtkSmartPointer extractXFilter = 
		vtkSmartPointer::New();
	extractXFilter->SetComponents(0);
	extractXFilter->SetInputConnection(sobelFilter->GetOutputPort());
	extractXFilter->Update();

	double xRange[2];
	extractXFilter->GetOutput()->GetScalarRange(xRange);

	vtkSmartPointer xImageAbs =
		vtkSmartPointer::New();
	xImageAbs->SetOperationToAbsoluteValue();
	xImageAbs->SetInputConnection(extractXFilter->GetOutputPort());
	xImageAbs->Update();

	vtkSmartPointer xShiftScale =
		vtkSmartPointer::New();
	xShiftScale->SetOutputScalarTypeToUnsignedChar();
	xShiftScale->SetScale( 255 / xRange[1] );
	xShiftScale->SetInputConnection(xImageAbs->GetOutputPort());
	xShiftScale->Update();

	vtkSmartPointer extractYFilter = 
		vtkSmartPointer::New();
	extractYFilter->SetComponents(1);
	extractYFilter->SetInputConnection(sobelFilter->GetOutputPort());
	extractYFilter->Update();

	double yRange[2];
	extractYFilter->GetOutput()->GetScalarRange(yRange);

	vtkSmartPointer yImageAbs =
		vtkSmartPointer::New();
	yImageAbs->SetOperationToAbsoluteValue();
	yImageAbs->SetInputConnection(extractYFilter->GetOutputPort());
	yImageAbs->Update();

	vtkSmartPointer yShiftScale =
		vtkSmartPointer::New();
	yShiftScale->SetOutputScalarTypeToUnsignedChar();
	yShiftScale->SetScale( 255 / yRange[1] );
	yShiftScale->SetInputConnection(yImageAbs->GetOutputPort());
	yShiftScale->Update();

	vtkSmartPointer originalActor =
		vtkSmartPointer::New();
	originalActor->SetInputData(reader->GetOutput());

	vtkSmartPointer xActor =
		vtkSmartPointer::New();
	xActor->SetInputData(xShiftScale->GetOutput());

	vtkSmartPointer yActor =
		vtkSmartPointer::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 originalRenderer =
		vtkSmartPointer::New();
	originalRenderer->SetViewport(originalViewport);
	originalRenderer->AddActor(originalActor);
	originalRenderer->ResetCamera();
	originalRenderer->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer xRenderer =
		vtkSmartPointer::New();
	xRenderer->SetViewport(xViewport);
	xRenderer->AddActor(xActor);
	xRenderer->ResetCamera();
	xRenderer->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer yRenderer =
		vtkSmartPointer::New();
	yRenderer->SetViewport(yViewport);
	yRenderer->AddActor(yActor);
	yRenderer->ResetCamera();
	yRenderer->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer renderWindow =
		vtkSmartPointer::New();
	renderWindow->SetSize(1200, 300);
	renderWindow->AddRenderer(originalRenderer);
	renderWindow->AddRenderer(xRenderer);
	renderWindow->AddRenderer(yRenderer);
	renderWindow->Render();
	renderWindow->SetWindowName("SobelExample");

	vtkSmartPointer renderWindowInteractor =
		vtkSmartPointer::New();
	vtkSmartPointer style =
		vtkSmartPointer::New();

	renderWindowInteractor->SetInteractorStyle(style);
	renderWindowInteractor->SetRenderWindow(renderWindow);
	renderWindowInteractor->Initialize();
	renderWindowInteractor->Start();

	return EXIT_SUCCESS;
}

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第34张图片

实例39:拉普拉斯算子(边缘检测)

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第35张图片

#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

//测试图像:../data/lena-gray.jpg
int main(int argc, char* argv[])
{
	vtkSmartPointer reader =
		vtkSmartPointer::New();
	reader->SetFileName("lena-gray.jpg");
	reader->Update();

	vtkSmartPointer lapFilter =
		vtkSmartPointer::New();
	lapFilter->SetInputConnection(reader->GetOutputPort());
	lapFilter->SetDimensionality(2);

	double range[2];
	lapFilter->GetOutput()->GetScalarRange(range);

	vtkSmartPointer ShiftScale =
		vtkSmartPointer::New();
	ShiftScale->SetOutputScalarTypeToUnsignedChar();
	ShiftScale->SetScale( 255 / (range[1]-range[0]) );
	ShiftScale->SetShift(-range[0]);
	ShiftScale->SetInputConnection(lapFilter->GetOutputPort());
	ShiftScale->Update();

	vtkSmartPointer originalActor =
		vtkSmartPointer::New();
	originalActor->SetInputData(reader->GetOutput());

	vtkSmartPointer gradActor =
		vtkSmartPointer::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 originalRenderer =
		vtkSmartPointer::New();
	originalRenderer->SetViewport(originalViewport);
	originalRenderer->AddActor(originalActor);
	originalRenderer->ResetCamera();
	originalRenderer->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer gradRenderer =
		vtkSmartPointer::New();
	gradRenderer->SetViewport(gradviewport);
	gradRenderer->AddActor(gradActor);
	gradRenderer->ResetCamera();
	gradRenderer->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer renderWindow =
		vtkSmartPointer::New();
	renderWindow->AddRenderer(originalRenderer);
	renderWindow->AddRenderer(gradRenderer);
	renderWindow->SetSize(640, 320);
	renderWindow->Render();
	renderWindow->SetWindowName("LaplacianExample");

	vtkSmartPointer renderWindowInteractor =
		vtkSmartPointer::New();
	vtkSmartPointer style =
		vtkSmartPointer::New();

	renderWindowInteractor->SetInteractorStyle(style);
	renderWindowInteractor->SetRenderWindow(renderWindow);
	renderWindowInteractor->Initialize();
	renderWindowInteractor->Start();

	return EXIT_SUCCESS;
}

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第36张图片

实例40:均值滤波(图像光滑)

#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

//测试图像:../data/lena-gray.jpg
int main(int argc, char* argv[])
{
	vtkSmartPointer reader =
		vtkSmartPointer::New();
	reader->SetFileName("lena-gray.jpg");
	reader->Update();

	vtkSmartPointer originalCastFilter =
		vtkSmartPointer::New();
	originalCastFilter->SetInputConnection(reader->GetOutputPort());
	originalCastFilter->SetOutputScalarTypeToFloat();
	originalCastFilter->Update();

	vtkSmartPointer convolveFilter =
		vtkSmartPointer::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 convCastFilter = 
		vtkSmartPointer::New();
	convCastFilter->SetInputData(convolveFilter->GetOutput());
	convCastFilter->SetOutputScalarTypeToUnsignedChar();
	convCastFilter->Update();

	vtkSmartPointer originalActor =
		vtkSmartPointer::New();
	originalActor->SetInputData(reader->GetOutput());

	vtkSmartPointer convolvedActor =
		vtkSmartPointer::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 originalRenderer =
		vtkSmartPointer::New();
	originalRenderer->SetViewport(leftViewport);
	originalRenderer->AddActor(originalActor);
	originalRenderer->SetBackground(1.0, 1.0, 1.0);
	originalRenderer->ResetCamera();

	vtkSmartPointer convolvedRenderer =
		vtkSmartPointer::New();
	convolvedRenderer->SetViewport(rightViewport);
	convolvedRenderer->AddActor(convolvedActor);
	convolvedRenderer->SetBackground(1.0, 1.0, 1.0);
	convolvedRenderer->ResetCamera();

	vtkSmartPointer renderWindow =
		vtkSmartPointer::New();;
	renderWindow->AddRenderer(originalRenderer);
	renderWindow->AddRenderer(convolvedRenderer);
	renderWindow->SetSize(640, 320);
	renderWindow->Render();
	renderWindow->SetWindowName("MeanFilterExample");

	vtkSmartPointer renderWindowInteractor =
		vtkSmartPointer::New();
	vtkSmartPointer style =
		vtkSmartPointer::New();

	renderWindowInteractor->SetInteractorStyle(style);
	renderWindowInteractor->SetRenderWindow(renderWindow);
	renderWindowInteractor->Initialize();
	renderWindowInteractor->Start();

	return EXIT_SUCCESS;
}

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第37张图片

实例41:高斯平滑(图像光滑)

#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

//测试图像:../data/lena-gray.jpg
int main(int argc, char* argv[])
{
	vtkSmartPointer reader =
		vtkSmartPointer::New();
	reader->SetFileName("lena-gray.jpg");
	reader->Update();

	vtkSmartPointer gaussianSmoothFilter = 
		vtkSmartPointer::New();
	gaussianSmoothFilter->SetInputConnection(reader->GetOutputPort());
	gaussianSmoothFilter->SetDimensionality(2);
	gaussianSmoothFilter->SetRadiusFactor(5);
	gaussianSmoothFilter->SetStandardDeviation(3);
	gaussianSmoothFilter->Update();

	vtkSmartPointer originalActor =
		vtkSmartPointer::New();
	originalActor->SetInputData(reader->GetOutput());

	vtkSmartPointer smoothedActor =
		vtkSmartPointer::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 originalRenderer =
		vtkSmartPointer::New();
	originalRenderer->SetViewport(originalViewport);
	originalRenderer->AddActor(originalActor);
	originalRenderer->ResetCamera();
	originalRenderer->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer gradientMagnitudeRenderer =
		vtkSmartPointer::New();
	gradientMagnitudeRenderer->SetViewport(smoothedViewport);
	gradientMagnitudeRenderer->AddActor(smoothedActor);
	gradientMagnitudeRenderer->ResetCamera();
	gradientMagnitudeRenderer->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer renderWindow =
		vtkSmartPointer::New();
	renderWindow->AddRenderer(originalRenderer);
	renderWindow->AddRenderer(gradientMagnitudeRenderer);
	renderWindow->SetSize(640, 320);
	renderWindow->Render();
	renderWindow->SetWindowName("GaussianFilterExample");

	vtkSmartPointer renderWindowInteractor =
		vtkSmartPointer::New();
	vtkSmartPointer style =
		vtkSmartPointer::New();

	renderWindowInteractor->SetInteractorStyle(style);
	renderWindowInteractor->SetRenderWindow(renderWindow);
	renderWindowInteractor->Initialize();
	renderWindowInteractor->Start();

	return EXIT_SUCCESS;
}

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第38张图片

实例42:中值滤波(图像光滑)

#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

//测试图像:../data/lena-gray.jpg
int main(int argc, char* argv[])
{
	vtkSmartPointer reader =
		vtkSmartPointer::New();
	reader->SetFileName("lena-gray.jpg");
	reader->Update();

	vtkSmartPointer originalCastFilter =
		vtkSmartPointer::New();
	originalCastFilter->SetInputConnection(reader->GetOutputPort());
	originalCastFilter->SetOutputScalarTypeToFloat();
	originalCastFilter->Update();

	vtkSmartPointer convolveFilter =
		vtkSmartPointer::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 convCastFilter = 
		vtkSmartPointer::New();
	convCastFilter->SetInputData(convolveFilter->GetOutput());
	convCastFilter->SetOutputScalarTypeToUnsignedChar();
	convCastFilter->Update();

	vtkSmartPointer originalActor =
		vtkSmartPointer::New();
	originalActor->SetInputData(reader->GetOutput());

	vtkSmartPointer convolvedActor =
		vtkSmartPointer::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 originalRenderer =
		vtkSmartPointer::New();
	originalRenderer->SetViewport(leftViewport);
	originalRenderer->AddActor(originalActor);
	originalRenderer->SetBackground(1.0, 1.0, 1.0);
	originalRenderer->ResetCamera();

	vtkSmartPointer convolvedRenderer =
		vtkSmartPointer::New();
	convolvedRenderer->SetViewport(rightViewport);
	convolvedRenderer->AddActor(convolvedActor);
	convolvedRenderer->SetBackground(1.0, 1.0, 1.0);
	convolvedRenderer->ResetCamera();

	vtkSmartPointer renderWindow =
		vtkSmartPointer::New();;
	renderWindow->AddRenderer(originalRenderer);
	renderWindow->AddRenderer(convolvedRenderer);
	renderWindow->SetSize(640, 320);
	renderWindow->Render();
	renderWindow->SetWindowName("MeanFilterExample");

	vtkSmartPointer renderWindowInteractor =
		vtkSmartPointer::New();
	vtkSmartPointer style =
		vtkSmartPointer::New();

	renderWindowInteractor->SetInteractorStyle(style);
	renderWindowInteractor->SetRenderWindow(renderWindow);
	renderWindowInteractor->Initialize();
	renderWindowInteractor->Start();

	return EXIT_SUCCESS;
}

 

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第39张图片

实例43:各向异性滤波(图像光滑)

#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

//测试图像:../data/lena-gray.jpg
int main(int argc, char* argv[])
{
	vtkSmartPointer reader =
		vtkSmartPointer::New();
	reader->SetFileName("lena-gray.jpg");

	vtkSmartPointer diffusion =
		vtkSmartPointer::New();
	diffusion->SetInputConnection(reader->GetOutputPort());
	diffusion->SetNumberOfIterations(10);
	diffusion->SetDiffusionThreshold(20);
	diffusion->Update();

	vtkSmartPointer originalActor =
		vtkSmartPointer::New();
	originalActor->SetInputData(reader->GetOutput());

	vtkSmartPointer diffusionActor =
		vtkSmartPointer::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 camera =
		vtkSmartPointer::New();
	vtkSmartPointer leftRenderer =
		vtkSmartPointer::New();
	leftRenderer->SetViewport(leftViewport);
	leftRenderer->AddActor(originalActor);
	leftRenderer->SetBackground(1.0, 1.0, 1.0);
	leftRenderer->SetActiveCamera(camera);
	leftRenderer->ResetCamera();

	vtkSmartPointer rightRenderer =
		vtkSmartPointer::New();
	rightRenderer->SetViewport(rightViewport);
	rightRenderer->SetBackground(1.0, 1.0, 1.0);
	rightRenderer->AddActor(diffusionActor);
	rightRenderer->SetActiveCamera(camera);

	vtkSmartPointer renderWindow =
		vtkSmartPointer::New();
	renderWindow->AddRenderer(leftRenderer);
	renderWindow->AddRenderer(rightRenderer);
	renderWindow->SetSize(640, 320);
	renderWindow->Render();
	renderWindow->SetWindowName("AnistropicFilteringExample");

	vtkSmartPointer renderWindowInteractor =
		vtkSmartPointer::New();
	vtkSmartPointer style =
		vtkSmartPointer::New();

	renderWindowInteractor->SetInteractorStyle(style);
	renderWindowInteractor->SetRenderWindow(renderWindow);
	renderWindowInteractor->Initialize();
	renderWindowInteractor->Start();

	return EXIT_SUCCESS;
}

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第40张图片

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第41张图片

实例44:二维图像快速傅里叶变换(频域处理)

#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

//测试图像:../data/lena-gray.jpg
int main(int argc, char* argv[])
{
	vtkSmartPointer reader =
		vtkSmartPointer::New();
	reader->SetFileName ("lena-gray.jpg");
	reader->Update();

	vtkSmartPointer fftFilter = 
		vtkSmartPointer::New();
	fftFilter->SetInputConnection(reader->GetOutputPort());
	fftFilter->SetDimensionality(2);
	fftFilter->Update();

	vtkSmartPointer fftExtractReal =
		vtkSmartPointer::New();
	fftExtractReal->SetInputConnection(fftFilter->GetOutputPort());
	fftExtractReal->SetComponents(0);

	double range[2];
	fftExtractReal->GetOutput()->GetScalarRange(range);

	vtkSmartPointer ShiftScale =
		vtkSmartPointer::New();
	ShiftScale->SetOutputScalarTypeToUnsignedChar();
	ShiftScale->SetScale( 255.0 / (range[1]-range[0]) );
	ShiftScale->SetShift(-range[0]);
	ShiftScale->SetInputConnection(fftExtractReal->GetOutputPort());
	ShiftScale->Update();

	vtkSmartPointer rfftFilter =
		vtkSmartPointer::New();
	rfftFilter->SetInputConnection(fftFilter->GetOutputPort());
	rfftFilter->SetDimensionality(2);
	rfftFilter->Update();

	vtkSmartPointer ifftExtractReal =
		vtkSmartPointer::New();
	ifftExtractReal->SetInputConnection(rfftFilter->GetOutputPort());
	ifftExtractReal->SetComponents(0);

	vtkSmartPointer rfftCastFilter =
		vtkSmartPointer::New();
	rfftCastFilter->SetInputConnection(ifftExtractReal->GetOutputPort());
	rfftCastFilter->SetOutputScalarTypeToUnsignedChar();
	rfftCastFilter->Update();

	vtkSmartPointer originalActor =
		vtkSmartPointer::New();
	originalActor->SetInputData(reader->GetOutput());

	vtkSmartPointer fftActor =
		vtkSmartPointer::New();
	fftActor->SetInputData(ShiftScale->GetOutput());

	vtkSmartPointer rfftActor =
		vtkSmartPointer::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 originalRenderer =
		vtkSmartPointer::New();
	originalRenderer->SetViewport(originalViewport);
	originalRenderer->AddActor(originalActor);
	originalRenderer->ResetCamera();
	originalRenderer->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer fftRenderer =
		vtkSmartPointer::New();
	fftRenderer->SetViewport(fftViewport);
	fftRenderer->AddActor(fftActor);
	fftRenderer->ResetCamera();
	fftRenderer->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer rfftRenderer =
		vtkSmartPointer::New();
	rfftRenderer->SetViewport(rfftViewport);
	rfftRenderer->AddActor(rfftActor);
	rfftRenderer->ResetCamera();
	rfftRenderer->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer renderWindow =
		vtkSmartPointer::New();
	renderWindow->AddRenderer(originalRenderer);
	renderWindow->AddRenderer(fftRenderer);
	renderWindow->AddRenderer(rfftRenderer);
	renderWindow->SetSize(640, 320);
	renderWindow->Render();
	renderWindow->SetWindowName("FFTAndRFFTExample");

	vtkSmartPointer renderWindowInteractor =
		vtkSmartPointer::New();
	vtkSmartPointer style =
		vtkSmartPointer::New();

	renderWindowInteractor->SetInteractorStyle(style);
	renderWindowInteractor->SetRenderWindow(renderWindow);
	renderWindowInteractor->Initialize();
	renderWindowInteractor->Start();

	return EXIT_SUCCESS;
}

 

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第42张图片

实例45:理想低通滤波器(频域处理)

#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

//测试图像:../data/lena-gray.jpg
int main(int argc, char* argv[])
{
	vtkSmartPointer reader = 
		vtkSmartPointer::New();
	reader->SetFileName("lena-gray.jpg");
	reader->Update();

	vtkSmartPointer fftFilter =
		vtkSmartPointer::New();
	fftFilter->SetInputConnection(reader->GetOutputPort());
	fftFilter->Update();

	vtkSmartPointer lowPassFilter = 
		vtkSmartPointer::New();
	lowPassFilter->SetInputConnection(fftFilter->GetOutputPort());
	lowPassFilter->SetXCutOff(0.05);
	lowPassFilter->SetYCutOff(0.05);
	lowPassFilter->Update();

	vtkSmartPointer rfftFilter =
		vtkSmartPointer::New();
	rfftFilter->SetInputConnection(lowPassFilter->GetOutputPort());
	rfftFilter->Update();

	vtkSmartPointer ifftExtractReal =
		vtkSmartPointer::New();
	ifftExtractReal->SetInputConnection(rfftFilter->GetOutputPort());
	ifftExtractReal->SetComponents(0);

	vtkSmartPointer castFilter =
		vtkSmartPointer::New();
	castFilter->SetInputConnection(ifftExtractReal->GetOutputPort());
	castFilter->SetOutputScalarTypeToUnsignedChar();
	castFilter->Update();

	vtkSmartPointer originalActor =
		vtkSmartPointer::New();
	originalActor->SetInputData(reader->GetOutput());

	vtkSmartPointer erodedActor =
		vtkSmartPointer::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 renderWindow =
		vtkSmartPointer::New();
	renderWindow->SetSize(640, 320);
	renderWindow->Render();
	renderWindow->SetWindowName("IdealLowPassExample");

	vtkSmartPointer interactor =
		vtkSmartPointer::New();
	interactor->SetRenderWindow(renderWindow);

	vtkSmartPointer leftRenderer =
		vtkSmartPointer::New();
	renderWindow->AddRenderer(leftRenderer);
	leftRenderer->SetViewport(leftViewport);
	leftRenderer->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer rightRenderer =
		vtkSmartPointer::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;
}

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第43张图片

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第44张图片

实例46:巴特沃斯低通滤波器(频域处理)

#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

//测试图像:../data/lena-gray.jpg
int main(int argc, char* argv[])
{
	vtkSmartPointer reader = 
		vtkSmartPointer::New();
	reader->SetFileName("lena-gray.jpg");
	reader->Update();

	vtkSmartPointer fftFilter =
		vtkSmartPointer::New();
	fftFilter->SetInputConnection(reader->GetOutputPort());
	fftFilter->Update();

	vtkSmartPointer lowPassFilter = 
		vtkSmartPointer::New();
	lowPassFilter->SetInputConnection(fftFilter->GetOutputPort());
	lowPassFilter->SetXCutOff(0.05);
	lowPassFilter->SetYCutOff(0.05);
	lowPassFilter->Update();

	vtkSmartPointer rfftFilter =
		vtkSmartPointer::New();
	rfftFilter->SetInputConnection(lowPassFilter->GetOutputPort());
	rfftFilter->Update();

	vtkSmartPointer ifftExtractReal =
		vtkSmartPointer::New();
	ifftExtractReal->SetInputConnection(rfftFilter->GetOutputPort());
	ifftExtractReal->SetComponents(0);

	vtkSmartPointer castFilter =
		vtkSmartPointer::New();
	castFilter->SetInputConnection(ifftExtractReal->GetOutputPort());
	castFilter->SetOutputScalarTypeToUnsignedChar();
	castFilter->Update();

	vtkSmartPointer originalActor =
		vtkSmartPointer::New();
	originalActor->SetInputData(reader->GetOutput());

	vtkSmartPointer erodedActor =
		vtkSmartPointer::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 renderWindow =
		vtkSmartPointer::New();
	renderWindow->SetSize(640, 320);
	renderWindow->Render();
	renderWindow->SetWindowName("ButterworthLowPassExample");

	vtkSmartPointer interactor =
		vtkSmartPointer::New();
	interactor->SetRenderWindow(renderWindow);

	vtkSmartPointer leftRenderer =
		vtkSmartPointer::New();
	renderWindow->AddRenderer(leftRenderer);
	leftRenderer->SetViewport(leftViewport);
	leftRenderer->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer rightRenderer =
		vtkSmartPointer::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;
}

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第45张图片

实例47:理想高通滤波器(频域处理)

#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

//测试图像:../data/lena-gray.jpg
int main(int argc, char* argv[])
{
	vtkSmartPointer reader = 
		vtkSmartPointer::New();
	reader->SetFileName("lena-gray.jpg");
	reader->Update();

	vtkSmartPointer fftFilter =
		vtkSmartPointer::New();
	fftFilter->SetInputConnection(reader->GetOutputPort());
	fftFilter->Update();

	vtkSmartPointer highPassFilter = 
		vtkSmartPointer::New();
	highPassFilter->SetInputConnection(fftFilter->GetOutputPort());
	highPassFilter->SetXCutOff(0.1);
	highPassFilter->SetYCutOff(0.1);
	highPassFilter->Update();

	vtkSmartPointer rfftFilter =
		vtkSmartPointer::New();
	rfftFilter->SetInputConnection(highPassFilter->GetOutputPort());
	rfftFilter->Update();

	vtkSmartPointer ifftExtractReal =
		vtkSmartPointer::New();
	ifftExtractReal->SetInputConnection(rfftFilter->GetOutputPort());
	ifftExtractReal->SetComponents(0);

	vtkSmartPointer castFilter =
		vtkSmartPointer::New();
	castFilter->SetInputConnection(ifftExtractReal->GetOutputPort());
	castFilter->SetOutputScalarTypeToUnsignedChar();
	castFilter->Update();

	vtkSmartPointer originalActor =
		vtkSmartPointer::New();
	originalActor->SetInputData(reader->GetOutput());

	vtkSmartPointer erodedActor =
		vtkSmartPointer::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 renderWindow =
		vtkSmartPointer::New();
	renderWindow->SetSize(640, 320);
	renderWindow->Render();
	renderWindow->SetWindowName("IdealHighPassExample");

	vtkSmartPointer interactor =
		vtkSmartPointer::New();
	interactor->SetRenderWindow(renderWindow);

	vtkSmartPointer leftRenderer =
		vtkSmartPointer::New();
	renderWindow->AddRenderer(leftRenderer);
	leftRenderer->SetViewport(leftViewport);
	leftRenderer->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer rightRenderer =
		vtkSmartPointer::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;
}

 

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第46张图片

实例48:巴沃斯特高通滤波器(频域处理)

基于C++的水灵VTK8学习笔记2(VTK图像处理)_第47张图片

#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

//测试图像:../data/lena-gray.jpg
int main(int argc, char* argv[])
{
	vtkSmartPointer reader = 
		vtkSmartPointer::New();
	reader->SetFileName("lena-gray.jpg");
	reader->Update();

	vtkSmartPointer fftFilter =
		vtkSmartPointer::New();
	fftFilter->SetInputConnection(reader->GetOutputPort());
	fftFilter->Update();

	vtkSmartPointer highPassFilter = 
		vtkSmartPointer::New();
	highPassFilter->SetInputConnection(fftFilter->GetOutputPort());
	highPassFilter->SetXCutOff(0.1);
	highPassFilter->SetYCutOff(0.1);
	highPassFilter->Update();

	vtkSmartPointer rfftFilter =
		vtkSmartPointer::New();
	rfftFilter->SetInputConnection(highPassFilter->GetOutputPort());
	rfftFilter->Update();

	vtkSmartPointer ifftExtractReal =
		vtkSmartPointer::New();
	ifftExtractReal->SetInputConnection(rfftFilter->GetOutputPort());
	ifftExtractReal->SetComponents(0);

	vtkSmartPointer castFilter =
		vtkSmartPointer::New();
	castFilter->SetInputConnection(ifftExtractReal->GetOutputPort());
	castFilter->SetOutputScalarTypeToUnsignedChar();
	castFilter->Update();

	vtkSmartPointer originalActor =
		vtkSmartPointer::New();
	originalActor->SetInputData(reader->GetOutput());

	vtkSmartPointer erodedActor =
		vtkSmartPointer::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 renderWindow =
		vtkSmartPointer::New();
	renderWindow->SetSize(640, 320);
	renderWindow->Render();
	renderWindow->SetWindowName("ButterworthHighPassExample");

	vtkSmartPointer interactor =
		vtkSmartPointer::New();
	interactor->SetRenderWindow(renderWindow);

	vtkSmartPointer leftRenderer =
		vtkSmartPointer::New();
	renderWindow->AddRenderer(leftRenderer);
	leftRenderer->SetViewport(leftViewport);
	leftRenderer->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer rightRenderer =
		vtkSmartPointer::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;
}

 基于C++的水灵VTK8学习笔记2(VTK图像处理)_第48张图片

你可能感兴趣的:(VTK学习笔记)