原圖
程式碼
dev_close_window ()
*讀取影像
read_image (Image, 'E:/Halcon_Learn/定位方向_畫箭頭/clip.png')
*獲得影像尺寸
get_image_size (Image, Width, Height)
*開啟影像大小的視窗
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
*灰度化
rgb1_to_gray (Image, GrayImage)
*閾值化
threshold (GrayImage, Regions, 0, 67)
*區域化分塊
connection (Regions, ConnectedRegions)
*特徵篩選(面積)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 4380.1, 10000)
*統計符合區域個數
count_obj (SelectedRegions, Number)
*最小外接矩形
smallest_rectangle2 (SelectedRegions, Row, Column, Phi, Length1, Length2)
*設定畫筆顏色
dev_set_color ('green')
*設定填充形式
dev_set_draw ('margin')
*設定線寬
dev_set_line_width (3)
*設定字型大小
set_display_font (WindowHandle, 24, 'mono', 'true', 'false')
*顯示原圖
dev_display (Image)
*通過最小外接矩形引數生成矩形
gen_rectangle2 (Rectangle, Row, Column, Phi, Length1, Length2)
*求目標各區域的旋轉角度
orientation_region (SelectedRegions, Phi1)
*求目標各區域的中心
area_center (SelectedRegions, Area, Row1, Column1)
*設定畫筆顏色
dev_set_color ('red')
*畫區域目標方向箭頭
disp_arrow (WindowHandle, Row1, Column1, Row1+Length1*cos(Phi1+1.5708), Column1+Length1*sin(Phi1+1.5708), 1)
*設定畫筆顏色
dev_set_color ('blue')
*迴圈顯示目標提示語
set_tposition (WindowHandle,10,10)
write_string (WindowHandle, '目標定位結果如下:')
*設定字型大小
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
for i := 0 to Number-1 by 1
set_tposition (WindowHandle, Row1[i], Column1[i])
write_string (WindowHandle, '第'+(i+1)+'個')
endfor
*儲存視窗顯示結果
dump_window (WindowHandle, 'png', '定位方向')
結果