液位检测附代码

那么多瓶液体,不想用那么多卡尺工具一个卡怎么办?上代码啊

添加CogToolBlock不用多说

我直接一个CogPMAlignTool直接找出全部

再添加一个CogCaliperTool随机卡出一个液位

不想多个卡尺工具,那咱们就用for

卡尺中心点按照CogPMAlignTool的中心原点定位一个一个卡,然后重新运行CogCaliperTool工具,话不多说直接上代码

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

public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
  #region Private Member Variables
  private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
//声明文本
  private CogGraphicLabel Label;

  private CogGraphicCollection cc = 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
    cc.Clear();//图形集合清空
    CogPMAlignTool pma = new CogPMAlignTool();//new CogPMAlignTool工具
    pma = mToolBlock.Tools["CogPMAlignTool1"] as CogPMAlignTool;//找他
    CogCaliperTool cal = new CogCaliperTool();//卡尺new他
    cal = mToolBlock.Tools["CogCaliperTool1"]as CogCaliperTool;//再找他
  


    // Run each tool using the RunTool function
    foreach(ICogTool tool in mToolBlock.Tools)
      mToolBlock.RunTool(tool, ref message, ref result);
    //CogPMAlignTool找到几个循环几次
    for (int i = 0; i < pma.Results.Count; i++)
    {
      
      cal.Region.CenterX= pma.Results[i].GetPose().TranslationX;//卡尺工具更换中心点
      cal.Region.CenterY = pma.Results[i].GetPose().TranslationY;
      cal.Run();//运行卡尺工具
      CogRectangle z = new CogRectangle();//new一个方形,也可以线段
     
      double x = cal.Results[0].Edge1.PositionX;//找到卡尺第二个线段坐标
      double y = cal.Results[0].Edge1.PositionY;
      if(cal.Results[0].Width > 110 && cal.Results[0].Width < 130){//制定标准
        Label = new CogGraphicLabel();//循环一次new一个文本
        Label.SetXYText(x, y, "OK "+cal.Results[0].Width.ToString("f2"));//给文本位置
        cc.Add(Label);//文本添加到图形集合
      }else{
        Label = new CogGraphicLabel();
        Label.SetXYText(x, y,"NG " +cal.Results[0].Width.ToString("f2"));
        Label.Color = CogColorConstants.Red;//不合格干成红色
        cc.Add(Label);
      }
     
      z.Color = CogColorConstants.Red;
      z.SetXYWidthHeight(x-8,y,15,0.01);//给方形位置微调他
      cc.Add(z);//添加方形
    }

    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)
  {
    for (int i = 0; i < cc.Count; i++)
    {
			 mToolBlock.AddGraphicToRunRecord(cc[i], lastRecord, "CogPMAlignTool1.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

}

写完后有一些不显示或者报错?

调整CogPMAlignTool中心原点位置或者CogCaliperTool大小

然后再来看看最终效果

  • 10
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
液位检测是指通过使用智能皂液器中的液位检测传感器来实时监测皂液的液位。智能皂液器是一种非接触式的皂液投放设备,它由微控制器、红外传感器、液位检测传感器、舵机等关键部件构成。它的特点是非接触、定量出液、液量数据实时监控和欠液提醒等功能。因此,它可以广泛应用于酒店、机场、办公园区等公共卫生间。\[2\] 在Python中进行液位检测,可以使用cv2库中的滑动条来实时读取并返回滑动条所在位置的值。通过调整阈值、Sobel算子大小等参数,可以对图像进行边缘检测。具体的代码实现可以参考引用\[3\]中的示例代码。 #### 引用[.reference_title] - *1* *3* [Python+OpenCv实现图像边缘检测(滑动调节阈值)](https://blog.csdn.net/qq_52516071/article/details/122770930)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Python + ESP32 DIY自动感应智能皂液器 避免触摸更安全](https://blog.csdn.net/HaaSTech/article/details/125393623)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值