VTK010_vtk-图像基础处理_上

主宰稳场 提交于 2020-03-05 22:14:02

1、图像融合:

图像融合,将两张图片进行融合在一起,体现为每张图片都有不同的透明度。


#include <vtkSmartPointer.h>
#include <vtkJPEGReader.h>
//#include <vtkImageCast.h>
#include <vtkImageData.h>
#include <vtkImageCanvasSource2D.h>
#include <vtkImageBlend.h>
#include <vtkImageActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>

int main(int argc, char* argv[])
{
	// 图片数据
	vtkSmartPointer<vtkJPEGReader> reader = vtkSmartPointer<vtkJPEGReader>::New();
	reader->SetFileName("flower.jpg");
	reader->Update();
	vtkSmartPointer<vtkJPEGReader> source = vtkSmartPointer<vtkJPEGReader>::New();
	source->SetFileName("light.jpg");
	source->Update();

	vtkSmartPointer<vtkImageBlend> blend = vtkSmartPointer<vtkImageBlend>::New();	// 图像混合器
	blend->AddInputConnection(source->GetOutputPort());
	blend->AddInputConnection(reader->GetOutputPort());
	blend->SetOpacity(0, 0.2);	// 设置每个图像的透明度
	blend->SetOpacity(1, 0.8);
	blend->Update();

	//渲染引擎
	vtkSmartPointer<vtkImageActor> actor1 = vtkSmartPointer<vtkImageActor>::New();
	actor1->SetInputData(reader->GetOutput());	// 原始图像
	vtkSmartPointer<vtkImageActor> actor2 = vtkSmartPointer<vtkImageActor>::New();
	actor2->SetInputData(source->GetOutput());	// 绘制图像
	vtkSmartPointer<vtkImageActor> blend_actor = vtkSmartPointer<vtkImageActor>::New();
	blend_actor->SetInputData(blend->GetOutput());	// 混合图像
	//定义视窗大小(xmin.ymin,xmax,ymax)
	//按window的尺寸进行比例分割
	double leftViewport[4] = { 0, 0, 0.33, 1 };
	double midViewport[4] = { 0.33, 0, 0.66, 1 };
	double rightViewport[4] = { 0.66, 0, 1, 1 };
	//render
	vtkSmartPointer<vtkRenderer> render1 = vtkSmartPointer<vtkRenderer>::New();
	render1->SetViewport(leftViewport);
	render1->AddActor(actor1);
	render1->ResetCamera();
	render1->SetBackground(1, 0, 0);
	vtkSmartPointer<vtkRenderer> render2 = vtkSmartPointer<vtkRenderer>::New();
	render2->SetViewport(midViewport);
	render2->AddActor(actor2);
	render2->ResetCamera();
	render2->SetBackground(0, 1, 0);
	vtkSmartPointer<vtkRenderer> render3 = vtkSmartPointer<vtkRenderer>::New();
	render3->SetViewport(rightViewport);
	render3->AddActor(blend_actor);
	render3->ResetCamera();
	render3->SetBackground(0, 0, 1);
	//window
	vtkSmartPointer<vtkRenderWindow> renderwindow =
		vtkSmartPointer<vtkRenderWindow>::New();
	renderwindow->AddRenderer(render1);
	renderwindow->AddRenderer(render2);
	renderwindow->AddRenderer(render3);
	renderwindow->SetSize(1024, 296);
	renderwindow->SetWindowName("Image-Fusion");
	renderwindow->Render();
	//interactor
	vtkSmartPointer<vtkRenderWindowInteractor> rwi = vtkSmartPointer<vtkRenderWindowInteractor>::New();
	vtkSmartPointer<vtkInteractorStyleImage> style =
		vtkSmartPointer<vtkInteractorStyleImage>::New();
	rwi->SetInteractorStyle(style);
	rwi->SetRenderWindow(renderwindow);
	rwi->Initialize();
	rwi->Start();

	return 0;
}

