halcon 圓檢測(識別圓、圓擬合)

## halcon 圓檢測(識別圓、圓擬合) ##

  1. 要求:
    如圖:
    這裏寫圖片描述
    識別左邊大的圓孔
    1. 算法實現及講解:
dev_close_window ()
***讀取圖片
read_image (Image, 'E:01.JPG')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width/2, Height/2, 'black', WindowHandle)
dev_display (Image)
rgb1_to_gray (Image, GrayImage)
***分割區域
*選取圓所在區域,縮小圖像處理範圍
gen_circle (ROI_0, 541.5, 141.5, 151.539)
reduce_domain (GrayImage, ROI_0, ImageReduced)
**閾值分割

dev_set_draw ('fill')

threshold (ImageReduced, Regions, 115, 255)
*取Region邊界
boundary (Regions, RegionBorder, 'inner')
**膨脹
dilation_circle (RegionBorder, RegionDilation, 2.5)
*在弧形區域進行邊緣檢測
edges_sub_pix (ImageReduced, Edges, 'canny', 1, 20, 40)
*分割邊緣:線和圓
*對檢測的邊緣進行分割,識別線或者圓'lines_circles',
segment_contours_xld (Edges, ContoursSplit, 'lines_circles', 5, 5, 8)
*統計識別出圓或線的數量
count_obj (ContoursSplit, Number)
stop()
dev_close_window ()
dev_open_window (0, 0, Width/2, Height/2, 'black', WindowHandle)
dev_display (Image)
dev_set_draw ('margin')
dev_set_color ('red')
dev_update_window ('off')
*儲存擬合圓的圓心座標和半徑
ROW:=[]
COL:=[]
Rad:=[]
n:=0
for i := 1 to Number by 1

    *選擇輪廓並根據特性確定是否擬合圓:* Attrib = -1 線段 0 橢圓 1圓
    select_obj (ContoursSplit, ObjectSelected, i)
    get_contour_global_attrib_xld (ObjectSelected, 'cont_approx', Attrib)
    if (Attrib > 0)
        *逼近結果生成一個圓輪廓
        fit_circle_contour_xld (ObjectSelected, 'ahuber', -1, 2, 0, 3, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder)
        *這裏會生成大量的擬合圓,通過添加條件,選取自己需要的圓,這裏
        *條件爲半徑,(可以自己註釋條件,查看所有擬合圓結果 )
        if(Radius<73 and Radius>70)
        *生成輪廓
            gen_circle_contour_xld (ContCircle, Row, Column, Radius, 0, rad(360), 'positive', 1.0)
            *記錄圓的圓心座標和半徑信息
            ROW[n]:=Row
            COL[n]:=Column
            Rad[n]:=Radius
            n:=n+1
            dev_display (ContCircle)
        endif
    endif
endfor
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章