HALCON示例程序forest.hdev識別森林中的樹
示例程序源碼(加注釋)
- 關于顯示類函數解釋
dev_close_window ()
dev_update_window (‘off’)
read_image (Forest, ‘forest_air1’)
get_image_size (Forest, Width, Height)
dev_open_window (0, 0, Width, Height, ‘black’, WindowHandle)
decompose3 (Forest, Red, Green, Blue)
dev_display (Red) - 二值化
threshold (Blue, BlueBright, 80, 255) - 分割連通域
connection (BlueBright, BlueBrightConnection) - 通過面積進行區域篩選
select_shape (BlueBrightConnection, Path, ‘area’, ‘and’, 100, 100000000)
dev_set_color (‘red’)
dev_set_draw (‘margin’)
dev_display (Path)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop () - 二值化紅色通道圖像
threshold (Red, RedBright, 120, 255) - 分割連通域
connection (RedBright, RedBrightConnection) - 使用面積進行區域篩選
select_shape (RedBrightConnection, RedBrightBig, ‘area’, ‘and’, 1500, 10000000) - 使用圓形元素進行閉運算
closing_circle (RedBrightBig, RedBrightClosing, 7.5) - 使用圓形元素進行開運算
opening_circle (RedBrightClosing, RedBrightOpening, 9.5) - 分割連通域
connection (RedBrightOpening, RedBrightOpeningConnection) - 使用面積進行區域篩選
select_shape (RedBrightOpeningConnection, BeechBig, ‘area’, ‘and’, 1000, 100000000) - 使用區域的平均灰度進行區域篩選
select_gray (BeechBig, Blue, Beech, ‘mean’, ‘and’, 0, 59)
dev_display (Red)
dev_display (Beech)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop () - 聯合區域
union1 (Beech, BeechUnion) - 返回區域的補集
complement (BeechUnion, NotBeech) - 求取兩區域不相交部分
difference (NotBeech, Path, NotBeechNotPath) - 縮小定義域
reduce_domain (Red, NotBeechNotPath, NotBeechNotPathRed) - 二值化
threshold (NotBeechNotPathRed, BrightRest, 150, 255) - 分割連通域
connection (BrightRest, BrightRestConnection) - 通過面積篩選區域
select_shape (BrightRestConnection, Meadow, ‘area’, ‘and’, 500, 1000000)
dev_display (Red)
dev_display (Meadow)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop () - 將兩個對象合為一個對象
union2 (Path, RedBrightClosing, BeechPath) - smooth_image - 使用各種濾鏡平滑圖像。
- smooth_image(原圖:平滑后的圖像:濾波器,濾波參數:)
smooth_image (Red, RedGauss, ‘gauss’, 4.0) - invert_image - 反轉圖像。
invert_image (RedGauss, Invert) - watersheds - 從圖像中提取流域和盆地。
- watersheds (圖片:盆地,流域::)
watersheds (Invert, SpruceRed, Watersheds) - 通過面積與灰度進行區域篩選
select_shape (SpruceRed, SpruceRedLarge, ‘area’, ‘and’, 100, 5000)
select_gray (SpruceRedLarge, Red, SpruceRedInitial, ‘max’, ‘and’, 100, 200) - 生成一個空對象
gen_empty_obj (LocalThresh) - 計算對象數量
count_obj (SpruceRedInitial, NumSpruce)
dev_update_var (‘off’)
dev_update_pc (‘off’)
for i := 1 to NumSpruce by 1- 在組員對象中選取指定對象
select_obj (SpruceRedInitial, SingleSpruce, i) - 求取最大最小灰度值,當第三個參數設置為50時返回中值
min_max_gray (SingleSpruce, Red, 50, Min, Max, Range) - 減少定義域
reduce_domain (Red, SingleSpruce, SingleSpruceRed) - 二值化
threshold (SingleSpruceRed, SingleSpruceBright, Min, 255) - 分割連通域
connection (SingleSpruceBright, SingleSpruceBrightCon) - 選取最大面積的區域
select_shape_std (SingleSpruceBrightCon, MaxAreaSpruce, ‘max_area’, 70) - 將多個區域合并到一個組員下,但不改變對象個數
concat_obj (MaxAreaSpruce, LocalThresh, LocalThresh)
- 在組員對象中選取指定對象
endfor
- 開運算
opening_circle (LocalThresh, FinalSpruce, 1.5)
dev_set_line_width (2)
dev_set_color (‘red’)
dev_display (Red)
dev_display (FinalSpruce)
dev_set_color (‘green’)
dev_display (Beech)
dev_set_color (‘yellow’)
dev_display (Meadow)
處理思路
這個例子是主要講解了如何通過無人機拍攝的圖像識別其中的對象。smooth_image 、watersheds 等算子被應用。
后記
大家有什么問題可以向我提問哈,我看到了第一時間回復,希望在學習的路上多多結交良師益友。