VisionPro软件Image Stitch拼接算法

前言:

2D图像拼接的3种情景
1.一只相机取像位置固定,或者多只相机固定位置拍图,硬拷贝拼图,采用CopyRegion工具实现
2.一只或多只相机在多个位置拍照,相机视野互相重叠,基于Patmax特征定位后,无缝
拼图;采用CogImageStitch类实现;
3.一只或多只相机在多个位置拍照,相机视野只有小范围重叠,或者不重叠,无法使用 Patmax特征定位,可以用标定板标定位置关系,使用CogImageStitch类实现拼图.

注意:此方法是是预先标定的位置关系,如果采用1只相机多个位置拍摄,需要机构保证重复运动的精度在允许范围内,否则可能造成图像错位。

注意:无论是哪种拼接方式,单相机或是多相机拍照,都需要尽量调节到同一个高度拍照,否则可能造成图像重影,模糊等问题;

1.CopyRegionTool硬拷贝拼图

1.请参考QuickBuild自带例程: Script_Stitch_Job.vpp
2.在CogJob的作业属性-取像脚本中实现多张图像拷 贝拼接
3.注意CopyRegion工具的属性, DesinationImageAlignmentX和Y用于指定子图像在拼接大图的位置偏移

在这里插入图片描述

2.基于互相重叠的Patmax特征无缝拼接

请参考“TB_Patmax算法拼.vpp”; 此VPP实现3张图像上下拼接,其他拼接组合可以自行改写程序

流程:
1).添加Patmax工具,训练各个重叠特征;注意相邻的两张图同样的特征使用同一个Patmax工具即可;
2).载入第一张图像,运行整个CogJob,将图像给到TB_StitchImage1,Patmax定位结果给到Image1Pose1;注意不要用连线;
3).对其他图像重复同样的工作,中间的图像有两个PMA结果,需要连2个Pose;
4).TB_Stitch输出的图像即为拼接后图。

在这里插入图片描述
在这里插入图片描述
这里CogImageStitch类使用的方法:

1).AllocateBlendingBuffer,指定图像大小为拼接后图的尺寸,Transform不需要特别设置,在(0,0)附近即可,Scale为1;  
2).分别为3张图建立CogTransform2DLinear;第一个Transform建立在(0,0)位置, 其他图Transform关系依次Compose 前一个相邻Pose的Invert,因为后一个是依据前一个的位置关系来偏移的。以上下3张图拼接为例,就建立了图示的关系;         
3).将图像和Tansform关系分别传入不同的CogFixtureTool,在图像中添加对应的坐标系。注意坐标系名称不能一样;或者用代码AddSpace手动添加坐标系也可以;
4).生成的带新坐标系的图像传入CogImageStitch工具,用BlendImageIntoBuffer方法,会将每张图像对应添加到拼接大图的对
应位置。
5).调用FillDestinationImageFromBuffer来生成拼接图,完成。

注意BlendImageInfoBuffer和OverwriteImage两种方法的区别,Overwrite在像素重叠部分是互相覆盖了,而Blend模式是按照不同权重混合起来,因此更接近无缝拼接。

详细代码如下:

