大家好,我是华山自控编程朱老师
今天跟大家分享一个用C#来处理视觉间距测量的技能,下面我们讲解测量芯片引脚间距的一个实例,希望能帮助到大家。
程序运行截图如下:
下面我们就将该程序涉及的知识点进行介绍:
该程序引用了halcon库的相关视觉测量api。
具体过程如下:
1)我们需要加载原始图片:该步骤涉及到halocn的HImage类,实现代码如下:var image = new HImage(Path.GetFileName("pin.bmp"))。
2) 第二步,我们需要将RGB图像转换为灰度图像。具体为将第一步的image做个转换,实现代码如下:HImage _image = image.Rgb1ToGray();
3)第三步,我们就可以将该灰度图像显示到窗体上了。具体实现代码如下:
HWindow _window = hWindowControl1.HalconWindow;
_image.DispImage(_window);
4) 第四步,我们需要获取图像的大小,也就是宽度和高度。具体实现代码如下:_image.GetImageSize(out int width, out int height);
5) 第五步(算子核心代码)。我们将获取到的宽度width和高度height。用来提取垂直于轮廓线的直边
var measureObject = new HMeasure();
measureObject.GenMeasureRectangle2(155.5, 319.5, 0.0760436, 105.304, 8.0, width, height, "nearest_neighbor");
6)第六步(算子核心代码)我们在提取垂直于矩形或者环形弧的直边。具体实现代码如下:measureObject.MeasurePos(_image, 1, 30, "all", "all", out HTuple rowEdge, out HTuple columnEdge, out HTuple amplitude, out HTuple distance);
7)第七步,我们将寻到的中心点坐标循环输出到界面上:输出中心点直线:
_window.DispLine(rowEdge[index].D, columnEdge[index].D, rowEdge[index + 1].D, columnEdge[index + 1].D);
8)第八步,计算第七步的直线长度:
var dist = HMisc.DistancePp(rowEdge[index].D, columnEdge[index].D, rowEdge[index + 1].D, columnEdge[index + 1].D);
9)在直线附近输出第八步的长度到界面上:
_window.SetTposition(Convert.ToInt32(rowEdge[index].D + 5), Convert.ToInt32(columnEdge[index].D));
_window.WriteString(dist.ToString("N2"));
点击领取上位机编程全套入门教程+工具https://s.pdb2.com/pages/20230307/CnORDNt9HimMjNS.html
以上就是halcon测试芯片引脚间距的一个简单实例。关于第五步和第六步的两个算子gen_measure_rectangle2
和measure_pos用法,这里在详细的说明一下:
1)GenMeasureRectangle2原型
gen_measure_rectangle2( : : Row, Column, Phi, Length1, Length2, Width, Height, Interpolation : MeasureHandle)
功能
准备提取垂直于轮廓线的直边
参数列表
Row (input_control):矩形中心点的行坐标
Column (input_control):矩形中心点的列坐标
Phi (input_control):轮廓线与水平方向的夹角(弧度制)
Length1 (input_control):矩形长轴的一半
Length2 (input_control):矩形短轴的一半
Width (input_control) :待处理图像的宽度
Height (input_control) :待处理图像的高度
Interpolation (input_control) :插值方式(‘bicubic’, ‘bilinear’, ‘nearest_neighbor’)
MeasureHandle (output_control) :测量对象句柄
2)MeasurePos原型
measure_pos(Image : : MeasureHandle, Sigma, Threshold, Transition, Select : RowEdge, ColumnEdge, Amplitude, Distance)
功能
提取垂直于矩形或者环形弧的直边。
参数列表
Image (input_object) :输入图像
MeasureHandle (input_control) :测量对象句柄
Sigma (input_control) :高斯滤波参数
Threshold (input_control) :边缘强度控制阈值
Transition (input_control) :边界从白到黑还是从黑到白(分别对应’negative’, ‘positive’,也可以选择‘all’)
Select (input_control) :选择寻点的模式( 'all’输出所有点, 'first’输出第一个点, 'last’输出最后一个点)
RowEdge (output_control) :寻到直边的中心点行坐标
ColumnEdge (output_control) :寻到直边的中心点列坐标
Amplitude (output_control) :寻到直边的边缘强度值
Distance (output_control) :连续边之间的距离