HALCON示例程序inspect_bottle_mouth.hdev玻璃瓶口缺陷檢測
示例程序源碼(加注釋)
-
定義變量并初始化
SmoothX := 501
ThresholdOffset := 25
MinDefectSize := 50
PolarResolution := 640
RingSize := 70
get_system (‘store_empty_region’, StoreEmptyRegion)
set_system (‘store_empty_region’, ‘false’)
read_image (Image, ‘bottles/bottle_mouth_01’) -
關于顯示類函數解釋
dev_update_off ()
dev_close_window ()
dev_close_window ()
dev_open_window_fit_image (Image, 0, 0, 640, 512, WindowHandle1)
set_display_font (WindowHandle1, 16, ‘mono’, ‘true’, ‘false’)
dev_display (Image)
dev_set_draw (‘margin’)
dev_set_line_width (3)
dev_open_window_fit_size (0, 648, RingSize, PolarResolution, 150, 512, WindowHandle)
dev_set_draw (‘margin’)
dev_set_line_width (3)
dev_set_color (‘red’)
for Index := 1 to 16 by 1
read_image (Image, ‘bottles/bottle_mouth_’ + Index$’.02’)- 使用形態學檢測瓶口
- 使用多個閾值來分割單通道圖像
auto_threshold (Image, Regions, 2) - 選取灰度值最小的分割區域
select_obj (Regions, DarkRegion, 1) - 開運算
opening_circle (DarkRegion, RegionOpening, 3.5) - 閉運算
closing_circle (RegionOpening, RegionClosing, 25.5) - 填充孔洞
fill_up (RegionClosing, RegionFillUp) - 提取區域邊界
boundary (RegionFillUp, RegionBorder, ‘outer’) - 膨脹邊界
dilation_circle (RegionBorder, RegionDilation, 3.5) - 將膨脹邊界提取出
reduce_domain (Image, RegionDilation, ImageReduced) - 通過擬合圓形找到瓶口中心
- 關于edges_sub_pix算子解釋傳送門
edges_sub_pix (ImageReduced, Edges, ‘canny’, 0.5, 20, 40) - 分割XLD輪廓,算子解釋傳送門
segment_contours_xld (Edges, ContoursSplit, ‘lines_circles’, 5, 4, 2) - union_cocircular_contours_xld - 計算屬于同一個圓的輪廓的并集。
- union_cocircular_contours_xld(輪廓:合并的輪廓:兩個圓弧的最大角距離,兩個圓弧的最大重疊,連接線與圓弧切線之間的最大角度,兩個圓弧之間間隙的最大長度,兩個圓弧的最大半徑差,兩個圓弧的最大中心距離,是否合并沒有擬合圓的小輪廓,迭代次數:)
union_cocircular_contours_xld (ContoursSplit, UnionContours, 0.9, 0.5, 0.5, 200, 50, 50, ‘true’, 1) - XLD輪廓長度
length_xld (UnionContours, Length) - sort_index - 對元組的元素進行排序并返回已排序元組的索引。取最長輪廓
select_obj (UnionContours, LongestContour, sort_index(Length)[|Length| - 1] + 1) - 擬合圓形
fit_circle_contour_xld (LongestContour, ‘ahuber’, -1, 0, 0, 3, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder) - 使用極坐標變換將瓶口圓環展開成矩形
- 畫圓
gen_circle (Circle, Row, Column, Radius) - 膨脹
dilation_circle (Circle, RegionDilation, 5) - 腐蝕
erosion_circle (Circle, RegionErosion, RingSize - 5) - 求不同
difference (RegionDilation, RegionErosion, RegionDifference) - 縮減定義域
reduce_domain (Image, RegionDifference, ImageReduced) - polar_trans_image_ext - 將圖像進行極坐標變換。
- polar_trans_image_ext(圖片:極坐標變換圖,弧中心行坐標,弧中心列坐標,起始角度,結束角度,起始半徑,結束半徑,寬度,高度,差值方法:)
polar_trans_image_ext (ImageReduced, ImagePolar, Row, Column, 0, rad(360), Radius - RingSize, Radius, PolarResolution, RingSize, ‘nearest_neighbor’) - 使用動態閾值尋找缺陷
scale_image_max (ImagePolar, ImageScaleMax)
mean_image (ImageScaleMax, ImageMean, SmoothX, 3)
dyn_threshold (ImageScaleMax, ImageMean, Regions1, 55, ‘not_equal’)
connection (Regions1, Connection)
select_shape (Connection, SelectedRegions, ‘height’, ‘and’, 9, 99999) - 消除噪聲區域
closing_rectangle1 (SelectedRegions, RegionClosing1, 10, 20)
union1 (RegionClosing1, RegionUnion) - 反極坐標變換
polar_trans_region_inv (RegionUnion, XYTransRegion, Row, Column, 0, rad(360), Radius - RingSize, Radius, PolarResolution, RingSize, 1280, 1024, ‘nearest_neighbor’) - 顯示結果
dev_set_window (WindowHandle1)
dev_display (Image)
dev_set_color (‘blue’)
dev_display (RegionDifference)
dev_set_color (‘red’)
dev_display (XYTransRegion) - 顯示極坐標變換區域
dev_set_window (WindowHandle) - 圍繞圖像中心旋轉圖像
rotate_image (ImagePolar, ImageRotate, 90, ‘constant’)
dev_display (ImageRotate)
count_obj (RegionUnion, Number)
if (Number > 0)- 鏡像區域
mirror_region (RegionUnion, RegionMirror, ‘diagonal’, PolarResolution)
mirror_region (RegionMirror, RegionMirror, ‘row’, PolarResolution)
dev_display (RegionMirror)
disp_message (WindowHandle1, ‘Not OK’, ‘window’, -1, -1, ‘red’, ‘false’)
- 鏡像區域
else
disp_message (WindowHandle1, ‘OK’, ‘window’, -1, -1, ‘forest green’, ‘false’)
endif
if (Index < 16)
disp_continue_message (WindowHandle1, ‘black’, ‘true’)
stop ()
endif
endfor -
恢復系統參數
set_system (‘store_empty_region’, StoreEmptyRegion)
處理思路
這個例子是主要講解了使用極坐標變換進行缺陷檢測的例子。
auto_threshold 、select_obj ,自動閾值分割。
boundary提取區域邊緣edges_sub_pix、segment_contours_xld、union_cocircular_contours_xld、length_xld、select_obj 、fit_circle_contour_xld 邊界的提取、分割、合并、求長、選取、擬合。
polar_trans_image_ext極坐標變換polar_trans_region_inv反極坐標變換
mirror_region鏡像區域
后記
大家有什么問題可以向我提問哈,我看到了第一時間回復,希望在學習的路上多多結交良師益友。