创建一个可变形模型,然后使用这个模型在一系列图像中搜索特定的模式,并评估搜索结果 - 显示模型图像和轮廓:在第一个窗口中显示模型图像和轮廓,并显示相关信息

📅 2026-11-30 👤 拼多多助力营销技巧 📂 拼多多助力2027 👁️ 363 次阅读

这段代码是使用HALCON软件编写的,HALCON是一个用于机器视觉和图像分析的高级软件包。代码的主要目的是创建一个可变形模型,然后使用这个模型在一系列图像中搜索特定的模式,并评估搜索结果。

dev_update_off () Smoothness := 25 read_image (ModelImage, ‘gasket/gasket_model’) read_image (Image, ‘gasket/gasket_01’) dev_close_window () dev_open_window_fit_image (ModelImage, 0, 0, 500, -1, WindowHandle1) set_display_font (WindowHandle1, 16, ‘mono’, ‘true’, ‘false’) get_window_extents (WindowHandle1, Row, Column, Width, Height) dev_open_window_fit_image (Image, 0, Width + 12, 1024 - Width - 36, -1, WindowHandle2) set_display_font (WindowHandle2, 16, ‘mono’, ‘true’, ‘false’) *

Create variation model sobel_amp (ModelImage, EdgeAmplitude, ‘thin_max_abs’, 5) create_variation_model (425, 410, ‘byte’, ‘direct’, VariationModelID) prepare_direct_variation_model (ModelImage, EdgeAmplitude, VariationModelID, 30, 1.5)Create locally deformable model create_local_deformable_model (ModelImage, ‘auto’, rad(-10), rad(20), ‘auto’, 0.9, 1.1, ‘auto’, 0.9, 1.1, ‘auto’, ‘none’, ‘use_polarity’, ‘auto’, ‘auto’, [], [], ModelID) get_deformable_model_contours (ModelContours, ModelID, 1) area_center (ModelImage, Area, Row, Column) hom_mat2d_identity (HomMat2DIdentity) hom_mat2d_translate (HomMat2DIdentity, Row, Column, HomMat2DTranslate) affine_trans_contour_xld (ModelContours, ContoursAffinTrans, HomMat2DTranslate) dev_set_window (WindowHandle1) dev_set_line_width (2) dev_set_color (‘yellow’) dev_display (ModelImage) dev_display (ContoursAffinTrans) disp_message (WindowHandle1, ‘Model image and contours’, ‘window’, 12, 12, ‘black’, ‘true’) disp_continue_message (WindowHandle1, ‘black’, ‘true’) stop ()Process images iteratively NumImages := 7 for Index := 1 to NumImages by 1 read_image (Image, ‘gasket/gasket_’ + Index$‘02’) dev_set_window (WindowHandle2) dev_display (Image) disp_message (WindowHandle2, ‘Search …’, ‘window’, 12, 12, ‘black’, ‘true’)

