visionpro准确匹配蓝框中四个圆柱,并按从左到右显示编号

#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.CalibFix;
using Cognex.VisionPro.Caliper;
#endregion

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  #endregion

  //创建一个新的数组
  ArrayList labelList = 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);
      tool.Run();
    //清除数组中的数据
    labelList.Clear();
    
    //获取到工具
    CogFindCircleTool CT1 = mToolBlock.Tools["CogFindCircleTool1"] as CogFindCircleTool;
    double CT1_X = CT1.Results.GetCircle().CenterX;
    double CT1_Y = CT1.Results.GetCircle().CenterY;
    double CT1_Z = 1.0;
            
    CogFindCircleTool CT2 = mToolBlock.Tools["CogFindCircleTool2"] as CogFindCircleTool;
    double CT2_X  = CT2.Results.GetCircle().CenterX;
    double CT2_Y = CT2.Results.GetCircle().CenterY;
    double CT2_Z = 2.0;
    
    CogFindCircleTool CT3 = mToolBlock.Tools["CogFindCircleTool3"] as CogFindCircleTool;
    double CT3_X  = CT3.Results.GetCircle().CenterX;
    double CT3_Y = CT3.Results.GetCircle().CenterY;
    double CT3_Z = 3.0;
    
    CogFindCircleTool CT4 = mToolBlock.Tools["CogFindCircleTool4"] as CogFindCircleTool;
    double CT4_X  = CT4.Results.GetCircle().CenterX;
    double CT4_Y = CT4.Results.GetCircle().CenterY;
    double CT4_Z = 4.0;
    
    //给数组中添加数值
     labelList.Add(setLabel(CT1_X, CT1_Y, Convert.ToString(CT1_Z), CogColorConstants.Red));
     labelList.Add(setLabel(CT2_X, CT2_Y, Convert.ToString(CT2_Z), CogColorConstants.Red));
     labelList.Add(setLabel(CT3_X, CT3_Y, Convert.ToString(CT3_Z), CogColorConstants.Red));
     labelList.Add(setLabel(CT4_X, CT4_Y, Convert.ToString(CT4_Z), CogColorConstants.Red));
   // if  CT.Results.GetCircle().CenterX = null
     

    
//CogGraphicLebel 
    
    return false;
  }
  
  //创建一个新方法
 
  CogGraphicLabel setLabel(double x, double y, string mValue, CogColorConstants color)
  {
    CogGraphicLabel label = new CogGraphicLabel();
    label.SetXYText(x, y, mValue);
    label.Alignment = CogGraphicLabelAlignmentConstants.BaselineLeft;
    label.Color = color;
    label.SelectedSpaceName = "#";
    label.Font = new Font("宋体", 12);
    return label;
  }

  #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 g in labelList)
    {
      mToolBlock.AddGraphicToRunRecord(g, lastRecord, lastRecord.SubRecords[1].RecordKey, "");
    }
  }
  #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)准确匹配蓝框中四个圆柱,并按从左到右显示编号

2将得到的圆柱结果连点拟合成线L1

3)准确抓取三条小线段,拟合直线L2

上面是代码的内容,下面是实现的效果。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在VB显示VisionPro图像并显示框线,您可以使用Cognex.VisionPro.Display.CogDisplay控件和Cognex.VisionPro.ToolGroup.CogToolGroup类来实现。 以下是一个示例代码,演示如何在VB显示VisionPro图像并显示框线: ```vb Imports Cognex.VisionPro Imports Cognex.VisionPro.ToolGroup Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load ' 创建CogDisplay控件并设置其大小和位置 Dim cogDisplay As New Cognex.VisionPro.Display.CogDisplay() cogDisplay.Size = New Size(800, 600) cogDisplay.Location = New Point(10, 10) ' 创建CogToolGroup对象并加载VisionPro工具配置文件 Dim cogToolGroup As New CogToolGroup() cogToolGroup.LoadTool("path_to_your_tool_file.vpp") ' 创建CogImage对象并加载图像文件 Dim cogImage As New CogImageFile() cogImage.Open("path_to_your_image.bmp") ' 在CogImage上运行CogToolGroup以进行图像处理 cogToolGroup.Inputs("InputImage").Value = cogImage cogToolGroup.Run() ' 在CogDisplay上显示处理后的图像和框线 cogDisplay.Image = cogToolGroup.Outputs("OutputImage").Value cogDisplay.StaticGraphics.Add(cogToolGroup.CreateLastRunRecord().SubRecords.Item("YourRegionName").CreateGraphics()) ' 将CogDisplay控件添加到窗体 Me.Controls.Add(cogDisplay) End Sub End Class ``` 请注意,您需要将代码的"path_to_your_tool_file.vpp"替换为您实际的VisionPro工具配置文件路径,将"path_to_your_image.bmp"替换为您实际的图像文件路径,并将"YourRegionName"替换为实际的区域名称。此示例代码将在窗体加载时显示VisionPro图像,并在CogDisplay上显示经过处理的图像和框线。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

高高呀~

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值