下述代码是用于图像处理和测量的脚本,是使用HALCON软件编写的。HALCON是一款用于机器视觉的软件,广泛应用于图像分析、图像识别和机器视觉应用开发。这段脚本的主要功能是对一系列图像进行处理,以测量图像中特定对象的几何参数,例如线段的长度和角度。
dev_update_off ()
dev_close_window ()
read_image (Image, ‘diamond/diamond_01’)
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
set_display_font (WindowHandle, 16, ‘mono’, ‘true’, ‘false’)
get_image_size (Image, Width, Height)
*
- Create the metrology model data structure
create_metrology_model (MetrologyHandle) - The image size is set in advance to speed up the
- first call of apply_metrology_model.
set_metrology_model_image_size (MetrologyHandle, Width, Height) - Define the parameters of the metrology line objects
LineRow1 := [155,155]
LineColumn1 := [400,400]
LineRow2 := [290,290]
LineColumn2 := [230,570]
Tolerance := 20 - Create two metrology line objects and set parameters
add_metrology_object_line_measure (MetrologyHandle, LineRow1, LineColumn1, LineRow2, LineColumn2, Tolerance, 10, 1, 20, [], [], Index1) - Create region of interest for the alignment
gen_rectangle1 (Rectangle, LineRow1[0] - 40, LineColumn1[0] - 50, LineRow1[0] + 20, LineColumn1[0] + 50) - Change the reference coordinate system in which the
- metrology model is given to be situated at the top of the diamond
reduce_domain (Image, Rectangle, ImageReduced)
threshold (ImageReduced, Region, 128, 255)
get_region_points (Region, Rows, Columns)
set_metrology_model_param (MetrologyHandle, ‘reference_system’, [Rows[0],Columns[0],0]) - Main loop
for I := 1 to 5 by 1
read_image (Image, ‘diamond/diamond_’ + I
′
0
2
′
)
∗
R
o
u
g
h
l
y
s
e
g
m
e
n
t
t
h
e
d
i
a
m
o
n
d
′
s
p
o
s
i
t
i
o
n
r
e
d
u
c
e
d
o
m
a
i
n
(
I
m
a
g
e
,
R
e
c
t
a
n
g
l
e
,
I
m
a
g
e
R
e
d
u
c
e
d
)
t
h
r
e
s
h
o
l
d
(
I
m
a
g
e
R
e
d
u
c
e
d
,
R
e
g
i
o
n
,
128
,
255
)
∗
E
x
t
r
a
c
t
t
h
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
s
s
)
d
e
v
s
e
t
l
i
n
e
w
i
d
t
h
(
2
)
d
e
v
s
e
t
c
o
l
o
r
(
′
g
r
e
e
n
′
)
d
e
v
d
i
s
p
l
a
y
(
R
e
s
u
l
t
C
o
n
t
o
u
r
)
d
e
v
s
e
t
c
o
l
o
r
(
′
b
l
u
e
′
)
d
e
v
d
i
s
p
l
a
y
(
C
o
n
t
C
i
r
c
l
e
)
d
i
s
p
m
e
s
s
a
g
e
(
W
i
n
d
o
w
H
a
n
d
l
e
,
′
A
n
g
l
e
=
′
+
A
n
g
l
e
'02') * Roughly segment the diamond's position reduce_domain (Image, Rectangle, ImageReduced) threshold (ImageReduced, Region, 128, 255) * Extract the top of the diamond get_region_points (Region, Rows, Columns) * * Use the top of the diamond to align the metrology model in * the current image * align_metrology_model (MetrologyHandle, Rows[0], Columns[0], 0) * * * Perform the measurement for both lines in one call * apply_metrology_model (Image, MetrologyHandle) * * Access results * get_metrology_object_result (MetrologyHandle, 'all', 'all', 'result_type', 'all_param', LineParameter) angle_ll (LineParameter[0], LineParameter[1], LineParameter[2], LineParameter[3], LineParameter[4], LineParameter[5], LineParameter[6], LineParameter[7], Angle) Angle := deg(Angle) * * Display results * * Create line contours get_metrology_object_result_contour (ResultContour, MetrologyHandle, 'all', 'all', 1.5) intersection_lines (LineParameter[0], LineParameter[1], LineParameter[2], LineParameter[3], LineParameter[4], LineParameter[5], LineParameter[6], LineParameter[7], Row, Column, IsOverlapping1) * Calculate the orientation of the two lines line_orientation (LineParameter[0], LineParameter[1], LineParameter[2], LineParameter[3], Orientation1) if (Orientation1 > 0) Orientation1 := Orientation1 - rad(180) endif line_orientation (LineParameter[4], LineParameter[5], LineParameter[6], LineParameter[7], Orientation2) * * Visualize the angle between the lines gen_circle_contour_xld (ContCircle, Row, Column, 100, Orientation1, Orientation2, 'positive', 1) * Get the used measure regions and the measured points * for visualization get_metrology_object_measures (Contour, MetrologyHandle, 'all', 'all', MRow, MColumn) gen_cross_contour_xld (Cross, MRow, MColumn, 6, rad(45)) * Display everything dev_display (Image) dev_set_line_width (1) dev_set_color ('yellow') dev_display (Contour) dev_display (Cross) dev_set_line_width (2) dev_set_color ('green') dev_display (ResultContour) dev_set_color ('blue') dev_display (ContCircle) disp_message (WindowHandle, 'Angle = ' + Angle
′02′)∗Roughlysegmentthediamond′spositionreducedomain(Image,Rectangle,ImageReduced)threshold(ImageReduced,Region,128,255)∗Extractthetopofthediamondgetregionpoints(Region,Rows,Columns)∗∗Usethetopofthediamondtoalignthemetrologymodelin∗thecurrentimage∗alignmetrologymodel(MetrologyHandle,Rows[0],Columns[0],0)∗∗∗Performthemeasurementforbothlinesinonecall∗applymetrologymodel(Image,MetrologyHandle)∗∗Accessresults∗getmetrologyobjectresult(MetrologyHandle,′all′,′all′,′resulttype′,′allparam′,LineParameter)anglell(LineParameter[0],LineParameter[1],LineParameter[2],LineParameter[3],LineParameter[4],LineParameter[5],LineParameter[6],LineParameter[7],Angle)Angle:=deg(Angle)∗∗Displayresults∗∗Createlinecontoursgetmetrologyobjectresultcontour(ResultContour,MetrologyHandle,′all′,′all′,1.5)intersectionlines(LineParameter[0],LineParameter[1],LineParameter[2],LineParameter[3],LineParameter[4],LineParameter[5],LineParameter[6],LineParameter[7],Row,Column,IsOverlapping1)∗Calculatetheorientationofthetwolineslineorientation(LineParameter[0],LineParameter[1],LineParameter[2],LineParameter[3],Orientation1)if(Orientation1>0)Orientation1:=Orientation1−rad(180)endiflineorientation(LineParameter[4],LineParameter[5],LineParameter[6],LineParameter[7],Orientation2)∗∗Visualizetheanglebetweenthelinesgencirclecontourxld(ContCircle,Row,Column,100,Orientation1,Orientation2,′positive′,1)∗Gettheusedmeasureregionsandthemeasuredpoints∗forvisualizationgetmetrologyobjectmeasures(Contour,MetrologyHandle,′all′,′all′,MRow,MColumn)gencrosscontourxld(Cross,MRow,MColumn,6,rad(45))∗Displayeverythingdevdisplay(Image)devsetlinewidth(1)devsetcolor(′yellow′)devdisplay(Contour)devdisplay(Cross)devsetlinewidth(2)devsetcolor(′green′)devdisplay(ResultContour)devsetcolor(′blue′)devdisplay(ContCircle)dispmessage(WindowHandle,′Angle=′+Angle’.5’ + ‘°’, ‘window’, 12, 12, ‘black’, ‘true’)
if (I < 5)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
endif
stop ()
endfor
- Clean up memory
clear_metrology_model (MetrologyHandle)
diamond_01.png
diamond_02.png
diamond_03.png
diamond_04.png
diamond_05.png
程序运行结果如下:
以下是这段代码的主要步骤和功能:
初始化和设置:关闭设备窗口,读取第一张图像,并打开一个新的窗口来显示图像。
创建和配置度量模型:创建一个度量模型数据结构,设置图像大小,并定义度量线对象的参数。
定义感兴趣区域(ROI):生成一个矩形区域,用于对图像进行裁剪和阈值处理,以便提取图像中特定部分。
对图像进行预处理:通过裁剪和阈值处理来提取图像中的特定区域。
对度量模型进行对齐:使用图像中提取的点(例如钻石的顶部)来对齐度量模型。
应用度量模型并获取结果:对图像应用度量模型,并获取测量结果,例如线段的长度和角度。
可视化结果:在图像上绘制测量线、交叉点和圆,以可视化测量结果。
显示结果:在窗口中显示测量角度和其他信息。
主循环:对一系列图像重复上述步骤。
清理:在处理完所有图像后,清理度量模型以释放内存。
这段代码展示了如何使用HALCON进行复杂的图像处理和度量任务。