public override bool GroupRun(ref string message, ref CogToolResultConstants result)
  {
    // To let the execution stop in this script when a debugger is attached, uncomment the following lines.
    // #if DEBUG
    // if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break();
    // #endif

    CogImage8Grey Img1 =(CogImage8Grey) this.Inputs.Image1;
    CogImage8Grey Img2 =(CogImage8Grey) this.Inputs.Image2;
    CogImage8Grey Img3 =(CogImage8Grey) this.Inputs.Image3;
    CogImage8Grey StitchedImg = new CogImage8Grey(Img1.Width+500, Img1.Height * 3);
        
    CogImageStitch mStitch = new CogImageStitch();
  //  CogImageStitch.FillDefaultWeightImage(StitchedImg);
    
    CogImage8Grey imgMask0 = new CogImage8Grey(Img1.Width, Img1.Height);
    CogImageStitch.FillDefaultWeightImage(imgMask0);
    CogImage8Grey imgMask1 = new CogImage8Grey(Img1.Width, Img1.Height);
    CogImageStitch.FillDefaultWeightImage(imgMask1);
    CogImage8Grey imgMask2 = new CogImage8Grey(Img1.Width, Img1.Height);
    CogImageStitch.FillDefaultWeightImage(imgMask2);
    
    CogTransform2DLinear trans = new CogTransform2DLinear();
    trans.Scaling = 1;
    trans.TranslationX = 10;
    trans.TranslationY = 10;
    
    mStitch.AllocateBlendingBuffer(Img1.Width+500, Img1.Height * 3, trans);
   
    CogCoordinateSpaceTree mSpace1 = Img1.CoordinateSpaceTree;
    CogCoordinateSpaceTree mSpace2 = Img2.CoordinateSpaceTree;
    CogCoordinateSpaceTree mSpace3 = Img3.CoordinateSpaceTree;

    CogTransform2DLinear mTrans1 =new CogTransform2DLinear();
    mTrans1.Scaling = 1;
    mTrans1.TranslationX = 0;
    mTrans1.TranslationY = 0;
    mTrans1.Rotation = 0;
 //   mSpace1.AddSpace("@", "foo_0", mTrans1, true, CogAddSpaceConstants.ReplaceDuplicate);
    CogTransform2DLinear mTrans2 =this.Inputs.Image2Pose1.Compose(this.Inputs.Image1Pose1.Invert());
 //   mSpace2.AddSpace("@", "foo_1", mTrans2, true, CogAddSpaceConstants.ReplaceDuplicate);
    CogTransform2DLinear mTrans3 = this.Inputs.Image3Pose2.Compose(this.Inputs.Image2Pose2.Invert()).Compose(mTrans2);
 //   mSpace3.AddSpace("@", "foo_2", mTrans3, true, CogAddSpaceConstants.ReplaceDuplicate);
    
  /*   mStitch.BlendImageIntoBuffer(Img1, null);
    mStitch.BlendImageIntoBuffer(Img2, null);
    mStitch.BlendImageIntoBuffer(Img3, null);
 */   
    this.Tools.CogFixtureTool1.RunParams.UnfixturedFromFixturedTransform = mTrans1;
    this.Tools.CogFixtureTool1.Run();
    this.Tools.CogFixtureTool2.RunParams.UnfixturedFromFixturedTransform = mTrans2;
    this.Tools.CogFixtureTool2.Run();
    this.Tools.CogFixtureTool3.RunParams.UnfixturedFromFixturedTransform = mTrans3;
    this.Tools.CogFixtureTool3.Run();
    
    mStitch.BlendImageIntoBuffer((CogImage8Grey)this.Tools.CogFixtureTool1.OutputImage, imgMask0);
    mStitch.BlendImageIntoBuffer((CogImage8Grey)this.Tools.CogFixtureTool2.OutputImage,imgMask1);
    mStitch.BlendImageIntoBuffer((CogImage8Grey)this.Tools.CogFixtureTool3.OutputImage,imgMask2);    
  
    mStitch.FillDestinationImageFromBuffer(StitchedImg);
   
  //  MessageBox.Show(mTrans3.TranslationX.ToString() + "   " + mTrans3.TranslationY.ToString());

 
    this.Outputs.StitchedImage = StitchedImg;
    
    return false;
  }

3.使用标定板拼接

请参考“TB_标定板拼.vpp”; 此VPP实现3张图像上下拼接,其他拼接组合可以自行改写程序,标定板可以使用二维码标定板,也可以用Cognex标准标定板;
如果使用二维码标定板,不要求视野重叠,使用带 Cognex标记的标定板,则需要视野重叠,以便于各个拍照位置建立统一的标定板坐标系。

流程如下:

