halcon例程 -- 逐字细究(二)

* This example program demonstrates the basic usage of a fuzzy measure object.
* Here, the task is to determine the width of and the distance between the
* pins of a switch.
* 根据图片大小,测量出管脚的实际宽度和距离。
* First, read in the image and initialize the program.
read_image (Image, 'bin_switch/bin_switch_2')
*读入图像
get_image_size (Image, Width, Height)
*获取图像信息:返回宽度,和高度
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
*创建大小适合的窗体
dev_resize_window_fit_size (0, 0, Width, Height, -1, 640)
**按指定图像的尺寸来打开一个窗口
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
**设置显示的字体
dev_display (Image)
**显示图像

* Define the rectangular ROI within which the edges will be detected
* and create the measure.


Row := 290 
*输入矩形中心点的y值坐标
Column := 170
*输入矩形中心点的x值坐标
Phi := rad(-130)
*输入矩形长轴针对水平方向的角度
Length1 := 60
*输入矩形长轴半径
Length2 := 10
*输入矩形短轴半径(Length2<=Length1)
Interpolation := 'nearest_neighbor'
*提取边沿的最近点作为参考像素
gen_measure_rectangle2 (Row, Column, Phi, Length1, Length2, Width, Height, Interpolation, MeasureHandle)
**提取与矩形垂直中心轴线的直线边缘,设定ROI,返回输出MeasureHandle,即输出被测量尺寸物体对应的句柄

* Determine all edge pairs that have a negative transition, i.e., edge pairs
* that enclose dark regions.


Sigma := 0.9
*高斯滤波系数,范围:0.4 <= sigma <= 100
Threshold := 12
*灰度阈值,范围:1 <= Threshold <= 255
Transition := 'negative'
*选择检测过渡边沿的灰度值方式,其中negative是从白到黑
Select := 'all'
*返回所有边缘对,其中all是全部返回
measure_pairs (Image, MeasureHandle, Sigma, Threshold, Transition, Select, RowEdgeFirst, ColumnEdgeFirst, AmplitudeFirst, RowEdgeSecond, ColumnEdgeSecond, AmplitudeSecond, IntraDistance, InterDistance)
**提取与矩形或环状弧垂直的直线边缘。
*Image 输入图像
*MeasureHandle 输入测量句柄
*RowEdgeFirst 输出匹配边沿起点的y值坐标
*ColumnEdgeFirst 输出匹配边沿起点的x值坐标
*AmplitudeFirst 输出匹配边沿起点的幅值
*RowEdgeSecond 输出匹配边沿终点的y值坐标
*ColumnEdgeSecond 输出匹配边沿终点的x值坐标
*AmplitudeSecond 输出匹配边沿终点的幅值
*IntraDistance  输出匹配起点和终点的距离
*InterDistance  输出匹配终点到下一匹配起点之间的距离 
* Visualize the results


dev_display (Image)
*显示图像
dev_set_draw ('margin')
*设置一个区域的有效填充
*DrawMode:有效的填充类型,fill,即填满;margin,即只显示边沿,可能会受设置线宽和类型的影响
dev_set_color ('black')
*设置一个颜色用在某个显示区域,图形或XLD,该颜色也可以用于今后打开的图形窗口,它的有效时间为直到下次该函数定义的其他有效颜色
*ColorName 有效的颜色名称,默认为‘white’
gen_rectangle2 (Rectangle, Row, Column, Phi, Length1, Length2)
*创建任意矩形区域
*Rectangle 输出矩形区域
*Row 输入矩形中心点的y值坐标
*Column 输入矩形中心点的x值坐标
*Phi 输入矩形长轴针对水平方向的角度 ,限制在(-PI/2)< Phi && Phi <= (PI/2)范围
*Length1 输入矩形长轴半径
*Length2 输入矩形短轴半径(Length2<=Length1)
p_disp_dimensions (RowEdgeFirst, ColumnEdgeFirst, RowEdgeSecond, ColumnEdgeSecond, IntraDistance, InterDistance, Phi, Length2, WindowHandle)
* 显示所测量的相临边缘对之间的距离尺寸
disp_message (WindowHandle, 'Standard measure results', 'window', 12, 12, 'black', 'true')
* 在此程序写入一个文本消息,内容为'Standard measure results'
disp_continue_message (WindowHandle, 'black', 'true')
*此过程显示Press Run (F5) to continue 在右下角,作为提示信息
stop ()
*程序停止运行,按F5可以继续往下运行
* Due to the reflections on the middle pin, its width cannot be determined correctly.

