VisionPro的高级脚本编写2

高级脚本代码段:

#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.Blob;
using Cognex.VisionPro.Caliper;
using System.Collections.Generic; //添加泛型
#endregion

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  
  //
  List<CogGraphicLabel> labels = new List<CogGraphicLabel>();

  #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


    // Run each tool using the RunTool function
    foreach(ICogTool tool in mToolBlock.Tools)
      mToolBlock.RunTool(tool, ref message, ref result);
    
    //获取斑点信息
    CogBlobTool blobTool =  mToolBlock.Tools["CogBlobTool1"] as CogBlobTool;
    //斑点的子集合
    CogBlobResultCollection blobResult  = blobTool.Results.GetBlobs() as CogBlobResultCollection;
    //获取卡尺工具
    CogCaliperTool CaliperTool = mToolBlock.Tools["CogCaliperTool1"] as CogCaliperTool;
    //获取卡尺子集中的中心点
    CogRectangleAffine Affine = CaliperTool.Region as CogRectangleAffine;//放射矩形 卡尺的区域
    
    
    labels.Clear();
    foreach(CogBlobResult blob in blobResult)
    {
      Affine.CenterX = blob.CenterOfMassX;
      Affine.CenterY = blob.CenterOfMassY;
      
      //运行卡尺
      mToolBlock.RunTool(CaliperTool, ref message, ref result);
      CogGraphicLabel mylabel = new CogGraphicLabel();
      if(CaliperTool.Results.Count > 0)
      {
        mylabel.SetXYText(blob.CenterOfMassX, blob.CenterOfMassY, "合格品");
        mylabel.Color = CogColorConstants.Green;
      }
      else{
        mylabel.SetXYText(blob.CenterOfMassX, blob.CenterOfMassY, "不合格品");
        mylabel.Color = CogColorConstants.Red;
      }
      labels.Add(mylabel);
    }

    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 x in labels)
    {
     mToolBlock.AddGraphicToRunRecord(x,lastRecord,"CogImageConvertTool1.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));
  }
  #endregion

}

 

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
VisionPro高级脚本是一种功能强大的图像处理工具,可以帮助用户实现复杂的图像分析和处理任务。下面是一个关于VisionPro高级脚本的实例。 假设我们有一组产品图像,我们需要通过图像处理来检测其中的缺陷并进行分类。我们可以使用VisionPro高级脚本来实现这个任务。 首先,我们需要将图像加载到VisionPro软件中。可以通过编写脚本来实现图像加载。例如,我们可以使用“LoadImage”函数来加载图像。 然后,我们可以使用VisionPro中提供的各种图像处理算法来对加载的图像进行处理和分析。例如,我们可以使用“Threshold”函数来进行图像阈值化处理,将图像中的特定区域进行二值化操作。 接下来,我们可以使用“BlobAnalysis”函数来检测图像中的缺陷。该函数可以用于分析图像中的连通区域,并提取特定的属性,如位置、大小和形状等。 然后,我们可以使用“ClassificationTrain”函数来对缺陷进行分类。该函数可以用于训练分类器,根据所提取的缺陷属性来判断缺陷的类型。 最后,我们可以使用“OutputWindow”函数来将结果显示在输出窗口中。这样我们就可以通过VisionPro高级脚本输出图像处理和分析的结果,同时也可以保存结果供以后查看和分析。 以上是一个关于VisionPro高级脚本输出实例的简要说明。通过编写脚本,我们可以实现复杂的图像处理和分析任务,并高效地输出结果。VisionPro高级脚本提供了丰富的功能和算法,可以满足不同领域的图像处理需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

芝士是只猫

开源使得世界变得更美丽

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值