1).标定板放好,固定不动。大小要能够覆盖整个拍照视野范围 2).3只相机或同一个相机的3个位置对标定板拍照,执行。
CheckBoard标定;得到outputImage和OutputImageMask;注意此时每张图的输出坐标系都是标定片坐标系;
3).打开距离标定板坐标系原点最近的那张图,获取坐标原点在Root
space下的X和Y坐标值;利用标定板图计算图像的像素坐标与标定板坐标系的比例关系;分别输入到 ToolBlock的dScale,dTransX, dTransY中;
4).执行ToolBlock,即可得到拼接后图像。

在这里插入图片描述

CogImageStitch类使用的方法:

1).由于各张子图像已经采用标定板建立了标定板坐标系,因此前面特征拼接方法不同的是,这里不需要再自行建立坐标系关系,使用标定板坐标系即可;
2).但是在AllocateBlendingBuffer时,需要指定RootFromBlendingBuffer的坐标系变换关系。由于BlendingBuffer分配时使用的标定板坐标系,而图像是从rootspace下copy像素,因此变换关系的比例和Translation需要在定义BlendingBuffer时指定。
3)其他部分按照CogImageStitch的使用方法调用即可。

详细代码如下

public override bool GroupRun(ref string message, ref CogToolResultConstants result)
  {
    // To let the execution stop in this script when a debugger is attached, uncomment the following lines.
    // #if DEBUG
    // if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break();
    // #endif

    CogImage8Grey Img1 =(CogImage8Grey) this.Inputs.Image1;
    CogImage8Grey Img2 =(CogImage8Grey) this.Inputs.Image2;
    CogImage8Grey Img3 =(CogImage8Grey) this.Inputs.Image3;
    CogImage8Grey imgMask0 = (CogImage8Grey) this.Inputs.CalibMask1;
    CogImage8Grey imgMask1 = (CogImage8Grey) this.Inputs.CalibMask2;
    CogImage8Grey imgMask2 = (CogImage8Grey) this.Inputs.CalibMask3;
    CogImage8Grey StitchedImg = new CogImage8Grey(Img1.Width+500, Img1.Height * 3+500);
        
    CogImageStitch mStitch = new CogImageStitch();
  //  CogImageStitch.FillDefaultWeightImage(StitchedImg);
    
    CogImage8Grey imgWeight0 = new CogImage8Grey(Img1.Width, Img1.Height);
    CogImageStitch.FillDefaultWeightImage(imgWeight0);
    CogImage8Grey imgWeight1 = new CogImage8Grey(Img2.Width, Img2.Height);
    CogImageStitch.FillDefaultWeightImage(imgWeight1);
    CogImage8Grey imgWeight2 = new CogImage8Grey(Img3.Width, Img3.Height);
    CogImageStitch.FillDefaultWeightImage(imgWeight2);
    
    CogTransform2DLinear trans = new CogTransform2DLinear();
    trans.Scaling = this.Inputs.dScale;
    trans.TranslationX = this.Inputs.dTransX;
    trans.TranslationY = this.Inputs.dTransY;
    
    mStitch.AllocateBlendingBuffer(Img1.Width+500, Img1.Height * 3+500, trans);
   
    
    mStitch.BlendImageIntoBuffer(Img1, imgWeight0,imgMask0,0,0);
    mStitch.BlendImageIntoBuffer(Img2,imgWeight1,imgMask1,0,0);
    mStitch.BlendImageIntoBuffer(Img3,imgWeight2,imgMask2,0,0);    
  
    mStitch.FillDestinationImageFromBuffer(StitchedImg);
   
  //  MessageBox.Show(mTrans3.TranslationX.ToString() + "   " + mTrans3.TranslationY.ToString());

 
    this.Tools.CogBlobTool1.InputImage = StitchedImg;
    this.Tools.CogBlobTool1.Run();
    
    return false;
  }

总结:
1.小视野多次取像,每张图片单独使用检测工具,精度可满足需求;
不够直观,调试复杂;
2.小视野多次取像,根据标定结果,进行图像拼接;
精度可满足需求;同时更加直观,客户接受度高;检测工具使用更加方便;后期维护及设备复制更省心;

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱凤的小光

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值