代码中,读取了两幅jpg图像进行融合,同样,课设置多个图像输入,最终得到的图像与第一个输入图像大小一致,此外,图像融合还提供其他模式及功能。

2、vtk图像信息获取及修改:

图像信息,及获取图像的信息,包括图像大小(一般为长宽,厚一般均为1),图像中心(图像中心位置),以及图像间距(为每两个像素点之间的间距mm,一般图像中用的极少)。

#include <vtkSmartPointer.h>
#include <vtkImageViewer2.h>
#include <vtkJPEGReader.h>
#include <vtkImageData.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkImageChangeInformation.h>

int main(int argc, char*argv[])
{
	//读数据
	vtkSmartPointer<vtkJPEGReader> reader = vtkSmartPointer<vtkJPEGReader>::New();
	reader->SetFileName("light.jpg");
	reader->Update();
	//获取图像信息
	int dims[3] = { 0 };
	double origin[3] = { 0 };
	double spacing[3] = { 0 };
	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(spacing);
	std::cout << "像素间距:" << spacing[0] << " " << spacing[1] << " " << spacing[2] << std::endl;

	vtkSmartPointer<vtkImageChangeInformation> imageChange = vtkSmartPointer<vtkImageChangeInformation>::New();
	imageChange->SetInputConnection(reader->GetOutputPort());
	imageChange->SetOutputSpacing(10, 10, 10);
	//imageChange->SetOutputOrigin(1024, 750, 5);  // 手动设置中心点
	imageChange->SetCenterImage(true);	// 图像居中(图像中点作为中心点而非左下角)
	imageChange->Update();

	imageChange->GetOutput()->GetDimensions(dims);
	std::cout << "图像大小:" << dims[0] << "*" << dims[1] << "*" << dims[2] << std::endl;
	imageChange->GetOutput()->GetOrigin(origin);
	std::cout << "图像中心:" << origin[0] << " " << origin[1] << " " << origin[2] << std::endl;
	imageChange->GetOutput()->GetSpacing(spacing);
	std::cout << "像素间距:" << spacing[0] << " " << spacing[1] << " " << spacing[2] << std::endl;

	//显示图像
	vtkSmartPointer<vtkImageViewer2> imgViewer = vtkSmartPointer<vtkImageViewer2>::New();
	imgViewer->SetInputConnection(reader->GetOutputPort());
	vtkSmartPointer<vtkRenderWindowInteractor> rwi =
		vtkSmartPointer<vtkRenderWindowInteractor>::New();
	imgViewer->SetupInteractor(rwi);

	imgViewer->GetRenderer()->SetBackground(1.0, 1.0, 1.0);
	imgViewer->GetRenderer()->ResetCamera();
	imgViewer->SetSize(640, 480);
	imgViewer->Render();
	imgViewer->GetRenderWindow()->SetWindowName("GetImageInfo");

	rwi->Start();

	return 0;
}

代码中,读取了一幅图像并分别获取了图像的大小,中心点位置,以及像素间距信息,然后将图像更改后,再次读取。

对于更改后图像中心,实际上长度是1024像素,间距为10,所以图像总长度是10230,中心就是-5115,实际上,这个数据是将图像移动到中心的偏移,图像在xy平面上偏移(-5115,-3745)后,就能使图像位于中心。

3、图像像素访问及修改:

通过这个例子,可以实现图幅图像的每个像素点值的访问及修改,可以做最基本的图像处理。

#include <vtkSmartPointer.h>
#include <vtkImageData.h>
#include <vtkJPEGReader.h>
#include <vtkImageCast.h>
#include <vtkImageViewer2.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkImageIterator.h>

