Halcon检测边缘——笔记

tech2022-08-15  159

绿色:注释 褐色:控制开发 浅蓝色:外部函数——HDevelop函数文件,可在所有Halcon程序中使用 深蓝色:图像获取和处理 紫色:内部函数——本地程序函数,只在当前程序使用

做个补充:如何实现在图片上手动画出矩形和圆形来获取检测圆和检测矩形的坐标位置呢?

*显示初始化 dev_update_off () read_image (Image , 'D:/Backup/桌面/halcon选中/11.png') *read_image (Image, 'pads') get_image_size (Image, Width, Height) draw_circle (3600, Row1, Column1, Radius1) draw_circle (3600, Row2, Column2, Radius2) draw_rectangle2 (3600, Row3, Column3, Phi3, Length31, Length32) draw_rectangle2 (3600, Row4, Column4, Phi4, Length41, Length42) dev_close_window () dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle) set_display_font (WindowHandle, 14, 'mono', 'true', 'false') * *定义圆的近似位置和测量公差 RowCircle := [Row1,Row2] CircleInitRow := [RowCircle] CircleInitColumn := [Column1,Column2] gen_cross_contour_xld (Cross1, CircleInitRow, CircleInitColumn, 6, 0.785398) CircleInitRadius := [Radius1,Radius2] CircleRadiusTolerance := 20 *定义矩形的近似位置和测量公差 RectangleInitColumn := [Column3 ,Column4] RectangleInitRow := [Row3,Row4] RectangleInitPhi := [Phi3,Phi4] RectangleInitLength1 := [Length31,Length41] RectangleInitLength2 := [Length32,Length42] RectangleTolerance := 20 * *编制计量模型数据结构 create_metrology_model (MetrologyHandle) * *预先设置图像宽度是不必要的,但可以改善第一次测量的运行时间 set_metrology_model_image_size (MetrologyHandle, Width, Height) * *添加计量矩形对象到模式,正如上面定义的 add_metrology_object_rectangle2_measure (MetrologyHandle, RectangleInitRow, RectangleInitColumn, RectangleInitPhi, RectangleInitLength1, RectangleInitLength2, RectangleTolerance, 5, .5, 1, [], [], MetrologyRectangleIndices) * *将计量圆对象添加到上述定义的模型中 add_metrology_object_circle_measure (MetrologyHandle, CircleInitRow, CircleInitColumn, CircleInitRadius, CircleRadiusTolerance, 5, 1.5, 2, [], [], MetrologyCircleIndices) *可以在一个调用中度量每个度量对象的多个圆/矩形/线/椭圆实例 *因为我们喜欢测量每个对象的两个圆,所以我们将'num_instances'设置为2 set_metrology_object_param (MetrologyHandle, MetrologyCircleIndices, 'num_instances', 2) * 将“测量-过渡”设置为“均匀”确保只有一致的圆圈返回,那有要么只是从亮到暗的边缘,要么相反 * 因为一致性检查会在运行时增加,所以确实如此在默认情况下切换为 * 然而,在本例中,打开它更安全,因为正负边都存在 set_metrology_object_param (MetrologyHandle, MetrologyCircleIndices, 'measure_transition', 'uniform') * 设置最低分数可以使结果更加可靠 set_metrology_object_param (MetrologyHandle, MetrologyCircleIndices, 'min_score', .9) * *性能测量 * apply_metrology_model (Image, MetrologyHandle) * get_metrology_object_result (MetrologyHandle, MetrologyRectangleIndices, 'all', 'result_type', 'all_param', RectangleParameter) * 提取参数以提高可读性 Sequence := [0:5:|RectangleParameter| - 1] RectangleRow := RectangleParameter[Sequence] RectangleColumn := RectangleParameter[Sequence + 1] RectanglePhi := RectangleParameter[Sequence + 2] RectangleLength1 := RectangleParameter[Sequence + 3] RectangleLength2 := RectangleParameter[Sequence + 4] * *查看圆的测量结果 get_metrology_object_result (MetrologyHandle, MetrologyCircleIndices, 'all', 'result_type', 'all_param', CircleParameter) *提取参数以提高可读性 Sequence := [0:3:|CircleParameter| - 1] CircleRow := CircleParameter[Sequence] CircleColumn := CircleParameter[Sequence + 1] CircleRadius := CircleParameter[Sequence + 2] * * 显示结果 * * 得到测量轮廓 get_metrology_object_result_contour (Contours, MetrologyHandle, 'all', 'all', 1.5) * * 得到测量区域的轮廓和边缘点的坐标,作为拟合圆和矩形的基础 get_metrology_object_measures (Contour, MetrologyHandle, 'all', 'all', Row5, Column5) gen_cross_contour_xld (Cross, Row5, Column5, 6, 0.785398) * 显示所有 Color := ['gray','cyan','green'] dev_display (Image) dev_set_line_width (1) dev_set_color (Color[0]) dev_display (Contour) dev_set_color (Color[1]) dev_display (Cross) dev_set_line_width (2) dev_set_color (Color[2]) dev_display (Contours) Message := Color[2] + ': Measurement result' Message[1] := Color[1] + ': Edge candidate points' Message[2] := Color[0] + ': Measure regions' disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true') stop () * Clean up memory clear_metrology_model (MetrologyHandle)

 

最新回复(0)