在 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 }