int main()
{
	vtkSmartPointer<vtkJPEGReader> reader = vtkSmartPointer<vtkJPEGReader>::New();
	reader->SetFileName("flower.jpg");
	reader->Update();

	vtkSmartPointer<vtkImageCast> imageTrans = vtkSmartPointer<vtkImageCast>::New();
	imageTrans->SetInputConnection(reader->GetOutputPort());
	imageTrans->SetOutputScalarTypeToUnsignedChar();	// unsigned char
	imageTrans->Update();

	int dims[3];	// 1024, 887, 1
	imageTrans->GetOutput()->GetDimensions(dims);	// 获取像素范围

	int nbofComp;
	nbofComp = imageTrans->GetOutput()->GetNumberOfScalarComponents();	// 标量分量(3代表红绿蓝)

// 	for (int k = 0; k < dims[2]; k++)	// 对每一层
// 	{
// 		for (int j = 0; j < dims[1]; j++)	// 对每层的每一行
// 		{
// 			for (int i = 0; i < dims[0]; i++)	// 对每一行的每个点
// 			{
// 				if (i < 724 && i > 300 && j > 200 && j < 687)	// 左右300内,上下200内
// 				{	// 获取到这个点的像素位置(第一个分量)
// 					unsigned char *pixel = (unsigned char *)(imageTrans->GetOutput()->GetScalarPointer(i, j, k));
// 					*pixel = 255 - *pixel;				// 第一个分量反向
// 					*(pixel + 1) = 255 - *(pixel + 1);	// 第二个分量反向
// 					*(pixel + 2) = 255 - *(pixel + 2);	// 第三个分量反向
// 				}	// rgb rgb rgb 存储的
// 			}
// 		}
// 	}
	int subrect[6] = { 300, 724, 200, 687, 0, 0 };
	vtkImageIterator<unsigned char> imageIter(imageTrans->GetOutput(), subrect);	// vtkImageData
	while (!imageIter.IsAtEnd())	// 未遍历到最后
	{
		unsigned char* beginPx = imageIter.BeginSpan();
		unsigned char* endPx = imageIter.EndSpan();	// 实际上是下一个像素的R(红色)分量,前闭后开
		while (beginPx != endPx)
		{
			*beginPx = 255 - *beginPx;
			++beginPx;
		}
		imageIter.NextSpan();
	}


	vtkSmartPointer<vtkImageViewer2> imgViewer = vtkSmartPointer<vtkImageViewer2>::New();
	imgViewer->SetInputData(imageTrans->GetOutput());

	vtkSmartPointer<vtkRenderWindowInteractor> rwi =
		vtkSmartPointer<vtkRenderWindowInteractor>::New();
	imgViewer->SetupInteractor(rwi);
	imgViewer->Render();
	imgViewer->GetRenderer()->ResetCamera();
	imgViewer->Render();
	imgViewer->GetRenderer()->SetBackground(1.0, 1.0, 1.0);
	imgViewer->SetSize(640, 480);
	imgViewer->GetRenderWindow()->SetWindowName("VisitImagePixelDirectly");

	rwi->Start();

	return 0;
}

上面例子,将一幅图像的中间部分颜色翻转了,除了通过最简单的范围判断修改数据外(代码中屏蔽部分),还可以使用vtk提供的图像访问迭代器,操作如上面代码所示。vtk的图像访问迭代器实际上是对图像某一块的像素迭代,分别访问,对一个像素的每个分量进行处理,这里不存在图像数据的拷贝

4、图像数据类型的转换:

图像数据类型转换,实际上是对图像内部像素点记录的形式的一种转变,图片,常见的都是用unsigned char来记录的,0~255代表每种分量的强度,如果转化成float来记录,则需要将每个数据都/255即可,例子如下:

#include <vtkSmartPointer.h>
#include <vtkImageCast.h>
#include <vtkImageShiftScale.h>
#include <vtkBMPReader.h>
//#include <vtkImageViewer2.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>

#include <vtkActor.h>
#include <vtkDICOMImageReader.h>
#include <vtkPolyDataMapper.h>
#include <vtkImageActor.h>
#include <vtkImageReslice.h>
#include <vtkMatrix4x4.h>
#include <vtkImageData.h>
#include <vtkLookupTable.h>
#include <vtkImageMapToColors.h>
#include <vtkImageMapper3D.h>
#include <vtkInteractorStyleImage.h>

