本日跟大家分享一个用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"));
以上便是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) :连续边之间的间隔
免费领!
自动化掌握编程入门到开挂学习路径(附教程+软件工具)