Visionpro实现多图像拼接

将四张图拼接在一起,新建作业,在配置中新建C#脚本,添加代码如下

using System;
using System.Threading;
using System.Windows.Forms;
using Cognex.VisionPro;
using Cognex.VisionPro.QuickBuild;
using Cognex.VisionPro.ImageProcessing;

public class UserScript : CogJobBaseScript
{
  private CogCopyRegionTool  imageStitcher;
  private int                Counter;
  

#region "When an Acq Fifo Has Been Constructed and Assigned To The Job"
  // This function is called when a new fifo is assigned to the job.  This usually
  // occurs when the "Initialize Acquisition" button is pressed on the image source
  // control.  This function is where you would perform custom setup associated
  // with the fifo.
  public override void AcqFifoConstruction(Cognex.VisionPro.ICogAcqFifo fifo)
  {
  }
#endregion

#region "When an Acquisition is About To Be Started"
  // Called before an acquisition is started for manual and semi-automatic trigger
  // models.  If "Number of Software Acquisitions Pre-queued" is set to 1 in the
  // job configuration, then no acquisitions should be in progress when this
  // function is called.
  public override void PreAcquisition()
  {
    // 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

  }
#endregion

#region "When an Acquisition Has Just Completed"
  // Called immediately after an acquisition has completed.
  // Return true if the image should be inspected.
  // Return false to skip the inspection and acquire another image.
  public override bool PostAcquisitionRefInfo(ref Cognex.VisionPro.ICogImage image,
                                                  Cognex.VisionPro.ICogAcqInfo info)
  {
    // 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
    
    Counter = Counter + 1;
    
    if(Counter == 1)
    {
      //Create a new tool
      imageStitcher = new CogCopyRegionTool();
      
      //Create a destination image and assign it to the tool
      CogImage8Grey stitchedImage = new CogImage8Grey();
      stitchedImage.Allocate(image.Width * 2, image.Height * 2);
      imageStitcher.DestinationImage = stitchedImage;
      
      imageStitcher.Region = null;
      imageStitcher.RunParams.ImageAlignmentEnabled = true;
      
      //First sub-image goes into the upper left corner
      imageStitcher.RunParams.DestinationImageAlignmentX = 0;
      imageStitcher.RunParams.DestinationImageAlignmentY = 0;
      
    }
    
    else if(Counter == 2)
    {
      //Second sub-image goes into the upper right cornet
      imageStitcher.RunParams.DestinationImageAlignmentX = image.Width;
      imageStitcher.RunParams.DestinationImageAlignmentY = 0;
    }
    
    else if(Counter == 3)
    {
      //Third sub-image goes into the lower left corner
      imageStitcher.RunParams.DestinationImageAlignmentX = 0;
      imageStitcher.RunParams.DestinationImageAlignmentY = image.Height;
    }
    
    else
    {
      //Final sub-image goes into the lower right corner
      imageStitcher.RunParams.DestinationImageAlignmentX = image.Width;
      imageStitcher.RunParams.DestinationImageAlignmentY = image.Height;
    }
    
    //Run the tool to add the just-acquired sub-image
    imageStitcher.InputImage = CogImageConvert.GetIntensityImage(image, 0, 0, image.Width, image.Height);
    imageStitcher.Run();
    
    if(Counter == 4)
    {
      //Set the acquired image to the final stitched image
      image = imageStitcher.OutputImage;
      
      //Reset to begin a new stitched image next time
      imageStitcher = null;
      Counter = 0;
      
      //Return true to inspect the stitched image
      return true;
    }

    else
    {
      //Return false to skip inspectiona nd acquire the next sub-image
      return false;
    }
  }
#endregion

#region "When the Script is Initialized"
  //Perform any initialization required by your script here.
  public override void Initialize(CogJob jobParam)
  {
    //DO NOT REMOVE - Call the base class implementation first - DO NOT REMOVE
    base.Initialize(jobParam);
    
    imageStitcher = null;
    Counter = 0;
  }
#endregion

}

最终拼接效果如图所示:

 

参考:

蔚来教育企业店

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值