halcon 圆检测(识别圆、圆拟合)

## halcon 圆检测(识别圆、圆拟合) ##

  1. 要求:
    如图:
    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

你可能感兴趣的:(halcon)