将四张图拼接在一起,新建作业,在配置中新建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
}
最终拼接效果如图所示:
参考: