当前位置: 首页 > news >正文

Visionpro 液位高度检测

一.项目示例

分析:

以上这种属于检测液体是否发表, 由于相机拍摄很模糊,下面我们会用到,

图像与处理工具,CogIPOneImageTool  ,和像素处理 CogPixelMapTool

二 . 原图:

三. 题目要求

液位高度检测  手动定义一条基准线(绿色线条 通过卡尺工具和createline)  测量值液位高度到基准线高度  大于130 小于110为NG

四. 解决方法

注意:我这里写的比较少,好多工具都是手搓出来的

1.工具

2.图像处理

 

3.接下来,就是我手搓的两个工具 CogCreateSegmentTool  和  CogDistanceSegmentLineTool  (这里就不给大家拉了,学习任务繁重,望理解)

 拉好之后是这个样子

4.声明

 CogGraphicCollection dt = new CogGraphicCollection();
  CogGraphicLabel label;
  CogCreateSegmentTool segment;
  CogDistanceSegmentLineTool segmentLine;

5.将拉的工具进行声明

    
    dt.Clear();
    CogPixelMapTool pixel = mToolBlock.Tools["CogPixelMapTool1"]as CogPixelMapTool;
    CogPMAlignTool pma = mToolBlock.Tools["CogPMAlignTool1"]as CogPMAlignTool;
    CogCaliperTool caliper = mToolBlock.Tools["CogCaliperTool1"]as CogCaliperTool;
    CogCreateLineTool line = mToolBlock.Tools["CogCreateLineTool1"]as CogCreateLineTool;

6.循环并创建多个卡尺

  for(int i = 0;i < pma.Results.Count;i++)
    {
      caliper.Region.CenterX = pma.Results[i].GetPose().TranslationX;
      caliper.Region.CenterY = pma.Results[i].GetPose().TranslationY;
      caliper.Run();
      

7.给卡尺的位置创建一条线段

 segment = new CogCreateSegmentTool();
      segment.InputImage = pixel.OutputImage;
      segment.Segment.StartX = caliper.Results[0].Edge0.PositionX;
      segment.Segment.StartY = caliper.Results[0].Edge0.PositionY;
      segment.Segment.EndX = caliper.Results[0].Edge0.PositionX + 20;
      segment.Segment.EndY = caliper.Results[0].Edge0.PositionY;
      segment.Run();
      segment.GetOutputSegment().Color = CogColorConstants.Purple;
      dt.Add(segment.GetOutputSegment());

8.线段连接线,找出线段到线的那段距离

segmentLine = new CogDistanceSegmentLineTool();
      segmentLine.InputImage = pixel.OutputImage;
      segmentLine.Segment = segment.GetOutputSegment();
      segmentLine.Line = line.GetOutputLine();
      segmentLine.Run();

9.判断,距离是否合格

 label = new CogGraphicLabel();
      double width = Math.Round((segmentLine.Distance),2);
      double x = caliper.Results[0].Edge0.PositionX;
      double y = caliper.Results[0].Edge0.PositionY;
      if(width > 130 || width < 110)
      {
        label.Color = CogColorConstants.Red;
        label.SetXYText(x, y -15, "NG:" + width);
      }else
      {
        label.Color = CogColorConstants.Green;
        label.SetXYText(x, y -15, "OK:" + width);
      }
      label.Font = new Font("楷体", 15);
      dt.Add(label);
      
    }  

10.最后把集合遍历出来,放在你想要放的工具页面上去

 foreach(ICogGraphic s in dt)
    {
      mToolBlock.AddGraphicToRunRecord(s, lastRecord, "CogPixelMapTool1.输出图像", "script");
    }

五. 代码

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

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  #endregion
  
  CogGraphicCollection dt = new CogGraphicCollection();
  CogGraphicLabel label;
  CogCreateSegmentTool segment;
  CogDistanceSegmentLineTool segmentLine;
  /// <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
    
    dt.Clear();
    CogPixelMapTool pixel = mToolBlock.Tools["CogPixelMapTool1"]as CogPixelMapTool;
    CogPMAlignTool pma = mToolBlock.Tools["CogPMAlignTool1"]as CogPMAlignTool;
    CogCaliperTool caliper = mToolBlock.Tools["CogCaliperTool1"]as CogCaliperTool;
    CogCreateLineTool line = mToolBlock.Tools["CogCreateLineTool1"]as CogCreateLineTool;
    
   
    // Run each tool using the RunTool function
    foreach(ICogTool tool in mToolBlock.Tools)
      mToolBlock.RunTool(tool, ref message, ref result);
    
    for(int i = 0;i < pma.Results.Count;i++)
    {
      caliper.Region.CenterX = pma.Results[i].GetPose().TranslationX;
      caliper.Region.CenterY = pma.Results[i].GetPose().TranslationY;
      caliper.Run();
      
      segment = new CogCreateSegmentTool();
      segment.InputImage = pixel.OutputImage;
      segment.Segment.StartX = caliper.Results[0].Edge0.PositionX;
      segment.Segment.StartY = caliper.Results[0].Edge0.PositionY;
      segment.Segment.EndX = caliper.Results[0].Edge0.PositionX + 20;
      segment.Segment.EndY = caliper.Results[0].Edge0.PositionY;
      segment.Run();
      segment.GetOutputSegment().Color = CogColorConstants.Purple;
      dt.Add(segment.GetOutputSegment());
      
      
      segmentLine = new CogDistanceSegmentLineTool();
      segmentLine.InputImage = pixel.OutputImage;
      segmentLine.Segment = segment.GetOutputSegment();
      segmentLine.Line = line.GetOutputLine();
      segmentLine.Run();
      
      label = new CogGraphicLabel();
      double width = Math.Round((segmentLine.Distance),2);
      double x = caliper.Results[0].Edge0.PositionX;
      double y = caliper.Results[0].Edge0.PositionY;
      if(width > 130 || width < 110)
      {
        label.Color = CogColorConstants.Red;
        label.SetXYText(x, y -15, "NG:" + width);
      }else
      {
        label.Color = CogColorConstants.Green;
        label.SetXYText(x, y -15, "OK:" + width);
      }
      label.Font = new Font("楷体", 15);
      dt.Add(label);
      
    }  
    

      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(ICogGraphic s in dt)
    {
      mToolBlock.AddGraphicToRunRecord(s, lastRecord, "CogPixelMapTool1.输出图像", "script");
    }
  }
  #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

}

相关文章:

  • Sentinel 持久化配置
  • LeetCode刷题第6题【Z 字形变换】---解题思路及源码注释
  • 哈希表-四数之和
  • ceph部署-14版本(nautilus)-使用ceph-ansible部署实验记录
  • 常用架构图:业务架构、产品架构、系统架构、数据架构、技术架构、应用架构、功能架构及信息架构
  • java 通过阿里物联网平台推送数据到显示屏
  • 【办公类-90-01】】20250213周计划四类活动的写法(分散运动、户外游戏、个别化(美工室图书吧探索室))
  • Spring Boot 的约定优于配置,你的理解是什么?
  • Spreadjs与GcExcel
  • 如何使用 HPjtune 分析 Java GC 日志并优化 JVM 性能
  • JS的map方法和Map对象
  • 自己搭建可以和deepseek对话的WEB应用
  • Cursor AI开发微信小程序教程
  • DeepSeek崛起:中国AI产业的颠覆者与重构者
  • vue学习笔记10
  • html+css设计情人节网页制作主页页面
  • Tree Search for Web Agents
  • Attanger: Zotfile 插件在 Zotero7 的平替
  • ElementUI 的组件 Switch(开关)如何让文字显示在按钮上
  • DeepSeek 助力 Vue 开发:打造丝滑的无限滚动(Infinite Scroll)
  • 网站多久才能做起来/免费数据统计网站
  • 有域名了如何做网站/如何制作个人网站
  • 建造师官网/seo技术分享免费咨询
  • 温州百度网站快速优化/seo网站排名的软件
  • 外贸建站用的服务器/seo1新地址在哪里
  • wordpress可以做什么站/推销网站