一.Visionpro脚本用途
作业脚本是对相机取像进行控制的脚本,如设置相机的帧率,曝光,频闪,自动对焦等等功能.
二.作业脚本继承关系:
Public Class UserScript Inherits CogJobBaseScript
CogJobBaseScript类的成员如下
job 这个成员可以获取控制你脚本的CogJob;
2.当一个图像采集先进先出队列构建并分配工作会调用该方法,当点击初始化图像来源按钮时候,将会构建图像采集先进先出队列并分配工作.
1 public override void AcqFifoConstruction(Cognex.VisionPro.ICogAcqFifo fifo) 2
3 { 4
5
6
7 }
3.当手动图像采集和半自动触发图像采集以前调用该方法
1 public override void PreAcquisition() 2
3 { 4
5
6
7 }
4.这个函数和PostAcquisition相似,图像将以引用的方式传递进来,如果函数返回TRUE,则QuickBuild将会立即处理这个图像,如果返回FALSE,这个图像将不会被处理,QuickBuild接着取下一张图.
1 public override void PreAcquisitionRef() 2
3 { 4
5
6
7 }
5.当图像采集完后立即调用该方法
1 public override bool PostAcquisitionRefInfo(ref Cognex.VisionPro.ICogImage image,Cognex.VisionPro.ICogAcqInfo info) 2
3 { 4
5
6
7 }
6.当脚本初始化的时候调用
1 public override void Initialize(CogJob jobParam) 2
3 { 4
5 base.Initialize(jobParam); 6
7 }
例子中将读取的图片通过作业脚本转换为灰度图。
在作业栏目中“配置”->"作业属性"->"编辑脚本"中选择C#脚本,然后进行编写,其将彩色图像转换为灰度图像代码如下:
1 using System; 2 using Cognex.VisionPro; 3 using Cognex.VisionPro.QuickBuild; 4 using System.Drawing.Imaging; 5 using Cognex.VisionPro.ImageProcessing; 6 using System.Windows.Forms; 7
8 public class UserScript : CogJobBaseScript 9 { 10
11 #region "When an Acq Fifo Has Been Constructed and Assigned To The Job"
12 // This function is called when a new fifo is assigned to the job. This usually 13 // occurs when the "Initialize Acquisition" button is pressed on the image source 14 // control. This function is where you would perform custom setup associated 15 // with the fifo.
16 public override void AcqFifoConstruction(Cognex.VisionPro.ICogAcqFifo fifo) 17 { 18 } 19 #endregion
20
21 #region "When an Acquisition is About To Be Started"
22 // Called before an acquisition is started for manual and semi-automatic trigger 23 // models. If "Number of Software Acquisitions Pre-queued" is set to 1 in the 24 // job configuration, then no acquisitions should be in progress when this 25 // function is called.
26 public override void PreAcquisition() 27 { 28 // To let the execution stop in this script when a debugger is attached, uncomment the following lines. 29 // #if DEBUG 30 // if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break(); 31 // #endif
32
33 } 34 #endregion
35
36 #region "When an Acquisition Has Just Completed"
37 // Called immediately after an acquisition has completed. 38 // Return true if the image should be inspected. 39 // Return false to skip the inspection and acquire another image.
40 public override bool PostAcquisitionRefInfo(ref Cognex.VisionPro.ICogImage image, 41 Cognex.VisionPro.ICogAcqInfo info) 42 { 43 // To let the execution stop in this script when a debugger is attached, uncomment the following lines. 44 // #if DEBUG 45 // if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break(); 46 // #endif 47 //将输入图像的一部分复制到新的输出图像或目标图像,或者使用常数灰度值填充输入区域的一部分
48 CogCopyRegionTool imageStitcher; 49
50 //CogCopyRegionTool实例化
51 imageStitcher=new CogCopyRegionTool(); 52
53 //CogImage8Grey实例化
54 CogImage8Grey stitchedImage = new CogImage8Grey(); 55
56 //初始化图像
57 stitchedImage.Allocate(image.Width, image.Height); 58
59 imageStitcher.DestinationImage = stitchedImage; 60
61 //对输入图像的部分进行复制,如果为空是对整张图复制
62 imageStitcher.Region = null; 63
64 //将输入图像和目标图像对齐,如果为FALSE则表示目标图像将以左上角对齐
65 imageStitcher.RunParams.ImageAlignmentEnabled = true; 66 imageStitcher.RunParams.DestinationImageAlignmentX = 0; 67 imageStitcher.RunParams.DestinationImageAlignmentY = 0; 68
69 //将图像转化为灰度图像
70 imageStitcher.InputImage = CogImageConvert.GetIntensityImage(image, 0, 0, image.Width, image.Height); 71 imageStitcher.Run(); 72
73 imageStitcher.OutputImage.ToBitmap().Save(@"D:\C Add Add\VisionPro\Visionpro如何编写作业(Job)脚本\result.bmp"); 74
75 image = imageStitcher.OutputImage; 76
77
78 return true; 79 } 80 #endregion
81
82 #region "When the Script is Initialized"
83 //Perform any initialization required by your script here.
84 public override void Initialize(CogJob jobParam) 85 { 86 //DO NOT REMOVE - Call the base class implementation first - DO NOT REMOVE
87 base.Initialize(jobParam); 88 } 89 #endregion
90
91 }