class vtkImageInteractionCallback : public vtkCommand
{
public:

	static vtkImageInteractionCallback *New() {
		return new vtkImageInteractionCallback;
	};

	vtkImageInteractionCallback() {
		this->Slicing = 0;
		this->ImageReslice = 0;
		this->Interactor = 0;
	};

	void SetImageReslice(vtkImageReslice *reslice) {
		this->ImageReslice = reslice;
	};

	vtkImageReslice *GetImageReslice() {
		return this->ImageReslice;
	};

	void SetInteractor(vtkRenderWindowInteractor *interactor) {
		this->Interactor = interactor;
	};

	vtkRenderWindowInteractor *GetInteractor() {
		return this->Interactor;
	};

	void Execute(vtkObject *, unsigned long event, void *) override
	{
		vtkRenderWindowInteractor *interactor = this->GetInteractor();

		int lastPos[2];
		interactor->GetLastEventPosition(lastPos);
		int currPos[2];
		interactor->GetEventPosition(currPos);

		if (event == vtkCommand::LeftButtonPressEvent)
		{
			this->Slicing = 1;
		}
		else if (event == vtkCommand::LeftButtonReleaseEvent)
		{
			this->Slicing = 0;
		}
		else if (event == vtkCommand::MouseMoveEvent)
		{
			if (this->Slicing)
			{
				vtkImageReslice *reslice = this->ImageReslice;

				// Increment slice position by deltaY of mouse
				int deltaY = lastPos[1] - currPos[1];

				reslice->Update();
				double sliceSpacing = reslice->GetOutput()->GetSpacing()[2];
				vtkMatrix4x4 *matrix = reslice->GetResliceAxes();
				// move the center point that we are slicing through
				double point[4];
				double center[4];
				point[0] = 0.0;
				point[1] = 0.0;
				point[2] = sliceSpacing * deltaY;
				point[3] = 1.0;
				matrix->MultiplyPoint(point, center);
				matrix->SetElement(0, 3, center[0]);
				matrix->SetElement(1, 3, center[1]);
				matrix->SetElement(2, 3, center[2]);
				interactor->Render();
			}
			else
			{
				vtkInteractorStyle *style = vtkInteractorStyle::SafeDownCast(
					interactor->GetInteractorStyle());
				if (style)
				{
					style->OnMouseMove();
				}
			}
		}
	};

private:
	int Slicing;
	vtkImageReslice *ImageReslice;
	vtkRenderWindowInteractor *Interactor;
};

