visionPro高级应用理解
高级应用理解
小提示:
进行模板匹配的时候坚持三个原则:最稳定特征(保证抓取稳定),三足鼎立(选取三个角),选取最少的部分(降低模板匹配的耗时时间)
案例1:现在用两个直线拟合一条直线
现在的错误做法:从每条直线里面提取第一个点作为拟合直线的输入点。
正确做法:将每一个拟合点加入FitLine工具的使用点里面
public override bool GroupRun(ref string message, ref CogToolResultConstants result)
{
CogFitLineTool FitLinel =mToolBlock.Tools["CogFitLineTooll"]as CogFitLineTool;
while(FitLinel.RunParams.NumPoints > 0)
{
FitLinel.RunParams.DeletePoint(0);
}
if(FitLinel.RunParams.NumPoints == 0)
{
foreach(ICogTool tool in mToolBlock.Tools)
{
tool.Run();
if (tool.Name.Contains("CogFindLineTool"))
{
CogFindLineTool FindLine = tool as CogFindLineTool;
for(int i = 0;i < FindLine.Results.NumPointsUsed;i++)
FitLinel.RunParams.AddPoint(FindLine.Results[i].X, FindLine.Results[i].Y);
}
}
}
return false;
}
案例2:Blob+卡尺遍历
#region namespace imports
using System;
using System.Collections;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Cognex.VisionPro;
using Cognex.VisionPro.ToolBlock;
using Cognex.VisionPro3D;
using Cognex.VisionPro.Blob;
using Cognex.VisionPro.Caliper;
#endregion
public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
#endregion
ArrayList list =new ArrayList();
/// <summary>
/// Called when the parent tool is run.
/// Add code here to customize or replace the normal run behavior.
/// </summary>
/// <param name="message">Sets the Message in the tool's RunStatus.</param>
/// <param name="result">Sets the Result in the tool's RunStatus</param>
/// <returns>True if the tool should run normally,
/// False if GroupRun customizes run behavior</returns>
public override bool GroupRun(ref string message, ref CogToolResultConstants result)
{
// To let the execution stop in this script when a debugger is attached, uncomment the following lines.
// #if DEBUG
if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break();
// #endif
///Run each tool using the RunTool function
///foreach(ICogTool tool in mToolBlock.Tools)
///mToolBlock.RunTool(tool,ref message,ref result);
try
{
list.Clear();
CogBlobTool Blobl = mToolBlock.Tools["CogBlobTool1"]as CogBlobTool;
CogCaliperTool Caliperl = mToolBlock.Tools["CogCaliperTool1"]as CogCaliperTool;
CogRectangleAffine Rectangle = (CogRectangleAffine) Caliperl.Region;
foreach(ICogTool tool in mToolBlock.Tools)
Blobl.Run();
foreach(CogBlobResult BlobResult in Blobl.Results.GetBlobs())
{
Rectangle.CenterX = BlobResult.CenterOfMassX;
Rectangle.CenterY = BlobResult.CenterOfMassY;
Caliperl.Run();
if(Caliperl.Results.Count != 0)
{
CogGraphicLabel Label = new CogGraphicLabel();
Label.Color = CogColorConstants.Red;
Label.Font = new Font("楷体",12);
Label.SetXYText(BlobResult.CenterOfMassX, BlobResult.CenterOfMassY, "长度: " + Caliperl.Results[0].Width.ToString("f2"));
list.Add(Label);
}
else
{
CogGraphicLabel Label =new CogGraphicLabel();
Label.Color = CogColorConstants.Red;
Label.Font = new Font("楷体",12);
Label.SetXYText(200, 10, "未找到边缘");
list.Add(Label);
}
}
}
catch (Exception ex)
{
CogGraphicLabel Label = new CogGraphicLabel();
Label.Color = CogColorConstants.Red;
Label.Font = new Font("楷体",12);
Label.SetXYText(200, 10, "未找到边缘");
list.Add(Label);
}
return false;
}
#region When the Current Run Record is Created
/// <summary>
/// Called when the current record may have changed and is being reconstructed
/// </summary>
/// <param name="currentRecord">
/// The new currentRecord is available to be initialized or customized.</param>
public override void ModifyCurrentRunRecord(Cognex.VisionPro.ICogRecord currentRecord)
{
}
#endregion
#region When the Last Run Record is Created
/// <summary>
/// Called when the last run record may have changed and is being reconstructed
/// </summary>
/// <param name="lastRecord">
/// The new last run record is available to be initialized or customized.</param>
public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
{
foreach (ICogGraphic graphic in list)
{
mToolBlock.AddGraphicToRunRecord(graphic, lastRecord, "CogIPOneImageTool1.OutputImage", "script");
}
}
#endregion
#region When the Script is Initialized
/// <summary>
/// Perform any initialization required by your script here
/// </summary>
/// <param name="host">The host tool</param>
public override void Initialize(Cognex.VisionPro.ToolGroup.CogToolGroup host)
{
// DO NOT REMOVE - Call the base class implementation first - DO NOT REMOVE
base.Initialize(host);
// Store a local copy of the script host
this.mToolBlock = ((Cognex.VisionPro.ToolBlock.CogToolBlock)(host));
}
#endregion
}
这个原理说一下:将Blob区域作为仿射矩形。然后使用Blob工具获取到仿射矩形,然后使用仿射矩形的中心作为卡尺工具的区域中心。
案例3:模板匹配+卡尺遍历
图像效果
#region namespace imports
using System;
using System.Collections;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Cognex.VisionPro;
using Cognex.VisionPro.ToolBlock;
using Cognex.VisionPro3D;
using Cognex.VisionPro.ImageProcessing;
using Cognex.VisionPro.PMAlign;
using Cognex.VisionPro.Caliper;
#endregion
public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
#endregion
ArrayList ListLabel = new ArrayList();
/// <summary>
/// Called when the parent tool is run.
/// Add code here to customize or replace the normal run behavior.
/// </summary>
/// <param name="message">Sets the Message in the tool's RunStatus.</param>
/// <param name="result">Sets the Result in the tool's RunStatus</param>
/// <returns>True if the tool should run normally,
/// False if GroupRun customizes run behavior</returns>
public override bool GroupRun(ref string message, ref CogToolResultConstants result)
{
// To let the execution stop in this script when a debugger is attached, uncomment the following lines.
// #if DEBUG
if (System.Diagnostics.Debugger.IsAttached) System.Diagnostics.Debugger.Break();
// #endif
try
{
ListLabel.Clear();
CogPMAlignTool PMAlignl = mToolBlock.Tools["CogPMAlignTool1"]as CogPMAlignTool;
CogCaliperTool Caliperl = mToolBlock.Tools["CogCaliperTool1"]as CogCaliperTool;
CogCaliperTool Caliper2 = mToolBlock.Tools["CogCaliperTool2"]as CogCaliperTool;
CogRectangleAffine Rectangle1 = (CogRectangleAffine) Caliperl.Region;
CogRectangleAffine Rectangle2 = (CogRectangleAffine) Caliper2.Region;
PMAlignl.Run();
for(int i = 0;i < PMAlignl.Results.Count;i++)
{
Rectangle1.CenterX = PMAlignl.Results[i].GetPose().TranslationX;
Rectangle1.CenterY = PMAlignl.Results[i].GetPose().TranslationY;
Rectangle1.Rotation = PMAlignl.Results[i].GetPose().Rotation;
Rectangle2.CenterX = PMAlignl.Results[i].GetPose().TranslationX;
Rectangle2.CenterY = PMAlignl.Results[i].GetPose().TranslationY;
Rectangle2.Rotation = PMAlignl.Results[i].GetPose().Rotation;
Caliperl.Run();
Caliper2.Run();
if(Caliperl.Results.Count > 0)
{
CogGraphicLabel Label = new CogGraphicLabel();
Label.Color = CogColorConstants.Blue;
Label.Font = new Font("楷体", 12);
Label.SetXYText(PMAlignl.Results[i].GetPose().TranslationX+50, PMAlignl.Results[i].GetPose().TranslationY+25, Caliperl.Results[0].Width.ToString("f2"));
ListLabel.Add(Label);
}
else
{
CogGraphicLabel Label = new CogGraphicLabel();
Label.Color = CogColorConstants.Blue;
Label.Font = new Font("楷体", 12);
Label.SetXYText(100, 100, "异常");
ListLabel.Add(Label);
}
if(Caliper2.Results.Count > 0)
{
CogGraphicLabel Label = new CogGraphicLabel();
Label.Color = CogColorConstants.Cyan;
Label.Font = new Font("楷体", 12);
Label.SetXYText(PMAlignl.Results[i].GetPose().TranslationX-50, PMAlignl.Results[i].GetPose().TranslationY-25, Caliper2.Results[0].Width.ToString("f2"));
ListLabel.Add(Label);
}
else
{
CogGraphicLabel Label = new CogGraphicLabel();
Label.Color = CogColorConstants.Blue;
Label.Font = new Font("楷体", 12);
Label.SetXYText(100, 100, "异常");
ListLabel.Add(Label);
}
}
}
catch (Exception ex)
{
CogGraphicLabel Label = new CogGraphicLabel();
Label.Color = CogColorConstants.Red;
Label.Font = new Font("楷体", 12);
Label.SetXYText(200, 10, "未找到边缘");
ListLabel.Add(Label);
}
return false;
}
#region When the Current Run Record is Created
/// <summary>
/// Called when the current record may have changed and is being reconstructed
/// </summary>
/// <param name="currentRecord">
/// The new currentRecord is available to be initialized or customized.</param>
public override void ModifyCurrentRunRecord(Cognex.VisionPro.ICogRecord currentRecord)
{
}
#endregion
#region When the Last Run Record is Created
/// <summary>
/// Called when the last run record may have changed and is being reconstructed
/// </summary>
/// <param name="lastRecord">
/// The new last run record is available to be initialized or customized.</param>
public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
{
foreach (ICogGraphic graphic in ListLabel)
{
mToolBlock.AddGraphicToRunRecord(graphic, lastRecord, "CogIPOneImageTool1.OutputImage", "script");
}
}
#endregion
#region When the Script is Initialized
/// <summary>
/// Perform any initialization required by your script here
/// </summary>
/// <param name="host">The host tool</param>
public override void Initialize(Cognex.VisionPro.ToolGroup.CogToolGroup host)
{
// DO NOT REMOVE - Call the base class implementation first - DO NOT REMOVE
base.Initialize(host);
// Store a local copy of the script host
this.mToolBlock = ((Cognex.VisionPro.ToolBlock.CogToolBlock)(host));
}
#endregion
}
案例4:PMA粗定位+几何精定位
案例5:图像处理+ PMA精定位
进行医疗瓶盖读码操作
Q:为啥要进行图像处理?
A:我们其实是希望,使用图像预处理的膨胀操作,然后将其变成一大坨。然后使用Blob进行定位,然后最后进行读码操作
案例6:图像处理+ PMA粗定位+Blob精定位
大致说明下:
PMA粗定位用来定位瓶盖,然后Blob用来定位读码区域(这里是用两个Blob定位两行编码的区域,和上个案例的PMA工具基本相同)