在Halcon中,區(qū)域的特征Roundness(圓度)和Circularity(圓度)雖然都用于描述區(qū)域與圓形之間的相似程度,但它們在計算方法和應(yīng)用上存在一些區(qū)別。還是從幫助文檔著手:
1、Roundness(圓度)

機翻:

- 計算方法:Roundness通常通過計算區(qū)域輪廓上各點到區(qū)域中心的平均距離(Distance)與這些距離的標準差(Sigma)之間的關(guān)系來得到。這種計算方法更多地考慮了輪廓點到中心的分布情況(即側(cè)重于輪廓的均勻性),從而評估區(qū)域形狀的圓形程度;Roundness值越接近1,表示區(qū)域的形狀越接近圓形。
幫助文檔中還列舉出了計算公式,但是比較復雜,難以用Halcon腳本來復現(xiàn):

雖然搞不太清楚,但是沒有關(guān)系,我研究以后發(fā)現(xiàn)特征篩選中使用“Circularity”來篩選,可能更符合常見項目的篩選目的。
2、Circularity(圓度)
- 計算方法:Circularity通常是通過區(qū)域的面積(F)和近似最小外接圓的面積的比值來計算。Circularity值越接近1,表示區(qū)域的形狀越接近圓形。
幫助文檔中描述了它的計算方法:

上圖公式中,max是區(qū)域中心點到輪廓所有像素的最大距離,所以(max^2 * π)差不多近似等于區(qū)域最小外接圓的面積。
寫了這么多,其實還是不太容易看出Roundness和Circularity在應(yīng)用中的區(qū)別,我寫一個腳本程序讓大家直觀地看出這2個特征的差異:

1 dev_set_draw ('margin')
2 dev_set_line_width (1)
3 dev_get_window (WindowHandle)
4 set_display_font (WindowHandle, 15, 'Courier', 'true', 'false')
5
6 read_image (Image, '各種圖形.png')
7 threshold (Image, Region, 0, 55)
8 opening_circle (Region, RegionOpening, 1) //去掉可能的毛刺
9 connection (RegionOpening, ConnectedRegions)
10 *將多個輪廓按“column”從左到右排列
11 sort_region (ConnectedRegions, SortedRegions, 'first_point', 'true', 'column')
12
13 *獲得Roundness、Circularity屬性值
14 roundness (SortedRegions, Distance, Sigma, Roundness, Sides)
15 circularity (SortedRegions, Circularity)
16
17 dev_display (Image)
18 disp_message (WindowHandle, 'Roundness:', 'image', 30, 5, 'red', 'false')
19 disp_message (WindowHandle, 'Circularity:', 'image', 90, 5, 'red', 'false')
20 disp_message (WindowHandle, '計算近似值:', 'image', 120, 5, 'black', 'false')
21
22
23 for i := 0 to |Roundness|-1 by 1
24 *兩值差異>30%的,用藍色字顯示
25 bili := abs(Roundness[i] - Circularity[i]) / max2(Roundness[i] , Circularity[i])
26 if (bili > 0.3)
27 color := 'blue'
28 else
29 color := 'black'
30 endif
31 disp_message (WindowHandle, Roundness[i] $'.2f', 'image', 30, 168 + 104 * i, color, 'false')
32 disp_message (WindowHandle, Circularity[i] $'.2f', 'image', 90, 168 + 104 * i, color, 'false')
33
34 select_obj (SortedRegions, CurrentRoi, i + 1)
35 gen_contour_region_xld (CurrentRoi, Contours, 'border')
36 area_center (CurrentRoi, Area, Row, Column)
37 distance_pc (Contours, Row, Column, DistanceMin, DistanceMax)
38
39 * 公式:C1 = F / (max * max * π)
40 C1 := Area /(DistanceMax * DistanceMax * 3.14159)
41 C := min2(1, C1)
42
43 disp_message (WindowHandle, '(' + C $'.2f' + ')', 'image', 125, 159 + 104 * i, color, 'false')
44 endfor

上圖可以看出,用公式“C1 = F / (max * max * π)”計算出的Circularity值(C1),與算子“circularity (SortedRegions, Circularity)”得出的值基本相同。也就驗證了該公式的解讀是正確的。
同時可以得出結(jié)論:
① 缺陷檢測項目使用blob分析時,如果想用圓度來篩選圓形的斑點,使用特征Circularity顯然更加合理;
② 至于Roundness特征,當區(qū)域是“正方形、窄邊圓環(huán)、N角星”時,Roundness都很接近1,但是它們顯然不像“圓形斑點”;
③ “窄邊圓環(huán)”的Circularity值很小、Roundness值很大,這一規(guī)律有助于識別出“窄邊圓環(huán)”。
--------------------------------------------
本文系原創(chuàng),轉(zhuǎn)載請注明出處。
如果文章對您有幫助,可以點擊下方的【好文要頂】或【關(guān)注我】;如果您想進一步表示感謝,可通過網(wǎng)頁右側(cè)的【打賞】功能進行打賞。
感謝您的支持,我會繼續(xù)寫出更多的相關(guān)文章!文章有不理解的地方歡迎跟帖交流,博主經(jīng)常在線!^_^
浙公網(wǎng)安備 33010602011771號