int main()
{
	vtkSmartPointer<vtkDICOMImageReader> pSrcImage = vtkDICOMImageReader::New();
	pSrcImage->SetDirectoryName("F:\\Data\\36040001\\");	// 本身就是float的数据
	pSrcImage->SetDataScalarTypeToUnsignedChar();
	pSrcImage->SetDataByteOrderToLittleEndian();
	pSrcImage->Update();
	// 使用vtkImageCast直接转化为float
	vtkSmartPointer<vtkImageCast> imgCast = vtkSmartPointer<vtkImageCast>::New();
	imgCast->SetInputData((vtkDataObject *)pSrcImage->GetOutput());
	imgCast->SetOutputScalarTypeToFloat();
	imgCast->Update();
	// 使用vtkImageShiftScale手动转换
	vtkSmartPointer<vtkImageShiftScale> imgShiftScale = vtkSmartPointer<vtkImageShiftScale>::New();
	imgShiftScale->SetInputData((vtkDataObject*)pSrcImage->GetOutput());
	//imgShiftScale->SetOutputScalarTypeToFloat();
	imgShiftScale->SetShift(0);	// 不偏移
	imgShiftScale->SetScale(2);	// 每个点*2
	imgShiftScale->Update();

	int extent[6];			// 切片数量,xyz三方向切片数量
	double spacing[3];		// 片间距,xyz三方向每两片之间的间距
	double origin[3];		// 原点,三维mm
	imgCast->GetOutput()->GetExtent(extent);
	imgCast->GetOutput()->GetSpacing(spacing);
	imgCast->GetOutput()->GetOrigin(origin);
	double center[3];		// 获取到中心点
	center[0] = origin[0] + spacing[0] * 0.5 * (extent[0] + extent[1]);
	center[1] = origin[1] + spacing[1] * 0.5 * (extent[2] + extent[3]);
	center[2] = origin[2] + spacing[2] * 0.5 * (extent[4] + extent[5]);
	static double sagittalElements[16] = {		// 移动矩阵位置全是0,默认不移动,只有旋转矩阵
		0, 0, -1, 0,
		1, 0, 0, 0,
		0, -1, 0, 0,
		0, 0, 0, 1 };
	vtkSmartPointer<vtkMatrix4x4> resliceAxes =
		vtkSmartPointer<vtkMatrix4x4>::New();
	resliceAxes->DeepCopy(sagittalElements);
	// Set the point through which to slice
	resliceAxes->SetElement(0, 3, center[0]);		// 设置的位置为移动矩阵的位置,就是移动到模型中心
	resliceAxes->SetElement(1, 3, center[1]);
	resliceAxes->SetElement(2, 3, center[2]);

	vtkSmartPointer<vtkImageReslice> reslice = vtkSmartPointer<vtkImageReslice>::New();
	reslice->SetInputConnection(imgCast->GetOutputPort());
	reslice->SetOutputDimensionality(2);
	reslice->SetResliceAxes(resliceAxes);
	reslice->SetInterpolationModeToLinear();

	// Create a greyscale lookup table
	vtkSmartPointer<vtkLookupTable> table = vtkSmartPointer<vtkLookupTable>::New();	// 颜色表
	table->SetRange(0, 500); // image intensity range
	table->SetValueRange(0.0, 1.0); // from black to white
	table->SetSaturationRange(0.0, 1.0); // no color saturation
	table->SetRampToLinear();
	table->Build();
	// Map the image through the lookup table
	vtkSmartPointer<vtkImageMapToColors> color = vtkSmartPointer<vtkImageMapToColors>::New();
	color->SetLookupTable(table);
	color->SetInputConnection(reslice->GetOutputPort());

	vtkSmartPointer<vtkImageActor> actx1 = vtkSmartPointer<vtkImageActor>::New();
	actx1->GetMapper()->SetInputConnection(color->GetOutputPort());

	vtkSmartPointer<vtkRenderer> renderer =
		vtkSmartPointer<vtkRenderer>::New();
	renderer->AddActor(actx1);
	vtkSmartPointer<vtkRenderWindow> window =
		vtkSmartPointer<vtkRenderWindow>::New();
	window->AddRenderer(renderer);

	vtkSmartPointer<vtkInteractorStyleImage> imageStyle =
		vtkSmartPointer<vtkInteractorStyleImage>::New();
	vtkSmartPointer<vtkRenderWindowInteractor> interactor =
		vtkSmartPointer<vtkRenderWindowInteractor>::New();
	interactor->SetInteractorStyle(imageStyle);
	window->SetInteractor(interactor);
	window->Render();

	vtkSmartPointer<vtkImageInteractionCallback> callback =
		vtkSmartPointer<vtkImageInteractionCallback>::New();
	callback->SetImageReslice(reslice);
	callback->SetInteractor(interactor);

	imageStyle->AddObserver(vtkCommand::MouseMoveEvent, callback);
	imageStyle->AddObserver(vtkCommand::LeftButtonPressEvent, callback);
	imageStyle->AddObserver(vtkCommand::LeftButtonReleaseEvent, callback);

	interactor->Start();

	return EXIT_SUCCESS;
}

上面代码,通过尝试显示不同的对象,可以得到不同的效果

 

5、彩色图像转黑白图像:

