在 Visiual Studio.NET 環境下通過VisionPro打開相機並獲取圖像的基本步驟如下:
1.創建CogFrameGrabberGigEs對象,獲取連接到圖像采集卡的所有相機設備。
2.通過CogFrameGrabberGigEs獲取單個對象ICogFrameGrabber的引用,支持的圖像格式、相機序列號等信息可以從該接口對象中獲取。
3.通過CogFrameGrabber的CreateAcqFifo方法創建ICogAcqFifo接口對象,在使用CreateAcqFifo方法時須指定圖像格式、相機端口等信息。
4.通過ICogAcqFifo接口的Acquire等方法可以獲取所需圖像數據。
5.程序退出前斷開CogFrameGrabber與硬件的連接,否則可能導致退出異常。
注意事項:
①相機的IP與網卡在同一網段,關閉防火牆,打開網卡巨幀等物理硬件參數設置正確。
②編譯平台選擇x64,否則即使相機物理連接沒有問題,也可能出現創建CogFrameGrabbers的Count屬性為零的情況,在AnyCPU模式下也不報錯,原因尚不清楚。
1 using System; 2 using System.Drawing; 3 using System.Collections; 4 using System.ComponentModel; 5 using System.Windows.Forms; 6 using System.Data; 7 using Cognex.VisionPro; 8
9 namespace ImageAcquire 10 { 11 public class Form1 : System.Windows.Forms.Form 12 { 13 private Cognex.VisionPro.Display.CogDisplay cogDisplay1;//用於進行圖像顯示的VisionPro控件
14 private System.Windows.Forms.Button button1; 15 private System.ComponentModel.Container components = null; 16 ICogAcqFifo myFifo = null; 17 ICogFrameGrabber myFrameGrabber = null; 18 public Form1() 19 { 20 InitializeComponent(); 21 InitializeAcquisition(); 22 } 23
24 protected override void Dispose( bool disposing ) 25 { 26 if( disposing ) 27 { 28 if (components != null) 29 { 30 components.Dispose(); 31 } 32 **//5-斷開CogFrameGrabber對象與硬件的連接。**
33 if(myFrameGrabber!=null) 34 myFrameGrabber.Disconnect(false); 35 } 36 base.Dispose( disposing ); 37 } 38
39 private void InitializeAcquisition() 40 { 41 const string VIDEO_FORMAT = "Sony XC75 640x480"; 42 **// 1-創建CogFrameGrabbers對象**
43 CogFrameGrabbers myFrameGrabbers = new CogFrameGrabbers(); 44 **//2-獲取單個ICogFrameGrabber接口對象**
45 myFrameGrabber = myFrameGrabbers[0]; 46 **//3-創建ICogAcqFifo接口對象**
47 myFifo = myFrameGrabber.CreateAcqFifo(VIDEO_FORMAT,Cognex.VisionPro.CogAcqFifoPixelFormatConstants.Format8Grey, 0, false); 48 } 49
50 [STAThread] 51 static void Main() 52 { 53 Application.Run(new Form1()); 54 } 55
56 private void button1_Click(object sender, System.EventArgs e) 57 { 58 int trigNum; 59 **//4-通過ICogAcqFifo接口對象的Acquire方法進行圖像采集。**
60 cogDisplay1.Image = myFifo.Acquire(out trigNum); 61 } 62 } 63 }