1.案例 齿轮缺角
1.预留图形预处理工具
1.边缘提取工具
利用copyRegin工具 实现 齿轮环 分割效果 为blob分析 齿轮缺失做准备
1和2 把 blob中的灰度图添加到 copyRegin的俩个图像输入参数中
3.把blob匹配结果中心坐标给copyRegin 匹配区域坐标
4.设置填充数值 128 目的 为了 使填充的区域灰度值和 目标图像背景灰度值 一致
5.填充区域设置
6.区域外 调整像素 调整为 不写入像素
1.勾选使用图像配对
1.运行
2.运行后最终效果 (此 图效果 可以清晰的匹配 每个齿轮)
1.使用blob工具
2.设置参数
3.匹配效果图根据blob分析的结果数 来判断是否 缺失齿轮
#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.Blob;
#endregionpublic class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
#endregion
//定义图形集合
CogGraphicLabel label = new CogGraphicLabel();
public override bool GroupRun(ref string message, ref CogToolResultConstants result)
{
int count = (int)mToolBlock.Inputs["Count"].Value;
CogBlobTool blob = mToolBlock.Tools["CogBlobTool2"] as CogBlobTool;
// Run each tool using the RunTool function
foreach(ICogTool tool in mToolBlock.Tools)
mToolBlock.RunTool(tool, ref message, ref result);
//获取blob结果个数
int currentCount = blob.Results.GetBlobs().Count;
//获取齿轮差值
int diff = count - currentCount;
string res = "";
label.Color = CogColorConstants.Green;
label.Font = new Font("宋体", 20);
//根据差值判断是否有缺失
if (diff > 0)
{
res = "缺失:" + diff;
label.Color = CogColorConstants.Red;
}
else
{
res = "良品";
}
label.SetXYText(100, 250