VTK修炼之道23:图像基本操作_灰度图像映射成伪彩色图像(查表法)

1.查表法伪彩图映射

图像彩色映射的原理是首先生成一个颜色查找表,然后根据图像的一个标量值向颜色查找表中查找对应的颜色,并用新颜色值替代原来的像素值。VTK中vtkImageMapToColors负责图像彩色映射,vtkLookUpTable负责生成颜色查找表。

下面代码说明了怎样进行彩色映射:

#include 
VTK_MODULE_INIT(vtkRenderingOpenGL);

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
int main()
{
	vtkSmartPointer reader =
		vtkSmartPointer::New();
	reader->SetFileName("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 origActor =
		vtkSmartPointer::New();
	origActor->SetInputData(reader->GetOutput());

	vtkSmartPointer colorActor =
		vtkSmartPointer::New();
	colorActor->SetInputData(colorMap->GetOutput());
	//化妆
	double origView[4] = { 0.0, 0.0, 0.5, 1.0 };
	double colorView[4] = { 0.5, 0.0, 1.0, 1.0 };
	vtkSmartPointer origRender =
		vtkSmartPointer::New();
	origRender->SetViewport(origView);
	origRender->AddActor(origActor);
	origRender->ResetCamera();
	origRender->SetBackground(1.0, 0.0, 0.0);

	vtkSmartPointer colorRender =
		vtkSmartPointer::New();
	colorRender->SetViewport(colorView);
	colorRender->AddActor(colorActor);
	colorRender->ResetCamera();
	colorRender->SetBackground(0.0, 0.0, 0.0);
	//舞台
	vtkSmartPointer renderwindow =
		vtkSmartPointer::New();
	renderwindow->AddRenderer(origRender);
	renderwindow->AddRenderer(colorRender);
	renderwindow->SetSize(640, 320);
	renderwindow->SetWindowName("GrayToColor");
	//设置交互
	vtkSmartPointer rwi =
		vtkSmartPointer::New();
	vtkSmartPointer style =
		vtkSmartPointer::New();
	rwi->SetInteractorStyle(style);
	rwi->SetRenderWindow(renderwindow);
	rwi->Initialize();

	rwi->Start();
	return 0;
}

输出图像:

VTK修炼之道23:图像基本操作_灰度图像映射成伪彩色图像(查表法)_第1张图片

2.参看资料

1.《C++ primer》
2.《The VTK User’s Guide – 11thEdition》
3.《The Visualization Toolkit – AnObject-Oriented Approach To 3D Graphics (4th Edition)》
4.  张晓东, 罗火灵. VTK图形图像开发进阶[M]. 机械工业出版社, 2015.

你可能感兴趣的:(VTK修炼之道,VTK,三维数据渲染进阶)