VTK知识学习(32)-图像运算

1、数学运算

        vklmageMathematics 提供了基本的一元和二元数学操作。根据不同的操作,需要一个或者两个输入图像。二元数学操作要求两个输入图像具有相同的像素数据类型和颜色组分。当两个图像大小不同时,输出图像的范围为两个输入图像范围的并集,并且原点和像素间隔与第一个输入图像保持一致。VTK知识学习(32)-图像运算_第1张图片

       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中支持的二元数学操作如下:

  • SetOperationToAdd:两个图像对应像素加法运算。
  • SetOperationToSubtract:两个图像对应像素减法运算。
  • SetOperationToMultiply:两个图像对应像素相乘运算。
  • SetOperationToDivide:两个图像对应像素相除运算。
  • SetOperationToConjugate:将两个标量图像对应像素组合为共轭复。
  • SetOperationToComplexMultiply:两个图像对应像素复数乘法运算。
  • SetOperationToMin:取两个图像对应像素中的较小值。        
  • SetOperationToMax:取两个图像对应像素中的较大值。

 vtkImageMathematics中支持的一元数学操作如下:

  • SetOperationToInvert:图像像素值取倒数运算。
  • SetOperationToSin:图像像素值正弦运算。
  • SetOperationToCos:图像像素值余弦运算。
  • SetOperationToExp:图像像素值自然指数运算。
  • SetOperationToLog:图像像素值自然对数运算
  • SetOperationToAbsoluteValue:图像像素值取绝对值。
  • SetOperationToSquare:图像像素值平方运算。
  • SetOperationToSquareRoot:图像像素值平方根运算。
  • SetOperationTOATAN:图像像素值反正切运算。
  • SetOperationTOATAN2:图像像素值二元反正切运算。
  • SetOperationToMultiplyByK:图像像素值乘以常数K,需要先调用 SetConstantK()设置K 值。
  • SetOperationToAddConstant:图像像素值加上常数K,需要先调用 SetConstantK()设置K值。
  • SetOperationToReplaceCByK:将图像中像素为C的像素值替换为K,需要先调用SetConstantK()和 SetConstantC 设置K和C值。

2、逻辑运算

        vtkImageLogic 接受一个或者两个图像进行布尔逻辑运算,该类主要支持与(AND)、或(OR)、异或(XOR)、与非(NAND)、或非(NOR)以及非(NOT)。当选择一元操作符时只对第一个输入图像有效。当选择二元操作符时,两个输入图像的类型必须一致。VTK知识学习(32)-图像运算_第2张图片

        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 设置逻辑运算的函数如下:

  • SetOperationToAnd():逻辑与。
  • SetOperationToOr():逻辑或。
  • SetOperationToXor():逻辑异或。
  • SetOperationToNand():逻辑与非。
  • SetOperationToNor():逻辑或非。
  • iSetOperationToNot():逻辑非。

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