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

VisionPro学习日志(三)

时间:2023-08-21 09:02:29浏览次数:33  
标签:VisionPro private 学习 int mes using 日志 Cognex

VisionPro学习日志(三)

(1)脚本在哪写

添加脚本的方式,使用VB或者C#

image-20230804173150114

创建脚本

image-20230804173235450

还能进入ToolGroup控件内进行脚本的编写

注意,当有控件移除或者添加,那么需要使用添加或者移除引用集

image-20230804173522562

image-20230804194101648

(2)实例

案例1 统计图片中大米,红豆和花生的个数

image-20230807085146130

实现结果:

image-20230807085224233

方法:

  1. 使用Blob工具进行斑点提取,然后修改阈值范围,让物料都处于斑点状态(不要丢失物料的形状)
  2. 编写脚本对于每个斑点的面积进行判断,得到当前斑点的类型,然后进行统计

Blob对物料进行二值化

  1. 先观察灰度直方图的前景和对象的大致分割范围

    image-20230807085717773

  2. 然后微调阈值,让图像进行分割

    image-20230807085823558

  3. 适当使用形态学进行修饰

    image-20230807085835759

脚本编写思路:

  1. 获取blob工具中的所有结果
  2. 给每个结果使用label标注当前种类
  3. 最后统计每个种类的个数,并且显示出来
#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中编写主要的逻辑。

标签:VisionPro,private,学习,int,mes,using,日志,Cognex
From: https://www.cnblogs.com/LtWf/p/17645084.html

相关文章

  • 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......
  • 日志异步工作器的实现
    日志异步工作器的实现/*实现异步工作器*/#ifndef__M_LOOPER_H__#define__M_LOOPER_H__#include<mutex>#include<thread>#include<condition_variable>//条件变量#include"buffer.hpp"#include<functional>#include<memory>namespacen......
  • 重新学习一下new Date()
    newDate()你知道多少很多小伙伴可能都知道,Date是js中的一个内置对象,用于处理日期和时间。当你调用newDate()时,它会创建一个新的日期(Date)对象。表示当前本地日期和时间。那么你知道newDate()可以接收几种形式的参数吗?它的默认返回是什么日期格式?newDate()可......
  • 关于decimal非常浅显的学习与整理
    关于decimal非常浅显的学习与整理背景知识整数,小数,浮点,定点整数(Integer)是没有小数部分的数值,可以是正数、负数或零。在计算机中,整数通常以二进制形式存储。小数(Decimal)是带有小数部分的数值。小数可以是有限的,也可以是无限循环的。在计算机中,小数通常以浮点数或定点数的......