* 檢測瓶口缺陷
*
*
* tuning parameters
SmoothX := 501
ThresholdOffset := 25
MinDefectSize := 50
*
* initialization
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')
*激活WindowHandle1窗口
dev_set_window(WindowHandle1)
* Main loop
*
* Detect defects in bottle necks
for Index := 2 to 16 by 1
*讀取一張圖像
read_image (Image, 'bottles/bottle_mouth_'+Index$'.02')
*顯示圖像
dev_display (Image)
*自動閾值
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)
*
* 運用CANNY算法進行邊緣探測
edges_sub_pix (ImageReduced, Edges, 'canny', 0.5, 20, 40)
*對邊緣輪廓分割成直線和圓
segment_contours_xld (Edges, ContoursSplit, 'lines_circles', 5, 4, 2)
*將共圓上的輪廓連接起來
union_cocircular_contours_xld (ContoursSplit, UnionContours, 0.9, 0.5, 0.5, 200, 50, 50, 'true', 1)
*獲取輪廓的長度
length_xld (UnionContours, Length)
*對長度數值進行降序排列,并獲取數值最大的長度值
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)
*
* Part 2: Transform the ring-shaped bottle neck region to a rectangle
*生成擬合圓
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 (ImageReduced, ImagePolar, Row, Column, PolarResolution, Radius+5)
*
* Part 3: Find defects with a dynamic threshold
* Note the strong smoothing in x-direction in the transformed image.
*剪切矩形區域里的圖像
crop_part (ImagePolar, ImagePart, Radius-RingSize, 0, PolarResolution, RingSize)
*將最大灰度值在0-255范圍拉伸
scale_image_max (ImagePart, ImageScaleMax)
*對灰度拉伸的圖像進行均值濾波
mean_image (ImageScaleMax, ImageMean, SmoothX, 3)
*局部閾值處理
dyn_threshold (ImageScaleMax, ImageMean, Regions1, 50, '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, 6.28319, 0, Radius-RingSize, Radius, 640, RingSize, 1280, 1024, 'nearest_neighbor')
*
* Part 4: Display results
* 激活窗口WindowHandle1
dev_set_window (WindowHandle1)
dev_display (Image)
dev_set_color ('blue')
dev_display (RegionDifference)
dev_set_color ('red')
dev_display (XYTransRegion)
* display polar transformed inspected region with results
* The image and resulting region are rotated by 90 degrees
* only for visualization purposes! (I.e. to fit better on the screen)
* The rotation is NOT necessary for the detection algorithm.
*激活窗口WindowHandle
dev_set_window (WindowHandle)
*旋轉圖像
rotate_image (ImagePart, 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
dev_set_window (WindowHandle1)
endfor
* 恢復存儲空區域的狀態
set_system ('store_empty_region', StoreEmptyRegion)