首页 > 其他分享 >VisionPro 学习案例 (六)

VisionPro 学习案例 (六)

时间:2023-08-21 22:56:14浏览次数:29  
标签:mToolBlock VisionPro ca tLabel 学习 案例 using Cognex

VisionPro 学习案例 (六)

案例 液位检测(VisionPro)

image-20230821220727646

原图像如下:

image-20230821220831569

总体流程

  1. 先获取模板匹配,来确定瓶子个数
  2. 利用卡尺工具来扫描,获取平均参考线,这个可以作为直线参数。
  3. 再使用模板工具的结果,获取到每一个瓶子。在每个瓶子上使用卡尺工具,来获取当前液面情况,这个通过边缘对,可以获取X与Y的坐标关系。
  4. 遍历每一个瓶子坐标,使用线段与直线的距离测量工具(线段使用上一步的X与Y构造水平线段),直线使用参考水平线。这样可以获得当前液面距离参考液面的距离关系。

image-20230821222039856

具体实施操作

第一步 获取瓶子的参考液面关系(这里需要对图像进行预处理一下,当前液面的对比度太低了,特征不明显,为后续做准备)

image-20230821222018564

image-20230821222123861

第二步 制作水平参考线

使用卡尺工具,输出拟合的X与Y,不带角度,然后就能拟合出直线。

image-20230821222214670

image-20230821222307624

第三步 使用卡尺工具,遍历每一个匹配的安培瓶,然后输出当前的页面信息

首先拖出来一个卡尺对象

然后再开始写脚本

Step1:先获取当前工具的结果

CogPMAlignTool pma = mToolBlock.Tools["CogPMAlignTool1"] as CogPMAlignTool;
CogCaliperTool ca = mToolBlock.Tools["CogCaliperTool2"] as CogCaliperTool;

Step2:设置卡尺工具参数

这里将让卡尺的区域中心坐标是Pma工具的X与Y坐标,然后运行卡尺工具。然后个根据当前的卡尺结果的边缘对情况,制作出线段(当前液面线),注意是当前CogLineSegment这个工具

 foreach(CogPMAlignResult item in pma.Results)
    {
      ca.Region.CenterX = item.GetPose().TranslationX;
      ca.Region.CenterY = item.GetPose().TranslationY;
      ca.Run();
      
      CogLineSegment s = new CogLineSegment();
      s.StartX = ca.Results.Edges[0].PositionX - 10;
      s.StartY = ca.Results.Edges[0].PositionY;
      
      s.EndX = ca.Results.Edges[0].PositionX + 10;
      s.EndY = ca.Results.Edges[0].PositionY;
      s.Color = CogColorConstants.Red;
      s.LineWidthInScreenPixels = 3;
      gc.Add(s);
      
    }

Step3:测量当前液面线段与参考液面线之间的距离,进行OK/NG判断。

注意一点,当添加该工具以后,如果在编译的时候。出现无引用的话,需要添加引用

image-20230821223307425

image-20230821223114615

这里是在上面的 gc.Add(s);后面附加的。关键是Run(),让当前工具进行运行。

 	  Dis.Segment = s;      
      Dis.Run();
      CogGraphicLabel tLabel = new CogGraphicLabel();    
      
      if (Dis.Distance < 15)
      {
        tLabel.SetXYText(s.StartX, s.StartY, "OK");
        tLabel.Color = CogColorConstants.Green;        
      }
      else
      {
        tLabel.SetXYText(s.StartX, s.StartY, "NG");
        tLabel.Color = CogColorConstants.Red;
      }
      gc.Add(tLabel);

