发表于:2017/8/17 22:30:15
#0楼
如图某个行业需要检测如上图的胶线。
用的是康耐视的CCD。
以下是ccd工程师编写的算法脚本。
但是他这种算法不行。有没有更好的解决方案。
有些涉及到保密就不方便透露更多了
#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.CalibFix;
using Cognex.VisionPro.PixelMap;
using Cognex.VisionPro.ImageProcessing;
using Cognex.VisionPro.Dimensioning;
#endregion
public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
#endregion
//尺寸显示
CogGraphicLabel MyLabel = new CogGraphicLabel();
/// <summary>
/// Called when the parent tool is run.
/// Add code here to customize or replace the normal run behavior.
/// </summary>
/// <param name="message">Sets the Message in the tool's RunStatus.</param>
/// <param name="result">Sets the Result in the tool's RunStatus</param>
/// <returns>True 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);
int i;
double StandardLine = Convert.ToDouble(mToolBlock.Inputs["InputStandardLine"].Value);
double UpLine = Convert.ToDouble(mToolBlock.Inputs["InputUpLine"].Value);
double DownLine = Convert.ToDouble(mToolBlock.Inputs["InputDownLine"].Value);
double StandardWidth = Convert.ToDouble(mToolBlock.Inputs["InputStandardWidth"].Value);
double UpWidth = Convert.ToDouble(mToolBlock.Inputs["InputUpWidth"].Value);
double DownWidth = Convert.ToDouble(mToolBlock.Inputs["InputDownWidth"].Value);
//尺寸显示
ArrayList xPos = new ArrayList();
ArrayList yPos = new ArrayList();
int index = 0;
MyLabel.Font = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point);
MyLabel.Alignment = CogGraphicLabelAlignmentConstants.BaselineLeft;
ArrayList vLine = new ArrayList();
ArrayList vWidth = new ArrayList();
for(i = 1;i <= 9;i++)
{
Cognex.VisionPro.Dimensioning.CogDistancePointLineTool MyTool =
(Cognex.VisionPro.Dimensioning.CogDistancePointLineTool) mToolBlock.Tools["总宽" + i.ToString()];
Cognex.VisionPro.Dimensioning.CogDistancePointLineTool MyTool2 =
(Cognex.VisionPro.Dimensioning.CogDistancePointLineTool) mToolBlock.Tools["胶线" + i.ToString()];
double LineDis = Convert.ToDouble(MyTool.Distance);
double WidthDis = Convert.ToDouble(MyTool.Distance) - Convert.ToDouble(MyTool2.Distance);
//尺寸显示
double x = Convert.ToDouble(MyTool.X);
double y = Convert.ToDouble(MyTool.Y) + 0.2;
xPos.Add(x);
yPos.Add(y);
vLine.Add(LineDis);
if(MyTool2.Distance > 0.2)
{
vWidth.Add(LineDis - 0.2);
}
else
{
vWidth.Add(WidthDis);
}
}
bool find = false;
for(i = 0;i <9;i++)
{
if( Convert.ToDouble(vLine[i]) > StandardLine + UpLine ||
Convert.ToDouble(vLine[i]) < StandardLine - DownLine)
{
mToolBlock.Outputs["Output2"].Value = vLine[i];
find = true;
index = i;
MyLabel.Color = CogColorConstants.Red;
break;
}
}
if(!find)
{
mToolBlock.Outputs["Output2"].Value = vLine[0];
MyLabel.Color = CogColorConstants.Green;
index = 0;
}
string strBuf = vLine[index].ToString();
string strData = "";
if(strBuf.Length > 4)
{
for(int m = 0;m < 4;m++)
{
strData += strBuf[m];
}
}
else
{
strData = strBuf;
}
MyLabel.SetXYText(Convert.ToDouble(xPos[index]), Convert.ToDouble(yPos[index]), strData);
MyLabel.LineWidthInScreenPixels = 2;
bool find2 = false;
for(i = 0;i < 9;i++)
{
if( Convert.ToDouble(vWidth[i]) > StandardWidth + UpWidth ||
Convert.ToDouble(vWidth[i]) < StandardWidth - DownWidth)
{
mToolBlock.Outputs["Output1"].Value = vWidth[i];
find2 = true;
break;
}
}
if(!find2)
{
mToolBlock.Outputs["Output1"].Value = vWidth[0];
}
mToolBlock.Outputs["Output1"].Value = mToolBlock.Outputs["Output2"].Value;
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 name="currentRecord">
/// 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 name="lastRecord">
/// The new last run record is available to be initialized or customized.</param>
public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
{
mToolBlock.AddGraphicToRunRecord(MyLabel, lastRecord, "CogFixtureTool1.OutputImage", "script");
}
#endregion
#region When the Script is Initialized
/// <summary>
/// Perform any initialization required by your script here
/// </summary>
/// <param name="host">The 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
}
用的是康耐视的CCD。
以下是ccd工程师编写的算法脚本。
但是他这种算法不行。有没有更好的解决方案。
有些涉及到保密就不方便透露更多了
#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.CalibFix;
using Cognex.VisionPro.PixelMap;
using Cognex.VisionPro.ImageProcessing;
using Cognex.VisionPro.Dimensioning;
#endregion
public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
#endregion
//尺寸显示
CogGraphicLabel MyLabel = new CogGraphicLabel();
/// <summary>
/// Called when the parent tool is run.
/// Add code here to customize or replace the normal run behavior.
/// </summary>
/// <param name="message">Sets the Message in the tool's RunStatus.</param>
/// <param name="result">Sets the Result in the tool's RunStatus</param>
/// <returns>True 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);
int i;
double StandardLine = Convert.ToDouble(mToolBlock.Inputs["InputStandardLine"].Value);
double UpLine = Convert.ToDouble(mToolBlock.Inputs["InputUpLine"].Value);
double DownLine = Convert.ToDouble(mToolBlock.Inputs["InputDownLine"].Value);
double StandardWidth = Convert.ToDouble(mToolBlock.Inputs["InputStandardWidth"].Value);
double UpWidth = Convert.ToDouble(mToolBlock.Inputs["InputUpWidth"].Value);
double DownWidth = Convert.ToDouble(mToolBlock.Inputs["InputDownWidth"].Value);
//尺寸显示
ArrayList xPos = new ArrayList();
ArrayList yPos = new ArrayList();
int index = 0;
MyLabel.Font = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point);
MyLabel.Alignment = CogGraphicLabelAlignmentConstants.BaselineLeft;
ArrayList vLine = new ArrayList();
ArrayList vWidth = new ArrayList();
for(i = 1;i <= 9;i++)
{
Cognex.VisionPro.Dimensioning.CogDistancePointLineTool MyTool =
(Cognex.VisionPro.Dimensioning.CogDistancePointLineTool) mToolBlock.Tools["总宽" + i.ToString()];
Cognex.VisionPro.Dimensioning.CogDistancePointLineTool MyTool2 =
(Cognex.VisionPro.Dimensioning.CogDistancePointLineTool) mToolBlock.Tools["胶线" + i.ToString()];
double LineDis = Convert.ToDouble(MyTool.Distance);
double WidthDis = Convert.ToDouble(MyTool.Distance) - Convert.ToDouble(MyTool2.Distance);
//尺寸显示
double x = Convert.ToDouble(MyTool.X);
double y = Convert.ToDouble(MyTool.Y) + 0.2;
xPos.Add(x);
yPos.Add(y);
vLine.Add(LineDis);
if(MyTool2.Distance > 0.2)
{
vWidth.Add(LineDis - 0.2);
}
else
{
vWidth.Add(WidthDis);
}
}
bool find = false;
for(i = 0;i <9;i++)
{
if( Convert.ToDouble(vLine[i]) > StandardLine + UpLine ||
Convert.ToDouble(vLine[i]) < StandardLine - DownLine)
{
mToolBlock.Outputs["Output2"].Value = vLine[i];
find = true;
index = i;
MyLabel.Color = CogColorConstants.Red;
break;
}
}
if(!find)
{
mToolBlock.Outputs["Output2"].Value = vLine[0];
MyLabel.Color = CogColorConstants.Green;
index = 0;
}
string strBuf = vLine[index].ToString();
string strData = "";
if(strBuf.Length > 4)
{
for(int m = 0;m < 4;m++)
{
strData += strBuf[m];
}
}
else
{
strData = strBuf;
}
MyLabel.SetXYText(Convert.ToDouble(xPos[index]), Convert.ToDouble(yPos[index]), strData);
MyLabel.LineWidthInScreenPixels = 2;
bool find2 = false;
for(i = 0;i < 9;i++)
{
if( Convert.ToDouble(vWidth[i]) > StandardWidth + UpWidth ||
Convert.ToDouble(vWidth[i]) < StandardWidth - DownWidth)
{
mToolBlock.Outputs["Output1"].Value = vWidth[i];
find2 = true;
break;
}
}
if(!find2)
{
mToolBlock.Outputs["Output1"].Value = vWidth[0];
}
mToolBlock.Outputs["Output1"].Value = mToolBlock.Outputs["Output2"].Value;
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 name="currentRecord">
/// 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 name="lastRecord">
/// The new last run record is available to be initialized or customized.</param>
public override void ModifyLastRunRecord(Cognex.VisionPro.ICogRecord lastRecord)
{
mToolBlock.AddGraphicToRunRecord(MyLabel, lastRecord, "CogFixtureTool1.OutputImage", "script");
}
#endregion
#region When the Script is Initialized
/// <summary>
/// Perform any initialization required by your script here
/// </summary>
/// <param name="host">The 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
}