找一个最简单的例子用来简单说明一下这种类型的脚本怎么写。我们从头开始:
先导入图像:
我找了一个基本上大家有Visionpro的都会有这张图像。
因为我们主要是说轮廓边缘和最大外接矩形,所以这里我们参数不做详细的介绍,我是这样调整的参数。
我们把斑点过滤掉:
然后打开脚本:
关键的设置
我们主要来说一下获取最大外接矩形。获取Blob对象的最大外接矩形是使用一个GetBoundingBox();的方法,是用来获取边缘,Blob对象可以直接调用。下面是最关键的地方,大家思考一个问题,这个Blob对象我通过什么方式获取呢???
脚本里这里有两种获取对象的方式,第一种在60行,我们先说第一种。
// rect = Blob1.Results.GetBlobByID(i).GetBoundingBox(CogBlobAxisConstants.ExtremaAngle);
第一种是通过Blobs.Results.GetBlobByID(i)的方式获取Blob对象的,这种方式是根据Blob在我们进行调整参数之后,斑点过滤之前的Blob对象,我们可以理解为,参数调整完之后,所有Blob对象都有了ID,这个ID不会变了,我们想再进行斑点筛选,结果里显示的是筛选之后的Blob对象,ID号还是原来的ID,不会将筛选完的Blob对象再重新分配ID。但是通过这个GetBlobByID(i)方法是通过ID号获得对象的,获取的不是我们筛选后的对象。
例如:我们有 ID号为0,1,2,3,4,5的六个Blob对象,假设过滤了斑点1和2,筛选过后剩了0,3,4,5四个Blob对象,接下来应该是要对筛选后的四个对象进行处理,但是GetBlobByID(1)获取的仍然是ID号为1的Blob对象,这种获取方式显然不是我们想要的。
第一种获取方式的结果:只按照ID号进行获取
下面我们说第二种获取方式:
rect = Blob1.Results.GetBlobs()[i].GetBoundingBox(CogBlobAxisConstants.ExtremaAngle);
这种获取方式就可以获取我们进行筛选之后的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 System.Collections.Generic;
#endregion
public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
// private Cognex.VisionPro.Blob.CogBlobTool Blob1;//定义Blob工具
private List<CogGraphicLabel> mLabels = new List<CogGraphicLabel>();
CogGraphicCollection gc = new CogGraphicCollection(); //定义图形标签工具
#endregion
/// <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
gc.Clear();
mLabels.Clear();
CogBlobTool Blob1 = mToolBlock.Tools["CogBlobTool1"] as CogBlobTool;
CogGraphicLabel myLabel = new CogGraphicLabel();
// Run each tool using the RunTool function
foreach(ICogTool tool in mToolBlock.Tools)
mToolBlock.RunTool(tool, ref message, ref result);
Blob1.Run();
for(int i = 0;i < Blob1.Results.GetBlobs().Count ;i++)
{//思路:把blob对象的边缘给拿出来赋值给多边形,然后把多边形添加到图形标签里就能显示出来了。
CogPolygon Polygon1 = new CogPolygon();//定义多边形
CogRectangleAffine rect = new CogRectangleAffine();//定义一个仿射矩形
Polygon1 = Blob1.Results.GetBlobs()[i].GetBoundary();//把blob对象的边缘给拿出来赋值给多边形
Polygon1.Color = CogColorConstants.Red;//设置多边形的颜色
Polygon1.LineStyle = CogGraphicLineStyleConstants.Solid;//线风格
Polygon1.LineWidthInScreenPixels = 3;//线宽
gc.Add(Polygon1);//添加到图形标签中去Blobl
//rect = Blob1.Results.GetBlobByID(i).GetBoundingBox(CogBlobAxisConstants.ExtremaAngle);
rect = Blob1.Results.GetBlobs()[i].GetBoundingBox(CogBlobAxisConstants.ExtremaAngle);
//这个不好找,添加终端里边也没有这个,所以需要记录一下。
//意思就是把对象的限定框赋值给我们定义的仿射矩形,目的就是把这个值拿出来,只有把值拿出来我们才能进行后续操作。
CogGraphicLabel gc1 = new CogGraphicLabel();
gc1.SetXYText(rect.CenterX, rect.CenterY, i.ToString() + "(" + rect.CenterX + "," + rect.CenterY + ")");
gc1.Color = CogColorConstants.DarkGreen;
gc1.Font = new Font("Arial", 6);
mLabels.Add(gc1);
rect.Color = CogColorConstants.Blue;//设置仿射矩形颜色
rect.LineStyle = CogGraphicLineStyleConstants.Solid;//设置仿射矩形线风格
rect.LineWidthInScreenPixels = 3;//设置仿射矩形线宽
gc.Add(rect);//添加到图像标签中
}
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 c in gc){
mToolBlock.AddGraphicToRunRecord(c, lastRecord, "CogBlobTool1.BlobImage", "");
}
foreach (CogGraphicLabel label in mLabels)
{
mToolBlock.AddGraphicToRunRecord(label, lastRecord, "CogBlobTool1.BlobImage", "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
}
,一起进步。