测量出管脚的实际宽度和距离-官方实例

测量出管脚的实际宽度和距离

本篇的内容来自Halcon的官方代码

实现方法:
         画出一矩形确定ROI,将管脚信息包含在内,根据灰度值检测到管脚的边缘。再由边缘的变换次序(从灰度值从大到小和小到大) ,得出边缘对,计算边缘对的距离和相对距离,得出管脚宽度和距离。


* 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.
*以矩形两条中心轴交点作为原点,与X轴形成的角度
Row := 290
Column := 170
Phi := rad(-130)
Length1 := 60
Length2 := 10
Interpolation := 'nearest_neighbor'
*提取边缘最近的点作为参考像素
gen_measure_rectangle2 (Row, Column, Phi, Length1, Length2, Width, Height, Interpolation, MeasureHandle)
* 
* Determine all edge pairs that have a negative transition, i.e., edge pairs
* that enclose dark regions.
*高斯平滑滤波器中的参数
Sigma := 0.9
*灰度门槛值
Threshold := 12
*返回所有边缘对,如果设定为last则只回最后一对,如果为first则只回第一对
Transition := 'negative'
Select := 'all'
measure_pairs (Image, MeasureHandle, Sigma, Threshold, Transition, Select, RowEdgeFirst, ColumnEdgeFirst, AmplitudeFirst, RowEdgeSecond, ColumnEdgeSecond, AmplitudeSecond, IntraDistance, InterDistance)
* 
* 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, 'Standard measure results', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 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)
* 
* 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)
* 
* Finally, determine all edge pairs that have a negative transition and approximately the given size.
Sigma := 0.9
AmpThresh := 12
FuzzyThresh := 0.5
Transition := 'negative'
fuzzy_measure_pairs (Image, MeasureHandle, Sigma, AmpThresh, FuzzyThresh, Transition, RowEdgeFirst, ColumnEdgeFirst, AmplitudeFirst, RowEdgeSecond, ColumnEdgeSecond, AmplitudeSecond, RowEdgeCenter, ColumnEdgeCenter, 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)


猜你喜欢

转载自blog.csdn.net/liyuqian199695/article/details/78761679