HALCON示例程序circles.hdev邊界輪廓的圓形擬合
小哥哥小姐姐覺得有用點個贊唄!
示例程序源碼(加注釋)
- 讀入圖片
read_image (Image, ‘double_circle’) - 窗口初始化
dev_close_window ()
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, ‘black’, WindowHandle) - 將區域分割出來并且去除邊界
- 快速閾值分割
fast_threshold (Image, Region, 0, 120, 7) - 使用形態學運算來計算區域的邊界,這里就是將圖片邊界提取出來
boundary (Region, RegionBorder, ‘inner’) - 剪切最小外接矩形內縮的區域
函數原型clip_region_rel(Region : RegionClipped : Top, Bottom, Left, Right : )
Region :輸入區域;RegionClipped :輸出區域縮小的區域;Top, Bottom, Left, Right:內縮量
外接矩形內縮后與原有外接矩形形成一個矩形環,就是將區域減去這個矩形環生成的區域。
clip_region_rel (RegionBorder, RegionClipped, 5, 5, 5, 5) - 使用半徑為2.5的圓形元素對區域進行膨脹
dilation_circle (RegionClipped, RegionDilation, 2.5) - 減少圖像定義域
reduce_domain (Image, RegionDilation, ImageReduced)
提取邊緣
- edges_sub_pix - 使用Deriche,Lanser,Shen或Canny過濾器提取亞像素精確邊緣
函數原型:edges_sub_pix(Image : Edges : Filter, Alpha, Low, High : )
Image :輸入圖像;Edges :輸出的邊緣;Alpha:濾波器寬度;
Low:滯后閾值操作的閾值下限;High: 滯后閾值操作的閾值上限
edges_sub_pix (ImageReduced, Edges, ‘canny’, 2, 20, 60) - segment_contours_xld - 分割XLD輪廓為線段和圓弧或橢圓弧
函數原型;segment_contours_xld(Contours : ContoursSplit :
Mode, SmoothCont, MaxLineDist1, MaxLineDist2 : )
Contours :輸入輪廓;ContoursSplit:分割后的輪廓;Mode:輪廓分割模式;
SmoothCont:用于平滑輪廓的點數;MaxLineDist1:輪廓與近似線之間的最大距離(第一次迭代);
MaxLineDist2 :輪廓與近似線之間的最大距離(第二次迭代)
segment_contours_xld (Edges, ContoursSplit, ‘lines_circles’, 5, 4, 3) - 對區域元素進行計數
count_obj (ContoursSplit, Number)
顯示
dev_display (Image)
dev_set_draw (‘margin’)
dev_set_color (‘white’)
dev_update_window (‘off’)
for I := 1 to Number by 1- 通過索引號選取數組內的指定元素
select_obj (ContoursSplit, ObjectSelected, I) - 返回XLD輪廓的全局屬性值’cont_approx’表示:對于’cont_approx’=-1,輪廓近似線段;
- ‘cont_approx’=0,曲線近似橢圓;‘cont_approx’=1,曲線近似圓弧。
get_contour_global_attrib_xld (ObjectSelected, ‘cont_approx’, Attrib) - 圓弧線段進行圓形擬合
if (Attrib > 0)- fit_circle_contour_xld 對XLD輪廓進行圓弧擬合。
fit_circle_contour_xld (ObjectSelected, ‘ahuber’, -1, 2, 0, 3, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder) - 繪制擬合出來的圓
gen_circle_contour_xld (ContCircle, Row, Column, Radius, 0, rad(360), ‘positive’, 1.0)
dev_display (ContCircle)
endif
endfor
- fit_circle_contour_xld 對XLD輪廓進行圓弧擬合。
- 通過索引號選取數組內的指定元素
- 顯示設置與顯示
dev_set_colored (12)
dev_set_line_width (3)
dev_display (ContoursSplit)
處理思路
這個例子是集邊界提取、邊界分割、邊界擬合于一個程序,進行了實際的講解。這個例子當中的關于xld操作的算子以后我們做邊界提取、測量時會用到很多。
后記
大家有什么問題可以向我提問哈,我看到了第一時間回復,希望在學習的路上多多結交良師益友。