一.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 }