在我们的一个项目中,需要对客户的地板上的钉子找到,halcon程序写了2版,应该说比以前好了很多i,但是还是把其他不是钉子的地方找出来,由于客户地板上的钉子是机器打的,所以基本保持相同的横向和纵向距离,所以如何在代码中在最后加上这个距离的判断,使明显不对的钉子不要显示出来。代码如下所示,请查看:
- * 识别区域
- gen_rectangle1 (Rectangle, startY, startX, EndY, EndX)
- reduce_domain (Image, Rectangle, ImageReduced)
- * 初步边缘提取
- edges_image (ImageReduced, ImaAmp, ImaDir, 'canny', 1, 'nms', 10, 20)
- threshold (ImaAmp, Region1, 1, 255)
- connection (Region1, ConnectedRegions)
- select_shape (ConnectedRegions, SelectedRegions, ['outer_radius','anisometry','area'], 'and', [10,1,50], [30,3.9,300])
- shape_trans (SelectedRegions, RegionTrans, 'outer_circle')
- dilation_circle (RegionTrans, RegionDilation,5.5)
- union1 (RegionDilation, RegionUnion)
- reduce_domain (Image, RegionUnion, ImageReduced2)
- * 详细边缘提取
- edges_sub_pix (ImageReduced2, Edges, 'canny', 1, 5, 15)
- select_shape_xld (Edges, SelectedXLD, ['contlength','outer_radius'], 'and', [28,10], [99999,99999])
- segment_contours_xld (SelectedXLD, ContoursSplit, 'lines_circles', 4, 2, 2)
- * 筛选
- count_obj (ContoursSplit, NumberContours)
- gen_empty_obj (Circles)
- for i := 1 to NumberContours by 1
- select_obj (ContoursSplit, ObjectSelected, i)
- get_contour_global_attrib_xld (ObjectSelected, 'cont_approx', Attrib)
- if (Attrib == 1)
- concat_obj (Circles, ObjectSelected, Circles)
- endif
- endfor
- union_cocircular_contours_xld (Circles, UnionContours2, rad(60), rad(10), rad(30), 100, 50, 10, 'true', 1)
- select_contours_xld (UnionContours2, SelectedContours, 'contour_length', 28, 200, -0.5, 0.5)
- * 拟合圆
- count_obj (SelectedContours, NumberCircles)
- gen_empty_obj (ContCircles)
- for i := 1 to NumberCircles by 1
- select_obj (SelectedContours, ObjectSelected, i)
- fit_circle_contour_xld (ObjectSelected, 'algebraic', -1, 0, 0, 3, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder)
- gen_circle_contour_xld (ContCircle, Row, Column, Radius, 0, rad(360), 'positive', 1.5)
- GetRow[i-1] := Row
- GetCol[i-1] := Column
- concat_obj (ContCircles, ContCircle, ContCircles)
- endfor
- * 排序
- sort_contours_xld (ContCircles, SortedContours, 'upper_left', 'true', 'row')
- ResultRow := []
- ResultCol := []
- SampleRow := []
- SampleCol := []
- count_obj (SortedContours, Number2)
- j:=0
- if (Number2 >= 2)
- n := 1
- for i := 2 to Number2 by 1
- select_obj (SortedContours, Circle1, i-1)
- area_center_xld (Circle1, Area1, Row1, Column1, PointOrder1)
- select_obj (SortedContours, Circle2, i)
- area_center_xld (Circle2, Area2, Row2, Column2, PointOrder2)
- distance_pp(Row1, Column1, Row2, Column2, Distance)
- * disp_line(PointOrder2, Row1, Column1, Row2, Column2)
- if (i == 2)
- ResultRow[0] := Row1
- ResultCol[0] := Column1
- endif
- if (Distance > 10)
- ResultRow[n] := Row2
- ResultCol[n] := Column2
- n := n+1
- endif
- j:=j+1
- endfor
- endif
- return ()
复制代码 |