Find the model in the search image.As result, the rectified image, the respectivevector field, and the found contours are queried. count_seconds (S1) find_local_deformable_model (Image, ImageRectified, VectorField, DeformedContours, ModelID, rad(-10), rad(20), 1, 1, 1, 1, 0.93, 1, 0.7, 0, 0.4, [‘image_rectified’,‘vector_field’,‘deformed_contours’], [‘deformation_smoothness’,‘expand_border’,‘subpixel’], [Smoothness,0,1], Score, Row, Column) count_seconds (S2) Time := S2 - S1 if (|Score| > 0) gen_warped_mesh_region (VectorField, MeshRegion, Smoothness) gen_region_contour_xld (DeformedContours, EdgeRegion, ‘margin’) dilation_circle (EdgeRegion, RegionDilation, 2 * Smoothness) intersection (RegionDilation, MeshRegion, RegionIntersection) dev_set_line_width (1) dev_set_color (‘yellow’) dev_display (RegionIntersection) Found[Index] := |Score| dev_set_line_width (2) dev_set_color (‘green’) dev_display (DeformedContours) disp_message (WindowHandle2, [‘Match found in ’ + Time

′

1.2

f

′

+

′

s

′

,

′

S

c

o

r

e

:

′

+

S

c

o

r

e

'1.2f' + ' s','Score: ' + Score

′1.2f′+′s′,′Score:′+Score’.2f’], ‘window’, 12, 12, ‘black’, ‘true’) dev_set_window (WindowHandle1) dev_display (ImageRectified) compare_variation_model (ImageRectified, Region, VariationModelID) connection (Region, ConnectedRegions) select_shape (ConnectedRegions, SelectedRegions, ‘area’, ‘and’, 30, 99999) count_obj (SelectedRegions, Number) if (Number > 0) area_center (SelectedRegions, Area, Row1, Column1) elliptic_axis (SelectedRegions, Ra, Rb, Phi) tuple_gen_const (Number, 1, Ones) PointOrder := [] for Idx := 0 to Number - 1 by 1 PointOrder := [PointOrder,‘positive’] endfor gen_ellipse_contour_xld (ContEllipse, Row1, Column1, Phi, Ra + 10, Rb + 10, 0 * Ones, 6.28318 * Ones, PointOrder, 1.5) dev_set_color (‘red’) dev_display (ContEllipse) disp_message (WindowHandle1, ‘Part not OK!’, ‘window’, 12, 12, ‘red’, ‘true’) else disp_message (WindowHandle1, ‘Part OK’, ‘window’, 12, 12, ‘forest green’, ‘true’) endif else disp_message (WindowHandle2, ‘Nothing found’, ‘window’, 12, 12, ‘black’, ‘true’) endif if (Index < NumImages) disp_continue_message (WindowHandle2, ‘black’, ‘true’) stop () endif endfor Clean up clear_deformable_model (ModelID) clear_variation_model (VariationModelID)

gasket_model图片: gasket_01图片: 以下是待检测的图片: gasket_01.png~gasket_07.png 以下是程序运行结果:

以下是代码的主要步骤和功能:

初始化和窗口设置:设置平滑度(Smoothness)为25,读取模型图像(ModelImage)和第一张搜索图像(Image),然后关闭和重新打开窗口以适应图像大小。

创建变体模型:使用sobel_amp函数提取模型图像的边缘幅度,然后创建并准备一个直接变体模型(VariationModelID)。

创建局部可变形模型:使用create_local_deformable_model函数创建一个局部可变形模型(ModelID),并获取其轮廓。

显示模型图像和轮廓:在第一个窗口中显示模型图像和轮廓,并显示相关信息。

迭代处理图像:循环处理指定数量的图像(NumImages),对每张图像执行以下操作:

读取图像。 显示图像并提示正在搜索。 使用find_local_deformable_model函数在图像中搜索模型,获取结果(如校正后的图像、向量场、变形轮廓)。 计算搜索时间并评估搜索得分。 如果找到匹配项,生成变形区域、边缘区域,并在窗口中显示相关信息。 如果匹配得分大于,进一步处理以检查部件是否合格。 评估部件合格性:使用compare_variation_model函数比较校正后的图像,然后使用select_shape函数选择特定区域,并评估这些区域的中心和椭圆轴。如果找到的区域数量大于,生成一个椭圆轮廓并在窗口中显示,表示部件不合格。

清理:在处理完所有图像后,清理可变形模型和变体模型。

代码中使用了多个HALCON函数,如read_image、dev_open_window_fit_image、set_display_font、get_window_extents、dev_set_window、dev_display、disp_message等,用于图像读取、窗口管理、显示和信息提示。

请注意,这段代码是一个示例,可能需要根据实际应用场景进行调整。例如,图像路径、模型参数和搜索条件可能需要根据具体任务进行修改。

创建一个可变形模型,然后使用这个模型在一系列图像中搜索特定的模式,并评估搜索结果

精彩内容 拼多多帮助力入口

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