* As we know that the pins appear approximately 9 pixels wide, it is very easy to expand the measure to a
* fuzzy measure that will only return pairs that have a width of approximately the given size.

* First, create a fuzzy function that returns 1.0 for the given pair size and 0.0 for values that
* deviate more than 2 pixels from the given pair size.
create_funct_1d_pairs ([7,9,11], [0.0,1.0,0.0], SizeFunction)
*从(X,Y)的集合中创建一个函数
*XValues 创建该函数对应的x值
*YValues 创建该函数对应的y值
*SizeFunction 输出该函数

* Then, expand the measure to a fuzzy measure that will return only pairs of approximately the given size.
SetType := 'size'
set_fuzzy_measure (MeasureHandle, SetType, SizeFunction)
* 指定一个模糊元函数
*MeasureHandle 输入测量尺寸句柄
*SetType 设置模糊的类型
*SizeFunction 输入模糊成员函数


* Finally, determine all edge pairs that have a negative transition and approximately the given size.
Sigma := 0.9
*高斯滤波系数,范围:0.4 <= sigma <= 100
AmpThresh := 12
*最小的边沿灰度调整,范围:1 <= AmpThresh <= 255
FuzzyThresh := 0.5
*最小的模糊测量配对值 范围: 0.0 <= FuzzyThresh <= 1.0
Transition := 'negative'
*选择检测过渡边沿的灰度值方式,其中negative是从白到黑
Select := 'all'
*返回所有边缘对,其中all是全部返回
fuzzy_measure_pairs (Image, MeasureHandle, Sigma, AmpThresh, FuzzyThresh, Transition, RowEdgeFirst, ColumnEdgeFirst, AmplitudeFirst, RowEdgeSecond, ColumnEdgeSecond, AmplitudeSecond, RowEdgeCenter, ColumnEdgeCenter, FuzzyScore, IntraDistance, InterDistance)
* 模糊测量配对,提取与矩形或环状弧垂直的直线边缘的起点、终点、中间点的坐标值,并提供起点和终点两点之间的间距,终点至下一配对起点的间距
*Image 输入图像
*MeasureHandle 输入测量句柄
*RowEdgeFirst 输出匹配边沿起点的y值坐标
*ColumnEdgeFirst 输出匹配边沿起点的x值坐标
*AmplitudeFirst 输出匹配边沿起点的幅值
*RowEdgeSecond 输出匹配边沿终点的y值坐标
*ColumnEdgeSecond 输出匹配边沿终点的x值坐标
*AmplitudeSecond 输出匹配边沿终点的幅值
*RowEdgeCenter 输出匹配边沿中点的y值坐标
*ColumnEdgeCenter 输出匹配边沿中点的x值坐标
*FuzzyScore 输出匹配边沿的模糊评估值
*IntraDistance  输出匹配起点和终点的距离
*InterDistance  输出匹配终点到下一匹配起点之间的距离 
* And again, visualize the results
dev_display (Image)
dev_set_draw ('margin')
dev_set_color ('black')
gen_rectangle2 (Rectangle, Row, Column, Phi, Length1, Length2)
p_disp_dimensions (RowEdgeFirst, ColumnEdgeFirst, RowEdgeSecond, ColumnEdgeSecond, IntraDistance, InterDistance, Phi, Length2, WindowHandle)
disp_message (WindowHandle, 'Fuzzy measure results', 'window', 12, 12, 'black', 'true')

* Free the memory that has been allocated for the measure.
close_measure (MeasureHandle)
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值