This topic contains the following sections.
VisionPro supports image acquisition with an array of widely available Charge Coupled Device (CCD) cameras as well as the line of Cognex Digital (CDC) Cameras. The process of acquiring an image with your Visual Studio.NET application differs depending on the type of camera you are using. This topic describes how to create a Visual Studio.NET application that acquires an image from a CCD camera using a software trigger and displays the image using the Cognex Display control. For a description of acquiring images with a CDC camera, see the topic Acquiring Images with a CDC Camera and Frame Grabber. In addition, see the topic How to Work with Color Images for a description of how to acquire color images. Color acquisition requires a camera that supports color images.
The VisionPro Samples directory (Start->Cognex->VisionPro->VisionPro Samples) contains multiple example image-acquisition applications.
In order to acquire images using a CCD camera and frame grabber, your Visual Studio.NET application needs to accomplish the following tasks:
- Create an object reference to a frame grabber on your system.
- Select a video format for the camera connected to the frame grabber.
- Create an acquisition FIFO using the video format and frame grabber information.
- Start the acquisition by calling the frame grabber object's Acquire or StartAcquire method.
- Complete the acquisition, if necessary, and get a CogImage8Grey object that represents the acquired image.
Before your application can acquire an image, it must know the number and types of frame grabbers your system uses to digitize images. VisionPro defines a collection class, CogFrameGrabbers, that represents the frame grabbers installed on your PC. The following programming statements create a collection of frame grabbers and then declares an object variable set to the first frame grabber:
CogFrameGrabbers myFrameGrabbers = new CogFrameGrabbers(); ICogFrameGrabber myFrameGrabber = null; myFrameGrabber = myFrameGrabbers[0];
You later use this frame grabber object together with a video format to create an acquisition FIFO.
Next you must select a video format, which describes the camera connected to your frame grabber and the size of the image to acquire. The video format is specified as a string, and must be one of those returned by the FrameGrabber object's AvailableVideoFormats method. This example code declares a video format:
const string VIDEO_FORMAT = "Sony XC75 640x480";
To create a new acquisition FIFO, you need to use the CreateAcqFifo method of a FrameGrabber object, passing in the video format you want to use and an CogAcqFifoPixelFormatConstants. The following statements create an acquisition FIFO suitable for acquiring grey-scale images that are 640 pixels wide by 480 pixels high with a Sony XC-75 camera and the default FIFO pixel format:
ICogAcqFifo myFifo = null; myFifo = myFrameGrabber.CreateAcqFifo(VIDEO_FORMAT, CogAcqFifoPixelFormatConstants.Format8Grey,0,true);
Once you have created an acquisition FIFO, you can modify properties such as brightness and contrast. You can change acquisition FIFO properties at any time.
A ICogAcqFifo object has two methods that you can use to start acquiring an image:
Using Acquire places an acquisition request into a queue. If no other requests are pending, the acquisition takes place immediately. Otherwise, the acquisition request remains in the queue until it can be processed. The following programming statements use the Acquire method to return a reference to a CogImage8Grey object. The CogImage8Grey class represents an 8-bit, grey-scale image.
CogImage8Grey myImage; int trigNum; myImage = (CogImage8Grey)myFifo.Acquire(out trigNum);
The image the Acquire method returns is of type ICogImage, which needs to be cast into a CogImage8Grey image type before it can be stored in the myImage object.
The size of an acquisition FIFO is limited to 32 requests, so you must obtain images to remove them from the FIFO. If you do not, the FIFO will fill up and subsequent images will not be acquired. You can use CompleteAcquire to obtain outstanding completed images or Flush to remove outstanding requests. Use GetFifoState to monitor the number of pending acquisition requests in a FIFO.
Calling the StartAcquire method starts a new acquisition without waiting for the current acquisition to complete. You must then pass the returned value to the CompleteAcquire method to get the acquired image.
Perform the following steps to create a sample C# application in Visual Studio.NET. This application acquires an image from a camera and displays it using the Cognex Display control.
- Start Visual Studio.NET and create a new Visual C# Windows Application.
- Select Project->References and add references to the following assemblies:
- Cognex.VisionPro
- Cognex.VisionPro.Core
- Cognex.VisionPro.Display.Controls
These assemblies contain the basic classes, such as CogAcqFifo and CogImage8Grey, that you use to create VisionPro applications. They also contain classes that allow you to manipulate a frame grabber. By adding references to these assemblies to your project, you make the methods and properties of its classes available to your application.
- Enlarge the Windows form and add a CogDisplay control from the VisionPro Display Controls tab of the Toolbox. The CogDisplay control will display acquired images.
- Add the following using statement to the application:
using Cognex.VisionPro;
- Use the following statements to create an instance of a frame grabber collection and create an acquisition FIFO:
ICogAcqFifo myFifo = null; const string VIDEO_FORMAT = "Sony XC75 640x480"; ICogFrameGrabber myFrameGrabber = null; CogFrameGrabbers myFrameGrabbers = new CogFrameGrabbers(); myFrameGrabber = myFrameGrabbers[0]; myFifo = myFrameGrabber.CreateAcqFifo(VIDEO_FORMAT, Cognex.VisionPro.CogAcqFifoPixelFormatConstants.Format8Grey, 0, false);
- Add a command button to acquire an image and use the following statements in its Click event function:
int trigNum; cogDisplay1.Image = myFifo.Acquire(out trigNum);
The following is the complete Visual Studio.NET C# Windows application:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Cognex.VisionPro;
namespace ImageAcquire
{
///
/// Summary description for Form1.
///
public class Form1 : System.Windows.Forms.Form
{
private Cognex.VisionPro.Display.CogDisplay cogDisplay1;
private System.Windows.Forms.Button button1;
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
ICogAcqFifo myFifo = null;
ICogFrameGrabber myFrameGrabber = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
InitializeAcquisition();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeAcquisition()
{
const string VIDEO_FORMAT = "Sony XC75 640x480";
CogFrameGrabbers myFrameGrabbers = new CogFrameGrabbers();
myFrameGrabber = myFrameGrabbers[0];
myFifo = myFrameGrabber.CreateAcqFifo(VIDEO_FORMAT,
Cognex.VisionPro.CogAcqFifoPixelFormatConstants.Format8Grey, 0, false);
}
#region Windows Form Designer generated code
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
int trigNum;
cogDisplay1.Image = myFifo.Acquire(out trigNum);
}
}
}