vklmageMathematics 提供了基本的一元和二元数学操作。根据不同的操作,需要一个或者两个输入图像。二元数学操作要求两个输入图像具有相同的像素数据类型和颜色组分。当两个图像大小不同时,输出图像的范围为两个输入图像范围的并集,并且原点和像素间隔与第一个输入图像保持一致。
private void TestMathematics()
{
//绘制一个暗红色矩形
vtkImageCanvasSource2D imageSource = vtkImageCanvasSource2D.New();
imageSource.SetNumberOfScalarComponents(3);
imageSource.SetScalarTypeToUnsignedChar();
imageSource.SetExtent(0, 4, 0, 4, 0, 0);
imageSource.SetDrawColor(100, 0, 0);
imageSource.FillBox(0, 4, 0, 4);
imageSource.Update();
//将图像中的所有相互值乘以一个常数K
vtkImageMathematics imageMath = vtkImageMathematics.New();
imageMath.SetOperationToMultiplyByK(); //设置计算为 像素值乘以常数K
imageMath.SetConstantK(2); //设置常数K
imageMath.SetInputConnection(imageSource.GetOutputPort());
imageMath.Update();
vtkImageActor originalActor = vtkImageActor.New();
originalActor.SetInputData(imageSource.GetOutput());
vtkImageActor magnifyActor = vtkImageActor.New();
magnifyActor.SetInputData(imageMath.GetOutput());
vtkRenderer originalRenderer = vtkRenderer.New();
originalRenderer.SetViewport(0, 0, 0.5, 1);
originalRenderer.AddActor(originalActor);
originalRenderer.ResetCamera();
originalRenderer.SetBackground(1, 1, 1);
vtkRenderer magnifyRender = vtkRenderer.New();
magnifyRender.SetViewport(0.5, 0, 1, 1);
magnifyRender.AddActor(magnifyActor);
magnifyRender.ResetCamera();
magnifyRender.SetBackground(1, 1, 1);
vtkRenderWindow renderWindow = renderWindowControl.RenderWindow;
renderWindow.AddRenderer(originalRenderer);
renderWindow.AddRenderer(magnifyRender);
renderWindow.Render();
}
先生成一幅图像,图像中绘制了一个暗红色矩形;然后定义vkImageMathematics对象,并调用 SetOperationToMultiplyByK()函数来将图像中的所有像素值乘以一个常数K,这里常数值为 2.0。
vtkImageMathematics中支持的二元数学操作如下:
vtkImageMathematics中支持的一元数学操作如下:
vtkImageLogic 接受一个或者两个图像进行布尔逻辑运算,该类主要支持与(AND)、或(OR)、异或(XOR)、与非(NAND)、或非(NOR)以及非(NOT)。当选择一元操作符时只对第一个输入图像有效。当选择二元操作符时,两个输入图像的类型必须一致。
private void TestImageLogic()
{
//绘制一个矩形
vtkImageCanvasSource2D imageSource1 = vtkImageCanvasSource2D.New();
imageSource1.SetNumberOfScalarComponents(1);
imageSource1.SetScalarTypeToUnsignedChar();
imageSource1.SetExtent(0, 100, 0, 100, 0, 0);
imageSource1.SetDrawColor(0);
imageSource1.FillBox(0, 100, 0, 100);
imageSource1.SetDrawColor(255);
imageSource1.FillBox(20, 60, 20, 60);
imageSource1.Update();
//绘制一个矩形
vtkImageCanvasSource2D imageSource2 = vtkImageCanvasSource2D.New();
imageSource2.SetNumberOfScalarComponents(1);
imageSource2.SetScalarTypeToUnsignedChar();
imageSource2.SetExtent(0, 100, 0, 100, 0, 0);
imageSource2.SetDrawColor(0);
imageSource2.FillBox(0, 100, 0, 100);
imageSource2.SetDrawColor(255);
imageSource2.FillBox(40, 80, 40, 80);
imageSource2.Update();
vtkImageLogic imageLogic = vtkImageLogic.New();
imageLogic.SetInput1Data(imageSource1.GetOutput());
imageLogic.SetInput2Data(imageSource2.GetOutput());
imageLogic.SetOperationToXor(); //设置逻辑操作算子为异或操作 相反为真
imageLogic.SetOutputTrueValue(200); //设置当两个图像对应像素值异或结果为真时的输出像素值
imageLogic.Update();
ShowImageBy3(imageSource1.GetOutput(), imageSource2.GetOutput(), imageLogic.GetOutput());
}
先生成了两个二值图像,两个图像中的前景为两个部分重叠的矩形。定义 vkmageLogic对象,并设置两个图像为输入,SetOperationToXor()设置逻辑操作算子为异或操作,并且SetOutputTrueValue()设置当两个图像对应像素值异或结果为真时的输出像素值,可以看作两个矩形的重叠部分像素值相同,因此输出为0:矩形的不重看部分像素值一个为0,一个为255,因此异或结果为真,输出值为128。
vtkImageLogic 设置逻辑运算的函数如下: