VisionPro 学习案例 (六)
VisionPro 学习案例 (六)
案例 液位检测(VisionPro)
原图像如下:
总体流程
- 先获取模板匹配,来确定瓶子个数
- 利用卡尺工具来扫描,获取平均参考线,这个可以作为直线参数。
- 再使用模板工具的结果,获取到每一个瓶子。在每个瓶子上使用卡尺工具,来获取当前液面情况,这个通过边缘对,可以获取X与Y的坐标关系。
- 遍历每一个瓶子坐标,使用线段与直线的距离测量工具(线段使用上一步的X与Y构造水平线段),直线使用参考水平线。这样可以获得当前液面距离参考液面的距离关系。
具体实施操作
第一步 获取瓶子的参考液面关系(这里需要对图像进行预处理一下,当前液面的对比度太低了,特征不明显,为后续做准备)
第二步 制作水平参考线
使用卡尺工具,输出拟合的X与Y,不带角度,然后就能拟合出直线。
第三步 使用卡尺工具,遍历每一个匹配的安培瓶,然后输出当前的页面信息
首先拖出来一个卡尺对象
然后再开始写脚本
Step1:先获取当前工具的结果
CogPMAlignTool pma = mToolBlock.Tools["CogPMAlignTool1"] as CogPMAlignTool;
CogCaliperTool ca = mToolBlock.Tools["CogCaliperTool2"] as CogCaliperTool;
Step2:设置卡尺工具参数
这里将让卡尺的区域中心坐标是Pma工具的X与Y坐标,然后运行卡尺工具。然后个根据当前的卡尺结果的边缘对情况,制作出线段(当前液面线),注意是当前CogLineSegment这个工具
foreach(CogPMAlignResult item in pma.Results)
{
ca.Region.CenterX = item.GetPose().TranslationX;
ca.Region.CenterY = item.GetPose().TranslationY;
ca.Run();
CogLineSegment s = new CogLineSegment();
s.StartX = ca.Results.Edges[0].PositionX - 10;
s.StartY = ca.Results.Edges[0].PositionY;
s.EndX = ca.Results.Edges[0].PositionX + 10;
s.EndY = ca.Results.Edges[0].PositionY;
s.Color = CogColorConstants.Red;
s.LineWidthInScreenPixels = 3;
gc.Add(s);
}
Step3:测量当前液面线段与参考液面线之间的距离,进行OK/NG判断。
注意一点,当添加该工具以后,如果在编译的时候。出现无引用的话,需要添加引用
这里是在上面的 gc.Add(s);后面附加的。关键是Run(),让当前工具进行运行。
Dis.Segment = s;
Dis.Run();
CogGraphicLabel tLabel = new CogGraphicLabel();
if (Dis.Distance < 15)
{
tLabel.SetXYText(s.StartX, s.StartY, "OK");
tLabel.Color = CogColorConstants.Green;
}
else
{
tLabel.SetXYText(s.StartX, s.StartY, "NG");
tLabel.Color = CogColorConstants.Red;
}
gc.Add(tLabel);
代码全文如下:
#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.Caliper;
using Cognex.VisionPro.Dimensioning;
#endregion
public class CogToolBlockAdvancedScript : CogToolBlockAdvancedScriptBase
{
#region Private Member Variables
private Cognex.VisionPro.ToolBlock.CogToolBlock mToolBlock;
CogGraphicCollection gc = new CogGraphicCollection();
#endregion
/// <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);
CogPMAlignTool pma = mToolBlock.Tools["CogPMAlignTool1"] as CogPMAlignTool;
CogCaliperTool ca = mToolBlock.Tools["CogCaliperTool2"] as CogCaliperTool;
CogDistanceSegmentLineTool Dis = mToolBlock.Tools["CogDistanceSegmentLineTool1"] as CogDistanceSegmentLineTool;
gc.Clear();
foreach(CogPMAlignResult item in pma.Results)
{
ca.Region.CenterX = item.GetPose().TranslationX;
ca.Region.CenterY = item.GetPose().TranslationY;
ca.Run();
CogLineSegment s = new CogLineSegment();
s.StartX = ca.Results.Edges[0].PositionX - 10;
s.StartY = ca.Results.Edges[0].PositionY;
s.EndX = ca.Results.Edges[0].PositionX + 10;
s.EndY = ca.Results.Edges[0].PositionY;
s.Color = CogColorConstants.Red;
s.LineWidthInScreenPixels = 3;
gc.Add(s);
Dis.Segment = s;
Dis.Run();
CogGraphicLabel tLabel = new CogGraphicLabel();
if (Dis.Distance < 15)
{
tLabel.SetXYText(s.StartX, s.StartY, "OK");
tLabel.Color = CogColorConstants.Green;
}
else
{
tLabel.SetXYText(s.StartX, s.StartY, "NG");
tLabel.Color = CogColorConstants.Red;
}
gc.Add(tLabel);
}
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)
{
foreach(ICogGraphic g in gc)
{
mToolBlock.AddGraphicToRunRecord(g, lastRecord, "CogIPOneImageTool1.OutputImage", "");
}
}
#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
}
案例 液位检测(Halcon)
list_files ('./液位测量', ['files','follow_links'], ImageFiles)
tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
read_image (Image, ImageFiles[0])
get_image_size (Image, Width, Height)
gen_rectangle1 (ModelRegion, 226.536, 355.093, 299.001, 417.629)
reduce_domain (Image, ModelRegion, TemplateImage)
create_shape_model (TemplateImage, 4, rad(0), rad(360), rad(1.2228), ['none','no_pregeneration'], 'use_polarity', [15,24,4], 5, ModelID)
get_shape_model_contours (ModelContours, ModelID, 1)
dev_get_window (WindowHandle)
set_display_font (WindowHandle, 26, 'mono', 'true', 'false')
dev_update_off ()
dev_set_line_width (3)
for Index := 0 to |ImageFiles| - 1 by 1
read_image (Image, ImageFiles[Index])
dev_display (Image)
find_shape_model (Image, ModelID, rad(0), rad(360), 0.63, 15, 0.5, 'least_squares', [4,1], 0.75, Rows, Columns, Angles, Scores)
RefLevel:=mean(Rows)-160
dev_set_color ('blue')
disp_line (WindowHandle, RefLevel, 0, RefLevel, Width-1)
for I := 0 to |Rows|-1 by 1
gen_measure_rectangle2 (mean(Rows)-135, Columns[I], rad(90), 70, 18, Width, Height, 'nearest_neighbor', MeasureHandle)
measure_pos (Image, MeasureHandle, 1, 4, 'all', 'first', RowEdge, ColumnEdge, Amplitude, Distance)
if (abs(RowEdge-RefLevel)<20)
dev_set_draw ('margin')
dev_set_color ('green')
disp_rectangle2 (WindowHandle,mean(Rows)-135, Columns[I], rad(90), 70, 18)
disp_message (WindowHandle, 'OK', 'image', mean(Rows)-135, Columns[I], 'green', 'true')
else
dev_set_draw ('margin')
dev_set_color ('red')
disp_rectangle2 (WindowHandle,mean(Rows)-135, Columns[I], rad(90), 70, 18)
disp_message (WindowHandle, 'NG', 'image', mean(Rows)-135, Columns[I], 'red', 'true')
endif
endfor
wait_seconds (1)
endfor