实际上,这和之前的图像数据更改很类似,一种方法可以用之前的程序,将红绿蓝分量均改成0.3R+0.59G+0.11B得到的值即可,当然,这里的比例数据可以根据情况更改。此外,vtk还提供了专门的对象来更改,vtkImageLuminance,可以将彩色图像变为灰度图像:

#include <vtkSmartPointer.h>
#include <vtkJPEGReader.h>
#include <vtkImageLuminance.h>
#include <vtkImageActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>

int main()
{
	vtkSmartPointer<vtkJPEGReader> reader = vtkSmartPointer<vtkJPEGReader>::New();
	reader->SetFileName("light.jpg");
	// 转化为黑白了
	vtkSmartPointer<vtkImageLuminance> luminanceFilter = vtkSmartPointer<vtkImageLuminance>::New();
	luminanceFilter->SetInputConnection(reader->GetOutputPort());
	luminanceFilter->Update();
	// 原始图片
	vtkSmartPointer<vtkImageActor> srcAct = vtkSmartPointer<vtkImageActor>::New();
	srcAct->SetInputData(reader->GetOutput());
	// 灰度图
	vtkSmartPointer<vtkImageActor> grayAct = vtkSmartPointer<vtkImageActor>::New();
	grayAct->SetInputData(luminanceFilter->GetOutput());
	double srcViewPort[4] = { 0.0, 0.0, 0.5, 1.0 };
	double grayViewPort[4] = { 0.5, 0.0, 1.0, 1.0 };

	vtkSmartPointer<vtkRenderer> srcRender = vtkSmartPointer<vtkRenderer>::New();
	srcRender->SetViewport(srcViewPort);
	srcRender->AddActor(srcAct);
	srcRender->ResetCamera();
	srcRender->SetBackground(1.0, 1.0, 1.0);
	vtkSmartPointer<vtkRenderer> grayRender = vtkSmartPointer<vtkRenderer>::New();
	grayRender->SetViewport(grayViewPort);
	grayRender->AddActor(grayAct);
	grayRender->ResetCamera();
	grayRender->SetBackground(1.0, 1.0, 1.0);

	vtkSmartPointer<vtkRenderWindow> renderwindow =
		vtkSmartPointer<vtkRenderWindow>::New();
	renderwindow->AddRenderer(srcRender);
	renderwindow->AddRenderer(grayRender);
	renderwindow->SetSize(1024, 375);
	renderwindow->Render();
	renderwindow->SetWindowName("RGB to Gray");

	vtkSmartPointer<vtkRenderWindowInteractor> rwi =
		vtkSmartPointer<vtkRenderWindowInteractor>::New();
	vtkSmartPointer<vtkInteractorStyleImage> style =
		vtkSmartPointer<vtkInteractorStyleImage>::New();
	rwi->SetInteractorStyle(style);
	rwi->SetRenderWindow(renderwindow);
	rwi->Initialize();

	rwi->Start();
	return 0;
}

如上图,彩色图像转化成灰度图像,通过像素点单个转换也会得到类似的效果。

6、彩色图像红绿蓝成分提取:

成分提取,实际上就是讲一幅彩色图像的红绿蓝三个分量都提取出来,可以得到一幅图的红色部分,绿色部分等,与PS里面的显示不同通道实际上是一样的。

#include <vtkSmartPointer.h>
#include <vtkJPEGReader.h>
#include <vtkImageExtractComponents.h>
#include <vtkImageActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>

