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;
#endregion
public 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, res);
return false;
}
public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord){
mToolBlock.AddGraphicToRunRecord(label, lastRecord, "CogIPOneImageTool1.InputImage", "Script");
}
1使用cogIPoneImage去掉背景图中的白色噪点
2.添加灰阶形态NxM 内核高度与宽度 设置 3 x3
1.使用blob 匹配出图像中的引脚
1.设置过滤参数
1.使用卡尺工具 测量每个引脚距离 目的:检测是否有缺失的引脚
#region namespace imports
using System;
using System.Collections;
using System.Collections.Generic;
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.Blob;
using Cognex.VisionPro.Caliper;
#endregion
public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
#endregion
//声明图形集合
private CogGraphicCollection col = new CogGraphicCollection();
//声明BlobTool成员变量
CogBlobTool blob;
// 声明卡尺工具成员变量
CogCaliperTool cal1;
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
//映射blob
blob = mToolBlock.Tools["CogBlobTool1"] as CogBlobTool;
//映射卡尺工具
cal1 = mToolBlock.Tools["CogCaliperTool1"] as CogCaliperTool;
//判断是否有图形缺陷 true 为无缺陷 false反之
bool b = true;
//清空图形集合
col.Clear();
// 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 < blob.Results.GetBlobs().Count; i++)
{
//获取角度
double angle = Math.Abs((blob.Results.GetBlobs()[i].Angle) * 180 / Math.PI);
//通过角度大小判断是否NG
if (Math.Abs(angle- 90) > 5)
{
//初始化
CogPolygon polygon = new CogPolygon();
polygon = blob.Results.GetBlobs()[i].GetBoundary();
polygon.Color = CogColorConstants.Red;
col.Add(polygon);
b = false;
}
//获取blob检测的高度
double h = blob.Results.GetBlobs()[i].GetMeasure(CogBlobMeasureConstants.BoundingBoxExtremaAngleHeight);
//通过Y坐标判断 斑点在上方还是下方
//上方
if (blob.Results.GetBlobs()[i].CenterOfMassY < 220)
{
//判断高度 大于110或者小于90 上方特征有缺陷
if (h > 110 || h < 90)
{
//
CogPolygon polygon = new CogPolygon();
polygon = blob.Results.GetBlobs()[i].GetBoundary();
polygon.Color = CogColorConstants.Red;
col.Add(polygon);
b = false;
}
}
//下方
else if(blob.Results.GetBlobs()[i].CenterOfMassY > 220)
{
//判断高度 大于65或者小于50 下方特征有缺陷
if (h > 65 || h < 50)
{
//初始化边界显示线
CogPolygon polygon = new CogPolygon();
polygon = blob.Results.GetBlobs()[i].GetBoundary();
polygon.Color = CogColorConstants.Red;
//添加到图形集合工具
col.Add(polygon);
b = false;
}
}
}
//卡尺判断间距 判断引脚是否有缺失
//初始化泛型集合
List<double> listX = new List<double>();
//循环 卡尺工具结果个数
for (int j = 0; j < cal1.Results.Count; j++)
{
//添加每个结果X坐标点到泛型集合中
listX.Add(cal1.Results[j].Edge0.PositionX);
}
listX.Sort();//升序 目的:把做升序排列 方便后续 进行差值计算
for (int i = 0; i < listX.Count - 1; i++)
{
//相邻坐标点求 差值
double width = listX[i + 1] - listX[i];
//差值大于80 证明引脚有缺失
if (width > 80)
{
//初始化图形限定框
CogRectangleAffine rec = new CogRectangleAffine();
//限定款位置
rec.CenterX = (listX[i + 1] + listX[i]) / 2;
rec.CenterY = cal1.Results[i].Edge0.PositionY;
rec.SideXLength = width;
rec.SideYLength = 100;
rec.Color = CogColorConstants.Red;
//添加到图形集合工具
col.Add(rec);
b = false;
}
}
//初始化label 显示NG 或者OK信息
CogGraphicLabel label = new CogGraphicLabel();
label.Font = new Font("微软雅黑", 20);
if (b)
{
label.SetXYText(100, 80, "OK");
label.Color = CogColorConstants.Green;
}
else
{
label.SetXYText(100, 80, "NG");
label.Color = CogColorConstants.Red;
}
//添加到图形集合工具
col.Add(label);
return false;
}
public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
{
for (int i = 0; i < col.Count; i++)
{
mToolBlock.AddGraphicToRunRecord(col[i], lastRecord, "CogIPOneImageTool1.InputImage", "Script");
}
}
1.图像去噪
中值Nxm: 内核高度和宽度 设置 7x7 消除干扰像素
灰度形态调整:设置腐蚀 来缩小白色区域 扩大黑色区域 加粗字体效果