通过脚本实现CogDistancePointLineTool的功能
(引用命名空间在上篇文章有介绍,在此不赘述)
工具实例化
CogDistancePointLineTool distanceTool = new CogDistancePointLineTool();
输入参数:
image:
Cognex.VisionPro.CogImage8Grey image = mToolBlock.Inputs["Image"].Value as Cognex.VisionPro.CogImage8Grey;
distanceTool.InputImage = image;
line:
Cognex.VisionPro.CogLine line = mToolBlock.Inputs["line_down"].Value as Cognex.VisionPro.CogLine;
distanceTool.Line = line;
X/Y:
Cognex.VisionPro.Caliper.CogFindLineResults points = mToolBlock.Inputs["points_up"].Value as Cognex.VisionPro.Caliper.CogFindLineResults;
foreach(CogFindLineResult point in points)
{
if(point.Found && point.Used)
{
distanceTool.X = point.X;
distanceTool.Y = point.Y;
distanceTool.Run();
}
}
输出结果:
distanceTool.Distance ;//可用于输出,也可用于保存至结果数据中
double distance = distanceTool.Distance;
mToolBlock.Outputs["Output"].Value = distance;
var sx = distanceTool.SegmentX; //可用于输出,也可用于绘制图像线段
var sy = distanceTool.SegmentY;
//计算点到线的平均距离
int nNum = 0;
double dDistanceSum = 0;
double dDistanceAvg = 0;
StringBuilder sb = new StringBuilder();
Cognex.VisionPro.Caliper.CogFindLineResults points = mToolBlock.Inputs["points_up"].Value as Cognex.VisionPro.Caliper.CogFindLineResults;
Cognex.VisionPro.CogLine line = mToolBlock.Inputs["line_down"].Value as Cognex.VisionPro.CogLine;
Cognex.VisionPro.CogImage8Grey image = mToolBlock.Inputs["Image"].Value as Cognex.VisionPro.CogImage8Grey;
CogDistancePointLineTool distanceTool = new CogDistancePointLineTool();
distanceTool.InputImage = image;
distanceTool.Line = line;
foreach(CogFindLineResult point in points)
{
if(point.Found && point.Used)
{
distanceTool.X = point.X;
distanceTool.Y = point.Y;
distanceTool.Run();
nNum++;
dDistanceSum +=distanceTool.Distance;
// string strWriter = nNum.ToString() + "," + distanceTool.Distance.ToString();
// sb.AppendLine(strWriter);
}
}
dDistanceAvg = dDistanceSum / nNum;
// string strOut = "average" + "," + dDistanceAvg;
// sb.Append(strOut);
// SaveToFile(sb.ToString(), "D:\\tempPoint2Line.csv");
mToolBlock.Outputs["Output"].Value = dDistanceAvg;