一.Visionpro脚本用途
作业脚本是对相机取像进行控制的脚本,如设置相机的帧率,曝光,频闪,自动对焦等等功能.
二.作业脚本继承关系:
Public Class UserScript Inherits CogJobBaseScript
CogJobBaseScript类的成员如下
-
job 这个成员可以获取控制你脚本的CogJob;
2.当一个图像采集先进先出队列构建并分配工作会调用该方法,当点击初始化图像来源按钮时候,将会构建图像采集先进先出队列并分配工作.
public override void AcqFifoConstruction(Cognex.VisionPro.ICogAcqFifo fifo)
{
}
3.当手动图像采集和半自动触发图像采集以前调用该方法
public override void PreAcquisition()
{
}
4.这个函数和PostAcquisition相似,图像将以引用的方式传递进来,如果函数返回TRUE,则QuickBuild将会立即处理这个图像,如果返回FALSE,这个图像将不会被处理,QuickBuild接着取下一张图.
public override void PreAcquisitionRef()
{
}
5.当图像采集完后立即调用该方法
public override bool PostAcquisitionRefInfo(ref Cognex.VisionPro.ICogImage image,Cognex.VisionPro.ICogAcqInfo info)
{
}
6.当脚本初始化的时候调用
public override void Initialize(CogJob jobParam)
{
base.Initialize(jobParam);
}
例子中将读取的图片通过作业脚本转换为灰度图。
在作业栏目中“配置”->"作业属性"->"编辑脚本"中选择C#脚本,然后进行编写,其将彩色图像转换为灰度图像代码如下:
using System;
using Cognex.VisionPro;
using Cognex.VisionPro.QuickBuild;
using System.Drawing.Imaging;
using Cognex.VisionPro.ImageProcessing;
using System.Windows.Forms;
public class UserScript : CogJobBaseScript
{
#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
//将输入图像的一部分复制到新的输出图像或目标图像,或者使用常数灰度值填充输入区域的一部分
CogCopyRegionTool imageStitcher;
//CogCopyRegionTool实例化
imageStitcher=new CogCopyRegionTool();
//CogImage8Grey实例化
CogImage8Grey stitchedImage = new CogImage8Grey();
//初始化图像
stitchedImage.Allocate(image.Width, image.Height);
imageStitcher.DestinationImage = stitchedImage;
//对输入图像的部分进行复制,如果为空是对整张图复制
imageStitcher.Region = null;
//将输入图像和目标图像对齐,如果为FALSE则表示目标图像将以左上角对齐
imageStitcher.RunParams.ImageAlignmentEnabled = true;
imageStitcher.RunParams.DestinationImageAlignmentX = 0;
imageStitcher.RunParams.DestinationImageAlignmentY = 0;
//将图像转化为灰度图像
imageStitcher.InputImage = CogImageConvert.GetIntensityImage(image, 0, 0, image.Width, image.Height);
imageStitcher.Run();
imageStitcher.OutputImage.ToBitmap().Save(@"D:\C Add Add\VisionPro\Visionpro如何编写作业(Job)脚本\result.bmp");
image = imageStitcher.OutputImage;
return true;
}
#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);
}
#endregion
}
参考: