打開halcon,按下ctrl+e打開halcon自帶例程。工業領域->橡膠,人造材料,金屬薄片->fin.hdev
* fin.hdev: Detection of a fin
*
dev_update_window ('off')
read_image (Fins, 'fin' + [1:3])
get_image_size (Fins, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width[0], Height[0], 'black', WindowID)
set_display_font (WindowID, 14, 'mono', 'true', 'false')
for I := 1 to 3 by 1
select_obj (Fins, Fin, I)
dev_display (Fin)
*二值化處理,選中“亮”的那部分區域
binary_threshold (Fin, Background, 'max_separability', 'light', UsedThreshold)
dev_set_color ('blue')
dev_set_draw ('margin')
dev_set_line_width (4)
dev_display (Background)
disp_continue_message (WindowID, 'black', 'true')
stop ()
*閉運算,爲什麼閉運算能把那個“翼”形去掉,閉運算是先膨脹,後腐蝕。所謂膨脹就是把黑色邊緣部分用白色替換,
*當這個用作運算的卷積核足夠大,白色就把那個黑色的區域整個覆蓋掉了
*腐蝕是爲了把原來的邊緣還原
closing_circle (Background, ClosedBackground, 250)
dev_set_color ('green')
dev_display (ClosedBackground)
disp_continue_message (WindowID, 'black', 'true')
stop ()
*比較兩幅圖像,選中兩張圖像中不相同的區域
difference (ClosedBackground, Background, RegionDifference)
*開運算
opening_rectangle1 (RegionDifference, FinRegion, 5, 5)
dev_display (Fin)
dev_set_color ('red')
dev_display (FinRegion)
*獲取區域中心位置座標
area_center (FinRegion, FinArea, Row, Column)
if (I < 3)
disp_continue_message (WindowID, 'black', 'true')
stop ()
endif
endfor
毛刺圖片
剔除部分