using System;
using Cognex.VisionPro;
using Cognex.VisionPro.Display;
using Cognex.VisionPro.ToolBlock;
using System.Windows.Forms;
using System.Collections.Generic;
using Cognex.VisionPro.ImageFile;
using Cognex.VisionPro.ImageProcessing;
using System.Drawing;
namespace testOpenFifo
{
public partial class Form1 : Form
{
private static CogToolBlock CogToolBlock_1 = new CogToolBlock();
private static CogToolDisplay CogToolDisplay_1 = new CogToolDisplay();
private static CogToolDisplay CogToolDisplay_Image = new CogToolDisplay();
private static string strVppPath_1 = string.Format(@"C:\Users\DELL\Documents\WXWork\1688850478980082\Cache\File\2021-09\TriggerTest\TriggerTest\bin\x64\Debug\File\VisionProVpp\testVpp_v1.vpp");
//CogAcqFifoTool:Tool which acquires images using an acquisition fifo on a frame grabber.
private static CogAcqFifoTool acqFifoTool = new CogAcqFifoTool();
public string m_iVideoFormats = null;//视频格式
private static List<CogImage8Grey> imgList = new List<CogImage8Grey>();
private static List<CogImage8Grey> imageListOutPut = null;
public CogAcqInfo info = new CogAcqInfo();
//Image is not flipped or rotated,default value:None(Image is not flipped or rotated.)
CogIPOneImageFlipRotateOperationConstants m_enRotateType = CogIPOneImageFlipRotateOperationConstants.None;
public CogImage8Grey OneShotImage, image;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//CogToolDisplay:如何连接到Vision Pro工具以及如何调用工具所有的执行结果,
//它通过显示区上放一个下拉框允许用户选择浏览哪张图像(就像QB里面的显示结果控件一样)
CogToolDisplay_1 = cogToolDisplay1;
CogToolDisplay_Image = cogToolDisplayImage;
}
//使用.VPP+硬件触发
private void btTrigger_Click(object sender, EventArgs e)
{
CogToolBlock_1 = (CogToolBlock)CogSerializer.LoadObjectFromFile(strVppPath_1);
listBox1.Items.Add("VPP加载完成");
TriggerFifoClass triggerFifo = new TriggerFifoClass((CogAcqFifoTool)CogToolBlock_1.Tools[0], CogToolDisplay_1);
}
//通过配置参数,触发相机
private void btStartImage_Click(object sender, EventArgs e)
{
//ICogAcqFifo:cogAcqFifoCtlV21
acqFifoTool.Operator = cogAcqFifoCtlV21.Subject;
//此时,已经获得CogAcqFifoGigE,即获得相机,接下来可以获得相机的参数。
//选择的视频格式
//Mono8 8grey
//Mono10 16grey
m_iVideoFormats = acqFifoTool.Operator.VideoFormat;
//获得相机的各种参数
//点击初始化取相后,相机初始化
//acqFifoTool.Run();//使用目前的参数设置,运行相机工具
//StartLiveDisplay():Starts live image display using the specified acquisition FIFO.
//If own is true, StartLiveDisplay(Object, Boolean) optimizes display performance,
//but you should not attempt to use acqFifo to acquire images while live video is running.
//If acqFifo's TriggerModel is Auto, own must be true.
//See Acquiring from a Live Display to learn how to acquire images while displaying live video.
//想显示图像
CogToolDisplay_Image.Display.StartLiveDisplay(acqFifoTool.Operator, false);
}
//停止触发相机
private void btStopTrigger_Click(object sender, EventArgs e)
{
CogToolDisplay_Image.Display.StopLiveDisplay();
}
private bool FindCCDAndInitial()
{
acqFifoTool.Operator.TimeoutEnabled = false;
//拍照完成触发事件
acqFifoTool.Run();
//显示图片
if (CogToolDisplay_1!= null)
{
//获得相机捕获的一张图片
CogToolDisplay_1.Display.Image = acqFifoTool.OutputImage;
}
return true;
}
//捕获相机的图片
private void btGetImage_Click(object sender, EventArgs e)
{
FindCCDAndInitial();
}
}
}
其中,若是需要加载visionPro的.VPP文件,需要对其进行处理,添加处理类TriggerFifoClass.cs:
using System;
using Cognex.VisionPro;
using Cognex.VisionPro.ImageProcessing;
using System.Drawing;
using System.Collections.Generic;
namespace testOpenFifo
{
class TriggerFifoClass
{
public CogToolDisplay pDisplay;//显示图片以及图像
public int m_iVideoFormats = 0;//视频格式
public CogAcqFifoTool AcqFifoTool;//FifoTool
public CogImage8Grey OneShotImage, image;
public CogAcqInfo info = new CogAcqInfo();
//Image is not flipped or rotated,default value:None(Image is not flipped or rotated.)
CogIPOneImageFlipRotateOperationConstants m_enRotateType = CogIPOneImageFlipRotateOperationConstants.None;
public TriggerFifoClass(CogAcqFifoTool CFT, CogToolDisplay CTD)
{
pDisplay = CTD;
AcqFifoTool = CFT;
FindCCDAndInitial();
}
private bool FindCCDAndInitial()
{
AcqFifoTool.Operator.TimeoutEnabled = false;
//拍照完成触发事件
AcqFifoTool.Operator.Complete += new CogCompleteEventHandler(Acquisition_Complete);
return true;
}
/* void CogCompleteEventHandler(Object sender,CogCompleteEventArgs e){};
* Represents the method that will handle the Complete event of an ICogAcqFifo.
* The method must have the same parameters as this delegate.
*
*/
public void Acquisition_Complete(object sender, CogCompleteEventArgs e)
{
//vpp运行过程
/*numPending:
Type: System..::..Int32
* The number of acquisitions in the pending state. This is the number of acquisitions requested by StartAcquire()()()() for which acquisition has not started. *
*To achieve frame rate acquisition in manual trigger mode, the FIFO must always have one or more pending acquisitions.
*
* numReady
Type: System..::..Int32
The number of acquisition requests ready to be completed.
busy
Type: System..::..Boolean
* True if the oldest outstanding acquisition is waiting for a trigger signal or is acquiring an image. For master/slave acquisitions, it becomes true only after the master and all slaves are ready for acquisition.
*/
int Acq_numPending = 0;
int Acq_numReady = 0;
bool Acq_busy = false;
ICogAcqFifo AcqCapture = (ICogAcqFifo)sender;
//得到CCD狀態,Returns the instantaneous state of the fifo.
AcqCapture.GetFifoState(out Acq_numPending, out Acq_numReady, out Acq_busy);
if(Acq_numReady>0)
{
/*
* Completes the acquisition specified by the requested ticke and returns the acquired image.
* This method is the same as CompleteAcquire(Int32, Int32, Int32) but passes and returns its results in an ICogAcqInfo.
*
* */
image = (CogImage8Grey)AcqCapture.CompleteAcquireEx(info);//捕获到图像
//----------------------
#region
//CogIPOneImageTool:Tool that operates on a single input image to produce a single output image
CogIPOneImageTool pOneImage = new CogIPOneImageTool();
//CogIPOneImageFlipRotate:Class that flips and/or applies rotation to an image.
CogIPOneImageFlipRotate ipFlipRotat = new CogIPOneImageFlipRotate();
//The operation used to re - orient the image.
ipFlipRotat.OperationInPixelSpace = m_enRotateType;
//拍照获取图像使用的是取像工具的属性Operator,它是ICogAcqFifo类型。
/*
* Operater属性:
* Returns the collection of ICogIPOneImageOperators.
* When the tool is run, each operator in the collection will be executed sequentially with the output of the first passed on to the second, and so on.
*/
pOneImage.Operators.Add(ipFlipRotat);
pOneImage.InputImage = image;
pOneImage.Run();
Bitmap bitmap = pOneImage.OutputImage.ToBitmap();
CogImage8Grey pimage = new CogImage8Grey(bitmap);
#endregion
OneShotImage = pimage;
///-----------
if (pDisplay != null)
{
pDisplay.Display.Image = OneShotImage;
}
///-----------
}
}
}
}
运行结果(注意:需要先连接工业相机):