Halcon之hello world焊球檢測

   焊球項目是halcon中一個經典項目,在這裏我把我學習的成果分享給大家

dev_update_window ('off')
*關閉活動窗口
dev_close_window ()
*關閉窗口
dev_open_window (0, 0, 728, 512, 'black', WindowID)
*打開一個窗口
read_image (Bond, 'die/die_03')
*讀入例程中的圖像爲Bond-
dev_display (Bond)
*顯示Bond這個圖像
set_display_font (WindowID, 14, 'mono', 'true', 'false')
*設置顯示字體
disp_continue_message (WindowID, 'black', 'true')
*顯示繼續的提示信息
stop ()
threshold (Bond, Bright, 100, 255)
*取像素值在100-255之間的像素設置爲白色
shape_trans (Bright, Die, 'rectangle2')
*圖像轉換,將Bright轉化爲DIe的白色矩形
dev_set_color ('green')
*設置顏色爲綠色
dev_set_line_width (3)
*設置行寬爲3
dev_set_draw ('margin')
*畫出邊緣
dev_display (Die)
*顯示Die這個矩形圖像
disp_continue_message (WindowID, 'black', 'true')
*在界面上顯示按F5繼續的信息
stop ()
*停止
reduce_domain (Bond, Die, DieGrey)
*求出感興趣區域,求出Bond相對於的Die的感興趣區域
threshold (DieGrey, Wires, 0, 50)
*閾值分割,選取出像素值在0-50之間的像素
fill_up_shape (Wires, WiresFilled, 'area', 1, 100)
*填充分割出來的區域
dev_display (Bond)
*顯示Bond這個圖像
dev_set_draw ('fill')
*設置畫圖的方式爲fill
dev_set_color ('red')
*設置顏色爲紅色
dev_display (WiresFilled)
*顯示二值化分割出來的圖像
disp_continue_message (WindowID, 'black', 'true')
*顯示按命令繼續的命令
stop ()
*停止
opening_circle (WiresFilled, Balls, 15.5)
*對於滿足圓形區域的地方進行開運算,效果是去掉小尾巴
dev_set_color ('green')
*設置顏色爲綠色
dev_display (Balls)
*顯示Balls這個圖像
disp_continue_message (WindowID, 'black', 'true')
*顯示按F5繼續的提示
stop ()
*停止
connection (Balls, SingleBalls)
*連接相似的區域並編號
select_shape (SingleBalls, IntermediateBalls, 'circularity', 'and', 0.85, 1.0)
*選擇圓度在0.85到1.0之間的區域
sort_region (IntermediateBalls, FinalBalls, 'first_point', 'true', 'column')
​*對於分割的區域進行排序
dev_display (Bond)
*顯示Bond這個圖像
dev_set_colored (12)
*設置顯示FinalBalls需要用到12種顏色
dev_display (FinalBalls)
*顯示恢復出來的焊球
disp_continue_message (WindowID, 'black', 'true')
*顯示提示F5以繼續的消息
stop ()
*停止
smallest_circle (FinalBalls, Row, Column, Radius)
*針對分割出來的球形區域進行最小圓檢測
NumBalls := |Radius|
*提取出Radius的個數,在這裏也可以count_obj這個函數
Diameter := 2*Radius
*直徑求取
meanDiameter := sum(Diameter)/NumBalls
*平均直徑
mimDiameter := min(Diameter)
*最小直徑
dev_display (Bond)
*顯示Bond這個圖像
disp_circle (WindowID, Row, Column, Radius)
*先圖像上畫圓
dev_set_color ('white')
for i := 1 to NumBalls by 1
    if (fmod(i,2)=1)
        disp_message (WindowID, 'D: '+Diameter[i-1], 'image', Row[i-1]-2.7*Radius[i-1], max([Column[i-1]-60,0]), 'white', 'false')
*顯示直徑
    else
        disp_message (WindowID, 'D: '+Diameter[i-1], 'image', Row[i-1]+1.2*Radius[i-1], max([Column[i-1]-60,0]), 'white', 'false')、
*顯示直徑
    endif
endfor
* dump_window (WindowID, 'tiff_rgb', './ball')
dev_set_color ('green')
dev_update_window ('on')
disp_continue_message (WindowID, 'black', 'true')
stop ()
dev_close_window ()










發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章