int main()
{
	vtkSmartPointer<vtkJPEGReader> reader = vtkSmartPointer<vtkJPEGReader>::New();
	reader->SetFileName("light.jpg");
	// 成分提取器
	vtkSmartPointer<vtkImageExtractComponents> redComponent = vtkSmartPointer<vtkImageExtractComponents>::New();
	redComponent->SetInputConnection(reader->GetOutputPort());
	redComponent->SetComponents(0);	// red
	redComponent->Update();

	vtkSmartPointer<vtkImageExtractComponents> greenComponent = vtkSmartPointer<vtkImageExtractComponents>::New();
	greenComponent->SetInputConnection(reader->GetOutputPort());
	greenComponent->SetComponents(1);	// green
	greenComponent->Update();

	vtkSmartPointer<vtkImageExtractComponents> blueComponent = vtkSmartPointer<vtkImageExtractComponents>::New();
	blueComponent->SetInputConnection(reader->GetOutputPort());
	blueComponent->SetComponents(2);	// blue
	blueComponent->Update();
	// 图像演员
	vtkSmartPointer<vtkImageActor> srcActor = vtkSmartPointer<vtkImageActor>::New();
	srcActor->SetInputData(reader->GetOutput());
	vtkSmartPointer<vtkImageActor> redActor = vtkSmartPointer<vtkImageActor>::New();
	redActor->SetInputData(redComponent->GetOutput());
	vtkSmartPointer<vtkImageActor> greenActor = vtkSmartPointer<vtkImageActor>::New();
	greenActor->SetInputData(greenComponent->GetOutput());
	vtkSmartPointer<vtkImageActor> blueActor = vtkSmartPointer<vtkImageActor>::New();
	blueActor->SetInputData(blueComponent->GetOutput());
	// 窗口范围
	double srcView[4] = { 0.0, 0.0, 0.25, 1.0 };
	double redView[4] = { 0.25, 0.0, 0.5, 1.0 };
	double greenView[4] = { 0.5, 0.0, 0.75, 1.0 };
	double blueView[4] = { 0.75, 0.0, 1.0, 1.0 };

	vtkSmartPointer<vtkRenderer> srcRender = vtkSmartPointer<vtkRenderer>::New();
	srcRender->SetViewport(srcView);
	srcRender->AddActor(srcActor);
	srcRender->ResetCamera();
	srcRender->SetBackground(1.0, 1.0, 1.0);
	vtkSmartPointer<vtkRenderer> redRender = vtkSmartPointer<vtkRenderer>::New();
	redRender->SetViewport(redView);
	redRender->AddActor(redActor);
	redRender->ResetCamera();
	redRender->SetBackground(1.0, 1.0, 1.0);
	vtkSmartPointer<vtkRenderer> greenRender = vtkSmartPointer<vtkRenderer>::New();
	greenRender->SetViewport(greenView);
	greenRender->AddActor(greenActor);
	greenRender->ResetCamera();
	greenRender->SetBackground(1.0, 1.0, 1.0);
	vtkSmartPointer<vtkRenderer> blueRender =vtkSmartPointer<vtkRenderer>::New();
	blueRender->SetViewport(blueView);
	blueRender->AddActor(blueActor);
	blueRender->ResetCamera();
	blueRender->SetBackground(1.0, 1.0, 1.0);
	// Wnd
	vtkSmartPointer<vtkRenderWindow> renderwindow = vtkSmartPointer<vtkRenderWindow>::New();
	renderwindow->AddRenderer(srcRender);
	renderwindow->AddRenderer(redRender);
	renderwindow->AddRenderer(greenRender);
	renderwindow->AddRenderer(blueRender);
	renderwindow->SetSize(1024, 190);
	renderwindow->Render();
	renderwindow->SetWindowName("RGB Window");
	//设置交互
	vtkSmartPointer<vtkRenderWindowInteractor> rwi =
		vtkSmartPointer<vtkRenderWindowInteractor>::New();
	vtkSmartPointer<vtkInteractorStyleImage> style =
		vtkSmartPointer<vtkInteractorStyleImage>::New();
	rwi->SetInteractorStyle(style);
	rwi->SetRenderWindow(renderwindow);
	rwi->Initialize();

	rwi->Start();
	return 0;
}

效果如上图,四幅图像分别为原始图像,红色部分,绿色部分,蓝色部分,图中箭头所示,红色的光环(绿色蓝色部分非常少),在红色分量图中,清晰可见,在其他图中,几乎看不出。

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!