代码全文如下:

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

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
  
  CogGraphicCollection gc = new CogGraphicCollection();
  #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);

    CogPMAlignTool pma = mToolBlock.Tools["CogPMAlignTool1"] as CogPMAlignTool;
    CogCaliperTool ca = mToolBlock.Tools["CogCaliperTool2"] as CogCaliperTool;
    CogDistanceSegmentLineTool Dis = mToolBlock.Tools["CogDistanceSegmentLineTool1"] as CogDistanceSegmentLineTool;
    
    gc.Clear();
    foreach(CogPMAlignResult item in pma.Results)
    {
      ca.Region.CenterX = item.GetPose().TranslationX;
      ca.Region.CenterY = item.GetPose().TranslationY;
      ca.Run();
      
      CogLineSegment s = new CogLineSegment();
      s.StartX = ca.Results.Edges[0].PositionX - 10;
      s.StartY = ca.Results.Edges[0].PositionY;
      
      s.EndX = ca.Results.Edges[0].PositionX + 10;
      s.EndY = ca.Results.Edges[0].PositionY;
      s.Color = CogColorConstants.Red;
      s.LineWidthInScreenPixels = 3;
      gc.Add(s);
      
      Dis.Segment = s;      
      Dis.Run();
      CogGraphicLabel tLabel = new CogGraphicLabel();    
      
      if (Dis.Distance < 15)
      {
        tLabel.SetXYText(s.StartX, s.StartY, "OK");
        tLabel.Color = CogColorConstants.Green;        
      }
      else
      {
        tLabel.SetXYText(s.StartX, s.StartY, "NG");
        tLabel.Color = CogColorConstants.Red;
      }
      gc.Add(tLabel);
      
    }
    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 g in gc)
    {
      mToolBlock.AddGraphicToRunRecord(g, lastRecord, "CogIPOneImageTool1.OutputImage", ""); 
    }
  }
  #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

}

案例 液位检测(Halcon)

list_files ('./液位测量', ['files','follow_links'], ImageFiles)
tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
read_image (Image, ImageFiles[0])
get_image_size (Image, Width, Height)
gen_rectangle1 (ModelRegion, 226.536, 355.093, 299.001, 417.629)
reduce_domain (Image, ModelRegion, TemplateImage)
create_shape_model (TemplateImage, 4, rad(0), rad(360), rad(1.2228), ['none','no_pregeneration'], 'use_polarity', [15,24,4], 5, ModelID)
get_shape_model_contours (ModelContours, ModelID, 1)
dev_get_window (WindowHandle)
set_display_font (WindowHandle, 26, 'mono', 'true', 'false')
dev_update_off ()
dev_set_line_width (3)
for Index := 0 to |ImageFiles| - 1 by 1
    read_image (Image, ImageFiles[Index])
    dev_display (Image)
    find_shape_model (Image, ModelID, rad(0), rad(360), 0.63, 15, 0.5, 'least_squares', [4,1], 0.75, Rows, Columns, Angles, Scores)
    RefLevel:=mean(Rows)-160
    dev_set_color ('blue')
    disp_line (WindowHandle, RefLevel, 0, RefLevel, Width-1)
    for I := 0 to |Rows|-1 by 1   
        gen_measure_rectangle2 (mean(Rows)-135, Columns[I], rad(90), 70, 18, Width, Height, 'nearest_neighbor', MeasureHandle)
        measure_pos (Image, MeasureHandle, 1, 4, 'all', 'first', RowEdge, ColumnEdge, Amplitude, Distance)
        if (abs(RowEdge-RefLevel)<20)
            dev_set_draw ('margin')
            dev_set_color ('green')
            disp_rectangle2 (WindowHandle,mean(Rows)-135, Columns[I], rad(90), 70, 18)
            disp_message (WindowHandle, 'OK', 'image', mean(Rows)-135,  Columns[I], 'green', 'true')       
        else
            dev_set_draw ('margin')
            dev_set_color ('red')
            disp_rectangle2 (WindowHandle,mean(Rows)-135, Columns[I], rad(90), 70, 18)
            disp_message (WindowHandle, 'NG', 'image', mean(Rows)-135,  Columns[I], 'red', 'true')           
        endif        
    endfor
wait_seconds (1)
endfor

