机器视觉VsionPro液位检测
VisionPro 液位检测项目完整笔记这是工业液位 / 液面高度检测的标准方案模板匹配定位 动态卡尺找液面 距离判定 OK/NG适用于瓶装、杯装、试管类液位检测。我把代码、工具、逻辑全部整理成可直接学习、复用的笔记结构清晰、重点标注。一、整体功能一句话总结模板匹配CogPMAlignTool定位瓶子 / 容器位置动态移动卡尺CogCaliperTool2到目标位置精准找液面边缘点画红色水平线标记液面位置距离工具CogDistanceSegmentLineTool计算液面高度判定 OK/NG并在图像上显示绿色 / 红色标签图形叠加显示直观判断液位是否合格二、使用工具清单核心 4 个工具表格工具名称作用CogPMAlignTool1模板匹配定位容器位置解决产品偏移问题CogCaliperTool1辅助卡尺脚本未使用一般用于定位基准线CogCaliperTool2主检测卡尺动态找液面边缘CogDistanceSegmentLineTool1距离测量计算液面到基准线的高度CogIPOneImageTool1图像预处理灰度 / 滤波提升检测稳定性三、代码逐段笔记最核心1. 命名空间固定引用c##region namespace imports using System; using System.Collections; using System.Drawing; using Cognex.VisionPro; using Cognex.VisionPro.ToolBlock; using Cognex.VisionPro.PMAlign; // 模板匹配 using Cognex.VisionPro.Caliper; // 卡尺 using Cognex.VisionPro.Dimensioning;// 距离/尺寸工具 using Cognex.VisionPro.ImageProcessing; // 图像处理 #endregion作用导入 VisionPro 工具库让脚本可以调用匹配、卡尺、距离工具。2. 私有变量脚本全局对象c##region Private Member Variables private CogToolBlock mToolBlock; // 工具块主体固定 CogGraphicCollection gc new CogGraphicCollection(); // 图形集合 #endregiongc用来统一存放所有要画的线、文字最后一次性显示比 List 更适合 VisionPro 图形管理3. 主运行逻辑最重要逐行解析c#// 1. 运行工具块里所有工具预处理、匹配、卡尺等 foreach(ICogTool tool in mToolBlock.Tools) mToolBlock.RunTool(tool, ref message, ref result); // 2. 获取4个核心工具对象 CogPMAlignTool pma mToolBlock.Tools[CogPMAlignTool1] as CogPMAlignTool; CogCaliperTool c mToolBlock.Tools[CogCaliperTool2] as CogCaliperTool; CogDistanceSegmentLineTool dis mToolBlock.Tools[CogDistanceSegmentLineTool1] as CogDistanceSegmentLineTool; // 清空上一帧图形避免重叠 gc.Clear(); // 3. 遍历匹配到的目标支持多目标 foreach (CogPMAlignResult item in pma.Results) { // // 动态移动卡尺到匹配位置 // c.Region.CenterX item.GetPose().TranslationX; c.Region.CenterY item.GetPose().TranslationY; c.Run(); // 卡尺重新运行找边缘 // // 画红色水平线标记液面 // CogLineSegment s new CogLineSegment(); s.StartX c.Results.Edges[0].PositionX; // 液面X s.StartY c.Results.Edges[0].PositionY; // 液面Y s.EndX c.Results.Edges[0].PositionX 20; // 向右画20像素 s.EndY c.Results.Edges[0].PositionY; s.Color CogColorConstants.Red; s.LineWidthInScreenPixels 5; // 粗线醒目 gc.Add(s); // 加入图形集合 // // 距离测量 OK/NG判定 // CogGraphicLabel label new CogGraphicLabel(); dis.Segment s; // 把液面线段给距离工具 dis.Run(); // 计算距离液面到基准线 // 阈值15 → NG ≤15 → OK if(dis.Distance 15) { label.SetXYText(s.StartX, s.StartY, NG); label.Color CogColorConstants.Red; } else { label.SetXYText(s.StartX, s.StartY, OK); label.Color CogColorConstants.Green; } gc.Add(label); // 把OK/NG标签加入集合 } return false;核心逻辑总结模板定位 → 动态移动卡尺解决产品偏移卡尺找液面边缘点画红线标记液面距离工具算高度阈值判定 OK/NG图形存入集合等待显示4. 图形叠加显示最后一步c#// 把所有线、标签画到输出图上 foreach(ICogGraphic g in gc) { mToolBlock.AddGraphicToRunRecord( g, lastRecord, CogIPOneImageTool1.OutputImage, // 画在预处理后的图 ); }四、工具配置关键要点必记1. CogPMAlignTool1模板匹配学习模板瓶身 / 瓶口固定特征作用得到产品 XY 坐标让卡尺跟着产品动2. CogCaliperTool2液面检测卡尺边缘极性明→暗 或 暗→明根据液面是亮还是暗搜索方向垂直向下 / 向上扫过液面对比阈值调高避免干扰3. CogDistanceSegmentLineTool1距离工具一条线是基准线标准液位另一条是卡尺找到的液面线输出两条线之间的垂直距离五、关键知识点面试 / 项目必背液位检测 模板定位 垂直卡尺找边缘动态卡尺匹配结果 XY → 卡尺中心 XY → 重新 Run用CogLineSegment画线标记液面用DistanceSegmentLine测高度差图形统一存入CogGraphicCollectionOK/NG 根据距离阈值判断六、常见调整参数液面 OK 高度if(dis.Distance 15)里的15红线长度20可改长改短红线粗细LineWidthInScreenPixels 5卡尺位置根据匹配结果偏移微调 XY七、完整流程图示plaintext图像采集 → 预处理(IPOne) → 模板匹配(PMAlign) → 动态移动卡尺(Caliper2) → 找液面边缘 → 画红线 → 距离计算 → OK/NG判定 → 图形叠加显示总结这是最标准、最稳定、工业最常用的 VisionPro 液位检测方案鲁棒性强模板匹配抗偏移精度高卡尺亚像素边缘直观红线 OK/NG 标签可扩展多目标、多容器工具参数CogCaliperTool1CogCaliperTool2脚本命名空间#region namespace importsusing 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第一部分#region Private Member Variablesprivate Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;CogGraphicCollection gc new CogGraphicCollection();#endregion第二部分// Run each tool using the RunTool functionforeach(ICogTool tool in mToolBlock.Tools)mToolBlock.RunTool(tool, ref message, ref result);CogPMAlignTool pma mToolBlock.Tools[CogPMAlignTool1] as CogPMAlignTool;CogCaliperTool c mToolBlock.Tools[CogCaliperTool2] as CogCaliperTool;CogDistanceSegmentLineTool dis mToolBlock.Tools[CogDistanceSegmentLineTool1] as CogDistanceSegmentLineTool;gc.Clear();foreach ( CogPMAlignResult item in pma.Results){c.Region.CenterX item.GetPose().TranslationX;c.Region.CenterY item.GetPose().TranslationY;c.Run();CogLineSegment s new CogLineSegment();s.StartX c.Results.Edges[0].PositionX;s.StartY c.Results.Edges[0].PositionY;s.EndX c.Results.Edges[0].PositionX 20;s.EndY c.Results.Edges[0].PositionY;s.Color CogColorConstants.Red;s.LineWidthInScreenPixels 5;gc.Add(s);CogGraphicLabel label new CogGraphicLabel();dis.Segment s;dis.Run();if( dis.Distance 15 ){//NGlabel.SetXYText( s.StartX, s.StartY, NG);label.Color CogColorConstants.Red;}else{//OKlabel.SetXYText(s.StartX, s.StartY, OK);label.Color CogColorConstants.Green;}gc.Add(label);}return false;第三部分foreach( ICogGraphic g in gc){mToolBlock.AddGraphicToRunRecord(g,lastRecord,CogIPOneImageTool1.OutputImage,);}完整代码#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 namemessageSets the Message in the tools RunStatus./param /// param nameresultSets the Result in the tools RunStatus/param /// returnsTrue 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 c mToolBlock.Tools[CogCaliperTool2] as CogCaliperTool; CogDistanceSegmentLineTool dis mToolBlock.Tools[CogDistanceSegmentLineTool1] as CogDistanceSegmentLineTool; gc.Clear(); foreach ( CogPMAlignResult item in pma.Results) { c.Region.CenterX item.GetPose().TranslationX; c.Region.CenterY item.GetPose().TranslationY; c.Run(); CogLineSegment s new CogLineSegment(); s.StartX c.Results.Edges[0].PositionX; s.StartY c.Results.Edges[0].PositionY; s.EndX c.Results.Edges[0].PositionX 20; s.EndY c.Results.Edges[0].PositionY; s.Color CogColorConstants.Red; s.LineWidthInScreenPixels 5; gc.Add(s); CogGraphicLabel label new CogGraphicLabel(); dis.Segment s; dis.Run(); if( dis.Distance 15 ) { //NG label.SetXYText( s.StartX, s.StartY, NG); label.Color CogColorConstants.Red; } else { //OK label.SetXYText(s.StartX, s.StartY, OK); label.Color CogColorConstants.Green; } gc.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 namecurrentRecord /// 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 namelastRecord /// 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 namehostThe 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 }