VisionPro学习日志(三)
VisionPro学习日志(三)
(1)脚本在哪写
添加脚本的方式,使用VB或者C#
创建脚本
还能进入ToolGroup控件内进行脚本的编写
注意,当有控件移除或者添加,那么需要使用添加或者移除引用集
(2)实例
案例1 统计图片中大米,红豆和花生的个数
实现结果:
方法:
- 使用Blob工具进行斑点提取,然后修改阈值范围,让物料都处于斑点状态(不要丢失物料的形状)
- 编写脚本对于每个斑点的面积进行判断,得到当前斑点的类型,然后进行统计
Blob对物料进行二值化
-
先观察灰度直方图的前景和对象的大致分割范围
-
然后微调阈值,让图像进行分割
-
适当使用形态学进行修饰
脚本编写思路:
- 获取blob工具中的所有结果
- 给每个结果使用label标注当前种类
- 最后统计每个种类的个数,并且显示出来
#region namespace imports
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Cognex.VisionPro;
using Cognex.VisionPro.ToolBlock;
using Cognex.VisionPro3D;
using Cognex.VisionPro.Blob;
#endregion
public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
private CogGraphicLabel tLabel ;
private CogBlobTool mBlob;
private List<CogGraphicLabel> mLabels;
#endregion
#region Used Variables
private int RiceMinSize = 2000;
private int RiceMaxSize = 19000;
private int BeanMinSize = 23000;
private int BeanMaxSize = 40000;
private int NutMinSize = 70000;
private int NutMaxSize = 110000;
private int NutCnt = 0;
private int BeanCnt = 0;
private int RiceCnt = 0;
#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)
{
NutCnt = 0;
BeanCnt = 0;
RiceCnt = 0;
// 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
mLabels.Clear();
mToolBlock.RunTool(mBlob, ref message, ref result);
CogBlobResultCollection blobs = mBlob.Results.GetBlobs();
foreach(CogBlobResult blob in blobs)
{
CogGraphicLabel tempLabel = new CogGraphicLabel();
tempLabel.Font = new Font("Arial", 15);
tempLabel.Alignment = CogGraphicLabelAlignmentConstants.BaselineCenter;
double areaData = blob.Area;
string mes_data = "";
switch (GetTypeByAreaData(areaData))
{
case 0:mes_data = "大米";break;
case 1:mes_data = "红豆";break;
case 2:mes_data = "花生";break;
default:mes_data="UnKnown";break;
}
tempLabel.SetXYText(blob.CenterOfMassX, blob.CenterOfMassY, mes_data);
tempLabel.Color = CogColorConstants.Red;
mLabels.Add(tempLabel);
}
string Rice_mes = "大米 :" + RiceCnt.ToString();
string Bean_mes = "红豆 :" + BeanCnt.ToString();
string nut_mes = "花生 :" + NutCnt.ToString();
string lResult = Rice_mes + " " + Bean_mes + " " + nut_mes;
tLabel.Font = new Font("Arial", 15);
tLabel.SetXYText(300, 100, lResult);
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(CogGraphicLabel label in mLabels)
{
mToolBlock.AddGraphicToRunRecord(label, lastRecord, "CogBlobTool1.InputImage", " ");
}
mToolBlock.AddGraphicToRunRecord(tLabel, lastRecord, "CogBlobTool1.InputImage", " ");
}
#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));
mBlob = this.mToolBlock.Tools["CogBlobTool1"] as CogBlobTool;
mLabels = new List<CogGraphicLabel>();
tLabel = new CogGraphicLabel();
}
#endregion
#region Distinguish Rice Bean Nut by Data Area
/// <summary>
///
/// </summary>
/// <param name="area"></param>
/// <returns>0 Rice 1 Bean 2 Nut; -1 error</returns>
public int GetTypeByAreaData(double area)
{
if (area> RiceMinSize && area<RiceMaxSize)
{
RiceCnt+=1;
return 0;
}
if (area > BeanMinSize && area < BeanMaxSize)
{
BeanCnt+=1;
return 1;
}
if (area > NutMinSize && area < NutMaxSize)
{
NutCnt+=1;
return 2;
}
return -1;
}
#endregion
}
最开始用了个Region进行变量的定义。
Initialize 中进行变量的新建
然后在GroupRun中编写主要的逻辑。