1.
#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.CalibFix;
using Cognex.VisionPro.CompositeColorMatch;
#endregionpublic class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
#endregion
//声明一个label成员变量
private CogGraphicLabel label;
//声明CogPMAlignTool成员变量
CogPMAlignTool pma;
//声明CogCompositeColorMatchTool成员变量
CogCompositeColorMatchTool colorTool;
//声明图形集合
private CogGraphicCollection col = new CogGraphicCollection();
public override bool GroupRun(ref string message, ref CogToolResultConstants result)
{
//映射CogPMAlignTool对象
pma = mToolBlock.Tools["CogPMAlignTool1"] as CogPMAlignTool;
//映射CogCompositeColorMatchTool对象
colorTool = mToolBlock.Tools["CogCompositeColorMatchTool1"] as CogCompositeColorMatchTool;
//初始化label对象
label =new CogGraphicLabel();
//清空集合
col.Clear();
// Run each tool using the RunTool function
foreach(ICogTool tool in mToolBlock.Tools)
mToolBlock.RunTool(tool, ref message, ref result);
int orange =0,red = 0,green = 0,blue = 0,yellow = 0;
//code
//1.遍历pma结果
//2.循环匹配颜色
for (int i = 0; i < pma.Results.Count; i++)
{
//初始化图形限定框
CogRectangleAffine rec = new CogRectangleAffine();
//获取模板匹配的结果坐标点 用于限定框的坐标
rec.CenterX = pma.Results[i].GetPose().TranslationX;
rec.CenterY = pma.Results[i].GetPose().TranslationY;
//获取模板匹配的结果旋转度 用于限定框的旋转度
rec.Rotation = pma.Results[i].GetPose().Rotation;
//限定框长宽设置
rec.SideXLength = 100;
rec.SideYLength = 20;
//限定框颜色
rec.Color = CogColorConstants.Red;
//设置好的限定款 做为 颜色匹配工具的限定款
colorTool.Region = rec;
//执行颜色匹配工具
colorTool.Run();
//获得颜色匹配工具结果中最佳匹配的名字 用于后续记录个数
string color = colorTool.Result.ResultOfBestMatch.Color.Name;
//使用 switch 来进行颜色筛选 同时记录对象颜色个数
switch (color.ToLower())
{
case"orange":
orange++;
break;
case"red":
red++;
break;
case"green":
green++;
break;
case"blue":
blue++;
break;
case"yellow":
yellow++;
break;
default:break;
}
}
//拼接颜色字符串 添加到label中
string text = "Orange: " + orange + " red: " + red + " green: " + blue + " blue: " + orange + " yellow: " + yellow;
label.Font = new Font("宋体", 20);
label.Color = CogColorConstants.Green;
label.SetXYText(300, 100, text);
return false;
}
public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
{
mToolBlock.AddGraphicToRunRecord(label, lastRecord, "CogImageConvertTool1.InputImage", "Script");
}
2.案例:网格缺陷检测
1.使用IPoneimage 预处理图片工具 调整图形的整体对比度 使用blob时 更能清楚的辨别特征2.侵蚀正方体 使blob 白底区域减少 继续优化匹配结果
1.过滤周长 0-200
#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.Blob;
#endregionpublic class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
#endregion
//声明图形集合
private CogGraphicCollection col = new CogGraphicCollection();//声明blob成员变量
CogBlobTool blob;
public override bool GroupRun(ref string message, ref CogToolResultConstants result)
{
//绑定工具集合工具
blob = mToolBlock.Tools["CogBlobTool1"] as CogBlobTool;
//清空集合
col.Clear();
foreach(ICogTool tool in mToolBlock.Tools)
mToolBlock.RunTool(tool, ref message, ref result);//CogPolygon 边界线显示图形 可以勾画出blob中缺陷的轮廓
for (int i = 0; i < blob.Results.GetBlobs().Count; i++)
{
//初始化边界线显示图形
CogPolygon polygon = new CogPolygon();
//通过blob工具 获得所有结果的 边界线 GetBoundary() 获取边界线对象
polygon = blob.Results.GetBlobs()[i].GetBoundary();
//设置边界线颜色
polygon.Color = CogColorConstants.Red;
//设置边界线粗细度
polygon.LineWidthInScreenPixels = 5;
//添加边界线图形 到图形集合中
col.Add(polygon);
}
return false;
}
public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
{
//添加边界图形到LastRunRecord
for (int i = 0; i < col.Count; i++)
{
mToolBlock.AddGraphicToRunRecord(col[i], lastRecord, "CogIPOneImageTool1.InputImage", "Script");
}
}
3.
1.彩色图像中提取像素来创建灰度图像
2.对于所有黄色胶囊进行颜色提取 目的:为了得到黄色区域的灰度图1.经过提取后得到的灰度图 但是 灰度图中 胶囊因为有光照影响 出现了不同大小的黑色斑点
1.为了减少 黑色斑点带来的blob匹配难度 使用CogIPOneImageTool 来预处理图形
2.使用灰阶形态NxM
3.调整内核宽高
4.调整后图像1.使用blob设置参数
1.过滤 干扰 特征
#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.ColorExtractor;
using Cognex.VisionPro.ImageProcessing;
using Cognex.VisionPro.Blob;
#endregionpublic class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
#endregion
//声明图形集合
private CogGraphicCollection col = new CogGraphicCollection();
//声明blob
private CogBlobTool blob;
public override bool GroupRun(ref string message, ref CogToolResultConstants result)
{
//绑定工具集合工具
blob = mToolBlock.Tools["CogBlobTool1"] as CogBlobTool;
//获取ToolBlock 设置的输入参数 目的:用于blob结果判断的参考 也为以后动态设置检测胶囊数量 和面积 提供接口
int count = (int)mToolBlock.Inputs["Count"].Value;
int minArea = (int) mToolBlock.Inputs["MinArea"].Value;
//清空集合
col.Clear();
// Run each tool using the RunTool function
foreach(ICogTool tool in mToolBlock.Tools)
mToolBlock.RunTool(tool, ref message, ref result);
//定义缺失个数和ng个数变量 用于记录
int queShiCount = 0;
int ngCount = 0;
//通过筛选blob面积,判断药片质量
for (int i = 0; i < blob.Results.GetBlobs().Count; i++)
{
CogGraphicLabel label = new CogGraphicLabel();
string res = "";
label.Font = new Font("微软雅黑", 18);
label.LineWidthInScreenPixels = 5;
if (blob.Results.GetBlobs()[i].Area < minArea)
{
ngCount++;
res = "NG";
label.Color = CogColorConstants.Red;
}
else
{
res = "OK";
label.Color = CogColorConstants.Green;
}
//获取blob每个结果的中心坐标 用于 label坐标
double x = blob.Results.GetBlobs()[i].CenterOfMassX;
double y = blob.Results.GetBlobs()[i].CenterOfMassY;
//设置label位置和内容
label.SetXYText(x, y, res);
//添加label 到图像集合中
col.Add(label);
}
//判断blob结果数量 获取缺失个数
if (blob.Results.GetBlobs().Count < count)
{
queShiCount = count - blob.Results.GetBlobs().Count;
}
//创建label 用于显示 ng和缺失 或者合格
CogGraphicLabel label2 = new CogGraphicLabel();
label2.Font = new Font("微软雅黑", 18);
label2.LineWidthInScreenPixels = 5;
//res2 用于记录 缺失ng 或者合格字符串
string res2 = "";
if (queShiCount > 0 || ngCount > 0)
{
res2 = "缺失:" + queShiCount + " NG: " + ngCount;
label2.Color = CogColorConstants.Red;
}
else
{
res2 = "合格";
label2.Color = CogColorConstants.Green;
}
//设置label2位置和内容
label2.SetXYText(100, 100, res2);
//添加label2 到图像集合中
col.Add(label2);
return false;
}
public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
{
for (int i = 0; i < col.Count; i++)
{
mToolBlock.AddGraphicToRunRecord(col[i], lastRecord, "CogColorExtractorTool1.InputImage", "Script");
}
}
4.糖果检测 和 1 案例 基本一致
区别
案例1 使用时 pma 配合 CogRectangleAffine 矩形限定框 来进行CogCompositeColorMatchTool 颜色匹配工具 区域的划分
案例4 使用blob 配合CogCircle 圆心图形 来进行CogCompositeColorMatchTool 颜色匹配工具 区域的划分
1.在block开头加了 一个CogIPoneImageTool 目的:为了 后续图形 需要 图像预处理时 备用
#region namespace imports
using System;
using System.Collections;
using System.Drawing;
using System.IO;
using System.Text;
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.CompositeColorMatch;
#endregion
public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
#endregion
//声明label成员变量
CogGraphicLabel label;public override bool GroupRun(ref string message, ref CogToolResultConstants result)
{
//映射 CogCompositeColorMatchTool 对象
CogCompositeColorMatchTool colorMatch = mToolBlock.Tools["CogCompositeColorMatchTool1"]as CogCompositeColorMatchTool;
//映射 CogBlobTool对象CogBlobTool blob = mToolBlock.Tools["CogBlobTool1"] as CogBlobTool;
// Run each tool using the RunTool function
foreach(ICogTool tool in mToolBlock.Tools)
mToolBlock.RunTool(tool, ref message, ref result);
label = new CogGraphicLabel();
int yellow=0,green = 0,blue = 0,orange = 0,red = 0;
for (int i = 0; i < blob.Results.GetBlobs().Count; i++)
{
//创建圆心图形
CogCircle c = new CogCircle();
//使用blob结果的中心位置 (保险丝案例使用的时pma结果的中心位置)
double x = blob.Results.GetBlobs()[i].CenterOfMassX;
double y = blob.Results.GetBlobs()[i].CenterOfMassY;
//设置圆心坐标
c.CenterX = x;
c.CenterY = y;
c.Radius = 5;
//设置颜色匹配工具 的 检测区域
colorMatch.Region = c;
//执行工具
colorMatch.Run();
string colorName = colorMatch.Result.ResultOfBestMatch.Color.Name;
switch (colorName)
{
case "Yellow":
yellow++;
break;
case "Green":
green++;
break;
case "Blue":
blue++;
break;
case "Orange":
orange++;
break;
case "Red":
red++;
break;
}
//使用可变字符串进行拼接
StringBuilder sb = new StringBuilder();
sb.Append("Red: ").Append(red).Append(" Orange: ").Append(orange).
Append(" Green: ").Append(green).Append(" Blue: ").Append(blue).
Append(" Yellow: ").Append(yellow);
//显示label
label.SetXYText(150, 20, sb.ToString());
label.Color = CogColorConstants.Green;
label.Font = new Font("宋体", 20);
}
return false;
}
public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
{
mToolBlock.AddGraphicToRunRecord(label, lastRecord, "CogIPOneImageTool1.InputImage", "script");
}