首页 > 其他分享 >VisionPro学习日志(五)

VisionPro学习日志(五)

时间:2023-08-21 09:12:42浏览次数:36  
标签:VisionPro tool System 学习 using 日志 卡尺 Cognex

VisionPro学习日志(五)

案例1:多目标检测(动态使用卡尺)

检测流程:首先使用斑点工具,显示出每一个白色矩形框,然后使用卡尺工具测量每一个黑框的高度。

image-20230809092904017

image-20230809093040671

#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;
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);
    
    
    //2.获取斑点信息 
    CogBlobTool blobTool = mToolBlock.Tools["CogBlobTool1"]  as  CogBlobTool;
    //斑点集合
    CogBlobResultCollection blobResults = blobTool.Results.GetBlobs()  as CogBlobResultCollection; //斑点的集合
    //获取卡尺工具
    
    CogCaliperTool caliperTool = mToolBlock.Tools["CogCaliperTool1"]  as  CogCaliperTool;  //获取卡尺工具
    //设置卡尺的中心点 之前需要获取区域信息 
    
    CogRectangleAffine caliperRegion = caliperTool.Region  as CogRectangleAffine; //仿射矩形  caliperRegion 卡尺的区域
    
    labels.Clear();//清空列表
    
    foreach(CogBlobResult blob in  blobResults)
    {
      caliperRegion.CenterX = blob.CenterOfMassX;      //斑点的质心
      caliperRegion.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, "Good");
        myLabel.Color = CogColorConstants.Green;
      }
      else
      {
        myLabel.SetXYText(blob.CenterOfMassX, blob.CenterOfMassY, "Bad");
        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 label  in  labels )
    {
      mToolBlock.AddGraphicToRunRecord(label, 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));
  }
  #endregion

}

案例2:圆形瓶口缺陷检测

image-20230812115943628

最后结果不满意:无法精确到分辨缺陷。对于缺陷的尺寸没法正确标注。

image-20230812120255889

案例3:检测球形轴承的安装错误(重新复盘学习,后续会补充)

使用原图的模板,会出现无法识别到实例的情况。

解决思路:对原图进行预处理,然后降噪。让原图中的精细特征变多(有点类似于Halcon重新处理模板的图像,让模板的像素特征更加精确)。

案例4:圆形文字识别

image-20230812121428441

image-20230812121456279

原先可能会出现沿着水平轴展开是相反的情况

修改起始角度即可:

image-20230812121539828

标签:VisionPro,tool,System,学习,using,日志,卡尺,Cognex
From: https://www.cnblogs.com/LtWf/p/17645095.html

相关文章

  • 学习记录
    1.将float型赋值给整型 比如floatf=3.14inti=fi最后等于3。这个转换是由编译器完成的,当把float型的数据赋值给int型数据时,编译器会自动截断小数点后的数,把整数部分赋给int类型的变量由此可推断出四舍五入的实现方法:inta=b+0.5;2.for循环的低级错误:for(j=5......
  • VisionPro学习日志(二)
    VisionPro学习日志(二)几何工具(1)Creation工具Circle创建圆Ellipse创建椭圆Label创建标签Text可以直接输出指定的字符串Double的输入终端,只能链接前一阶段的输出终端为double。同时,需要链接图像例如:输出测量结果CreateLineBisectPointsTool中垂线CreateLinePa......
  • VisionPro学习日志(三)
    VisionPro学习日志(三)(1)脚本在哪写添加脚本的方式,使用VB或者C#创建脚本还能进入ToolGroup控件内进行脚本的编写注意,当有控件移除或者添加,那么需要使用添加或者移除引用集(2)实例案例1统计图片中大米,红豆和花生的个数实现结果:方法:使用Blob工具进行斑点提取,然后修......
  • VisionPro学习日志(四) 预处理工具
    VisionPro学习日志(四)预处理工具CogImageConvertTool图像格式转化亮度模式:可以将彩色图像转换为8位黑白图像HSI:输出转换为HSI模式下的图像CogIPOneImageTool常用图像处理方法常见的图像线性变换,卷积(可以自行设置掩膜大小),滤波与形态学CogPixelMapTool修改图像的灰度映射......
  • webpack学习笔记专题目录
    转载请注明来源:http://www.eword.name/Author:ewordEmail:[email protected]学习笔记专题目录webpack专题目录webpack学习笔记MacBook搭建python开发环境【必须】安装Python【必须】安装pip【必须】virtualenv的安装和使用【推荐】安装PyCharm【推荐】Py......
  • 利用ESXi学习设备vfio设备直通
    参考公开VMware硬件辅助的虚拟化《KVM实战原理、进阶与性能优化》场景需要在Guest操作系统中使用硬件虚拟化的能力,此时需要Host向Guest暴露硬件虚拟化能力。配置可以通过下面这个方法:启动Guest后,编辑GRUB参数,使能IOMMU,以Ubuntu为例:编译/etc/default/grub增加了i......
  • 18. 按钮的进一步学习
    图片按钮,单选框,多选框packageGUI;importjavax.swing.*;importjava.awt.*;importjava.net.URL;//按钮的进一步学习//图片按钮,单选框,多选框,本质上也是按钮publicclassTest18{publicstaticvoidmain(String[]args){newJButtonDemo();n......
  • webpack学习笔记所使用的版本信息
    学习笔记所使用的版本信息学习笔记用到的npm包版本信息[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]......
  • python学习日记 2023年8月20日
    fromPILimportImage##pipinstallpillowimportosim=Image.open('./1.jpg')w,h=im.sizeimage_row=3image_column=5names=os.listdir('./img_f')new_img=Image.new('RGB',(image_column*w,image_row*h))foryinra......
  • 《代码整洁之道 Clean Code》学习笔记 Part 1 - 命名、注释、格式
    前段时间在看《架构整洁之道》,里面提到了:构建一个好的软件系统,应该从写整洁代码做起。毕竟,如果建筑使用的砖头质量不佳,再好的架构也无法造就高质量的建筑。趁热打铁,翻出《代码整洁之道》再刷一遍。《代码整洁之道CleanCode》学习笔记Part1衡量代码质量的唯一标准:WTF/min......