halcon極坐標轉換與亮暗缺陷檢測結合的案例(轉)


 3 dev_set_draw ('margin')
 4 dev_set_line_width (2)
 5 set_font (3600, '-Courier New-16-*-*-*-*-1-')
 6 
 7 list_files ('C:/Users/Administrator/Desktop/bottle', ['files','follow_links','recursive'], ImageFiles)
 8 tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
 9 
10 for Index := 1 to |ImageFiles| - 1 by 1
11     read_image (Image, ImageFiles[Index])
12     get_image_size (Image, Width, Height)
13     threshold (Image, Region, 0, 60)
14     fill_up (Region, RegionFillUp)
15     opening_circle (RegionFillUp, RegionOpening, 8.5)
16     smallest_circle (RegionOpening, Row0, Column0, Radius0)
17  
18     *下面這個函數是自己寫的抓圓的函數,細節不表。你也可以用fit_circle_contour_xld  gen_circle_contour_xld實現類似功能
19     find_circle (Image, PartCircleXLD, Regions, Cross, Circle, Row0, Column0, Radius0 + 10, 0, 360, 30, 70, 20, 1, 50, 'negative', 'first', 'inner', 10, 'circle', RowCenter, ColCenter, Radius)
20     dev_display (Image)
21     dev_display (Circle)
22     
23     *該算子對一個圖像的圓弧區域進行極坐標變換,圓弧外徑是Radius,內徑是Radius - 100,即圓弧厚度是100
24     *同理,圓弧展開成矩形后,矩形寬度應該是外弧圓圈的周長,即6.28319 * Radius(周長 = 2π × r) ;矩形高度應該是圓弧厚度,即100
25     polar_trans_image_ext (Image, PolarTransImage, RowCenter, ColCenter, 0, 6.28319, Radius - 100, Radius, 6.28319 * Radius, 100, 'nearest_neighbor')
26     
27     *下面這句僅用於觀察image的反向極坐標變換,生成的圖片的寬高還是設置為最原始圖像的Width, Height
28     polar_trans_image_inv  (PolarTransImage, XYTransImage, RowCenter, ColCenter, 0, 6.28319, Radius - 100, Radius, Width, Height, 'nearest_neighbor')
29     
30     *mean_image選擇主要沿水平方向進行模糊,動態閾值的'not_equal'參數同時篩選出了跟周圍比過亮和過暗的區域(因為過暗和過亮都是缺陷)
31     mean_image (PolarTransImage, ImageMean, 500, 3)
32     dyn_threshold (PolarTransImage, ImageMean, Region1, 30, 'not_equal')
33     
34     fill_up_shape (Region1, RegionFillUp1, 'area', 1, 100)
35     *開運算去掉細小干擾
36     opening_circle (RegionFillUp1, RegionOpening1, 1.5)
37     connection (RegionOpening1, ConnectedRegions)
38     
39     *之所以要進行極坐標轉換,就是為了這里用'height'來篩選,這是本例使用極坐標變換最關鍵的原因
40     select_shape (ConnectedRegions, SelectedRegions, 'height', 'and', 10, 99999)
41     polar_trans_region_inv (SelectedRegions, XYTransRegion, RowCenter, ColCenter, 0, 6.28319, Radius - 100, Radius, 6.28319 * Radius, 100, Width, Height, 'nearest_neighbor')
42     dev_display (Image)
43     dev_display (XYTransRegion)
44     
45     stop ()
46 endfor


雖然極坐標變換是本例中的核心思路,但是仍有三句非常巧妙的代碼,仔細想想為什么這三句代碼很巧妙:

31 mean_image (PolarTransImage, ImageMean, 500, 3) 32 dyn_threshold (PolarTransImage, ImageMean, Region1, 30, 'not_equal')
40     select_shape (ConnectedRegions, SelectedRegions, 'height', 'and', 10, 99999)

原文連接 https://www.cnblogs.com/xh6300/p/10406753.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM