首页 > 代码库 > 利用Halcon寻找出边缘突出的部分

利用Halcon寻找出边缘突出的部分

技术分享

寻找图中边缘突出的部分,解决思路:

1、阈值分割,找出黑色部分

2、生成一个小矩形作为StructElement,进行opening操作,即可找到如下图的部分

3、与阈值分割后的进行difference操作,即可找到突出部分,后面再进行求取突出的最边缘部分。

技术分享

技术分享

其完整代码如下:

技术分享
 1 read_image (Image, C:/Users/gongyong/Desktop/第八讲图2.bmp)
 2 decompose3 (Image, Image1, Image2, Image3)
 3 threshold (Image1, Regions, 15, 72)
 4 connection (Regions, ConnectedRegions)
 5 select_shape_std (ConnectedRegions, SelectedRegions, max_area, 70)
 6 *生成一个矩形,方向和划痕方向相同
 7 *经过opening操作后,得到去掉突出的部分
 8 orientation_region (SelectedRegions, Phi)
 9 gen_rectangle2 (Rectangle, 300, 200, Phi, 100, 1)
10 opening (SelectedRegions, Rectangle, RegionOpening)
11 
12 *提取轮廓的xld
13 gen_contour_region_xld (RegionOpening, Contours, border)
14 fit_rectangle2_contour_xld (Contours, regression, -1, 0, 0, 3, 2, Row, Column, Phi1, Length1, Length2, PointOrder)
15 
16 gen_rectangle2_contour_xld (Rectangle1, Row, Column, Phi1, Length1, Length2)
17 
18 ***矩形左边线的中点
19 lr:=Row+Length1*sin(Phi1)
20 lc:=Column-Length1*cos(Phi1)
21 gen_cross_contour_xld (Cross, lr, lc, 6, Phi1)
22 
23 ***矩形右边线的中点
24 rr:=Row-Length1*sin(Phi1)
25 rc:=Column+Length1*cos(Phi1)
26 gen_cross_contour_xld (Cross1,rr,rc,6,Phi1)
27 ***生成矩形中线
28 gen_contour_polygon_xld (Contour, [lr,rr], [lc,rc])
29 ***比Rectangle1拉长了20
30 gen_rectangle2 (Rectangle2, Row, Column, Phi1, Length1+20, Length2)
31 ***提取出多余突出来的部分
32 difference (SelectedRegions, Rectangle2, RegionDifference)
33 opening_circle (RegionDifference, RegionOpening1, 1.5)
34 connection (RegionOpening1, ConnectedRegions1)
35 select_shape (ConnectedRegions1, SelectedRegions1, area, and, 10, 99999)
36 ***
37 count_obj (SelectedRegions1, Number)
38 
39 allr:=[]
40 allc:=[]
41 RowProj:=[]
42 ColProj:=[]
43 distall:=[]
44 Contour11:=[]
45 gen_empty_obj (allline)
46 for i := 1 to Number by 1
47     select_obj (SelectedRegions1, ObjectSelected, i)
48     get_region_points (ObjectSelected, Rows, Columns)
49     distance_pl (Rows, Columns, lr, lc, rr, rc, Distance)
50     distall:=[distall,max(Distance)[0]]
51     sel:=find(Distance,max(Distance))
52     allr:=[allr,Rows[sel]]
53     allc:=[allc,Columns[sel]]
54     projection_pl (Rows[sel][0], Columns[sel][0], lr, lc, rr, rc, RP, CP)
55     RowProj:=[RowProj,RP]
56     ColProj:=[ColProj,CP]
57     gen_contour_polygon_xld (Contour1, [Rows[sel][0],RP], [Columns[sel][0],CP])
58     concat_obj (allline, Contour1, allline)
59     
60     
61     
62     
63 endfor
64 gen_cross_contour_xld (Cross2, allr, allc, 6, Phi1)
65 
66 dev_display (Image)
67 dev_display (Contour)
68 dev_display (Cross2)
69 dev_display (allline)
View Code

 

利用Halcon寻找出边缘突出的部分