标签:mToolBlock,VisionPro,ca,tLabel,学习,案例,using,Cognex
From: https://www.cnblogs.com/LtWf/p/17647305.html

相关文章

  • GPT大语言模型引爆强化学习与语言生成模型的热潮、带你了解RLHF。
    GPT大语言模型引爆强化学习与语言生成模型的热潮、带你了解RLHF。随着ChatGPT的爆火,强化学习(ReinforcementLearning)和语言生成模型(LanguageModel)的结合开始变得越来越受人关注。有关ChatGPT的视频讲解可以参考这里。该项目的详细介绍可以参考这里。在这个项目中,我们将......
  • 【学习笔记】优化建图相关(线段树优化,倍增优化)
    优化建图发现并没有人写得很详细的样子,那我也摆烂好惹点击查看目录目录前言线段树优化建图单点连区间区间连区间例题解题:倍增优化建图例题解题:前言众所周知,连边的时间复杂度一般是\(O(1)\),但,当连边的对象是一个连续的树上区间的时候,我们或许有更优的连边方式:优化建图。......
  • Java学习IO流Day01
    io一、File2.1FIle概述File用来表示文件系统中的一个文件或者目录java.io包下2.2方法构造方法File(Stringpathname):通过指定路径名称创建一个新的FIle实例Filefile=newFile("D:\\demo.txt");File(Fileparent,Stringchild):根据父级目录对象和子文......
  • 深入学习JVM01
    一、云时代的JVM云时代的来临也给Java带来了不小的挑战,速度、内存、部署与管理的问题逐渐变成了Java的负累。不过它并没有坐以待毙,而是选择积极地适应新时代,引入更多新特性来应对这些挑战。......
  • SQL注入基础学习3
    SQL注入基础学习3二、靶场实操(开始先学习手工,后面的话,可以采用sqlmap等自动化工具)靶场采用sqli-labs8、第8关第八关只能采用盲注的方法,页面也不显示任何报错的信息。可以采用时间盲注或布尔盲注。测试是否存在注入点。页面输入)'时,页面无显示,说明可能存在注入点。后面的......
  • hadoop开发案例
    本次基于陌陌数据案例实现可视化数据分析数据准备:两个tsv文件,总计包含14w条数据,数据字段包括发送人,接收人账号,性别,GPS坐标等20多个字段,这些字段利用制表符进行分隔开,其中有为null的杂乱数据,需要将这些数据过滤,时间数据格式为年月日时分秒,需要substr()进行截取,GPS坐标利用split......
  • Python学习日记 2023年8月21日
    importrequestsimportosimportrefromtimeimportsleepfrombs4importBeautifulSoupurl='http://www.netbian.com/mei/'headers={'User-Agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,like......
  • openGauss学习笔记-46 openGauss 高级数据管理-子查询
    openGauss学习笔记-46openGauss高级数据管理-子查询子查询或称为内部查询,嵌套查询,指的是在数据库查询的WHERE子句中嵌入查询语句,相当于临时表。一个SELECT语句的查询结果能够作为另一个语句的输入值。子查询可以与SELECT,INSERT,UPDATE和DELETE语句一起使用。以下是子查询必须遵......
  • C学习8
    1、递归做阶乘#include<stdio.h>intfunc1(intx){intz=0;while(x>1){inty=x-1;z=x*func1(y);returnz;}}intmain(){inta=0;scanf_s("%d",&a);intret=func1(......
  • 数据治理专业认证CDMP学习笔记(思维导数据治理专业认证CDMP学习笔记(思维导图与知识点)-
    大家好,我是独孤风,一位曾经的港口煤炭工人,目前在某国企任大数据负责人,公众号大数据流动主理人。在最近的两年的时间里,因为公司的需求,还有大数据的发展趋势所在,我开始学习数据治理的相关知识。数据治理需要进行系统的学习才能真正掌握,也需要进行专业的考试认证才能证明自己在数据治理......