模板匹配 - 卡尺工具找圆
金属表面圆孔的识别与定位
步骤:
1. 模板匹配的方式找到圆孔
图片质量不好,对比度较差,需要对图像进行预处理,使图像变得更清楚,对比度变强;
illuminate,equ_histo_image
模板匹配获取圆孔的的轮廓
2. 由第一步获取轮廓,拟合一个圆形,再通过卡尺工具得到圆孔的圆,得到圆心坐标,即可实现精定位。
1 list_files('E:/学习/Halcon/模板匹配/金属圆环', 'files', Files) 2 tuple_regexp_select(Files, '.*', Selection) 3 4 read_image(Image, Files[0]) 5 6 *图像预处理 7 illuminate(Image, ImageIlluminate, 101, 101, 0.7) 8 equ_histo_image(ImageIlluminate, ImageEquHisto) 9 dev_set_draw('margin') 10 gen_circle (ROI_0, 657.5, 516.629, 38.984) 11 12 reduce_domain(ImageEquHisto, ROI_0, ImageReduced) 13 inspect_shape_model(ImageReduced, ModelImages, ModelRegions, 4, 40) 14 15 create_shape_model(ImageReduced, 4, -0.39, 0.79, 'auto', 'auto', 'use_polarity', 40, 5, ModelID) 16 get_shape_model_contours(ModelContours, ModelID, 1) 17 find_shape_model(Image, ModelID, -0.39, 0.78, 0.5, 1, 0.5, 'least_squares', 0, 0.9, Row, Column, Angle, Score) 18 19 vector_angle_to_rigid(0, 0, 0, Row, Column, 0, HomMat2D) 20 affine_trans_contour_xld(ModelContours, ContoursAffinTrans, HomMat2D) 21 dev_clear_window() 22 dev_display(Image) 23 dev_display(ContoursAffinTrans) 24 for I := 0 to |Files|-1 by 1 25 read_image(Image, Files[I]) 26 illuminate(Image, ImageIlluminate, 101, 101, 0.7) 27 equ_histo_image(ImageIlluminate, ImageEquHisto) 28 find_shape_model(ImageEquHisto, ModelID, -0.39, 0.78, 0.5, 1, 0.5, 'least_squares', 0, 0.9, Row, Column, Angle, Score) 29 30 vector_angle_to_rigid(0, 0, 0, Row, Column, 0, HomMat2D) 31 affine_trans_contour_xld(ModelContours, ContoursAffinTrans, HomMat2D) 32 33 dev_clear_window() 34 dev_display(ImageEquHisto) 35 dev_display(ContoursAffinTrans) 36 37 union_adjacent_contours_xld(ContoursAffinTrans, UnionContours, 100, 50, 'attr_keep') 38 dev_set_color('green') 39 dev_display(UnionContours) 40 length_xld(UnionContours, Length) 41 tuple_max(Length, Max) 42 tuple_find(Length, Max, Indices) 43 44 select_obj(UnionContours, ObjectSelected, Indices+1) //获取外圆的轮廓 45 dev_clear_window() 46 dev_display(ImageEquHisto) 47 fit_circle_contour_xld(ObjectSelected, 'algebraic', -1, 0, 0, 3, 2, CircleRow, CircleColumn, CircleRadius, StartPhi, EndPhi, PointOrder) 48 gen_circle(Circle, CircleRow, CircleColumn, CircleRadius) 49 dev_display(Circle) 50 51 create_metrology_model(MetrologyHandle) 52 CircleParam :=[CircleRow, CircleColumn, CircleRadius] 53 add_metrology_object_generic(MetrologyHandle, 'circle', CircleParam, 10, 5, 1, 30, [], [], Index) 54 55 set_metrology_object_param(MetrologyHandle, 'all', 'measure_transition', 'positive') 56 57 apply_metrology_model(ImageEquHisto, MetrologyHandle) 58 get_metrology_object_measures(Contours, MetrologyHandle, 'all', 'all', Row1, Column1) 59 get_metrology_object_result(MetrologyHandle, 'all', 'all', 'result_type', 'all_param', Parameter) 60 get_metrology_object_result_contour(Contour, MetrologyHandle, 'all', 'all', 1.5) 61 gen_cross_contour_xld(Cross, Row1, Column1, 20, Angle) 62 63 dev_clear_window() 64 dev_display(ImageEquHisto) 65 dev_set_color('green') 66 dev_display(Contours) 67 dev_set_color('blue') 68 dev_display(Contour) 69 dev_set_color('red') 70 dev_display(Cross)
71 clear_metrology_model(MetrologyHandle) 72 stop() 73 endfor
运行结果: