Halcon练手:定位并找圆

模板图片:
测试图片:



代码:

list_image_files ('.', 'default', [], ImageFiles)
read_image (ImageModel, ImageFiles[0])
dev_set_draw ('margin')
gen_rectangle1 (ROI_0, 75.2981, 481.751, 507.636, 898.23)
reduce_domain (ImageModel, ROI_0, ImageReduced)
dev_display (ImageReduced)
*亚像素轮廓
edges_sub_pix (ImageReduced, Edges, 'canny', 1, 40, 70)
*创建轮廓模板
create_shape_model_xld (Edges, 'auto', rad(-180), rad(360), 'auto', 'auto', 'ignore_local_polarity', 5, ModelID)
*找模板图片的轮廓位置
* get_shape_model_contours (ModelContours, ModelID, 1)
find_shape_model (ImageModel, ModelID, rad(-180), rad(360), 0.5, 1, 0.5, 'least_squares', 0, 0.9, RowModel, ColumnModel, AngleModel, Score)
    
for Index := 0 to |ImageFiles|-1 by 1
    read_image (Image, ImageFiles[Index])
    find_shape_model (Image, ModelID, rad(-180), rad(360), 0.5, 1, 0.5, 'least_squares', 0, 0.9, RowCur, ColumnCur, AngleCur, Score)
    *得到现在图片与模板图 矩阵变化
    vector_angle_to_rigid (RowModel, ColumnModel, AngleModel, RowCur, ColumnCur, AngleCur, HomMat2D)
*     hom_mat2d_identity (HomMat2DIdentity)
*     hom_mat2d_rotate (HomMat2DIdentity, AngleCur, 0, 0, HomMat2DRotate)
*     hom_mat2d_translate (HomMat2DRotate, RowCur, ColumnCur, HomMat2DTranslate)
    *将模板轮廓 根据矩阵变化 贴合现在图片显示
     affine_trans_contour_xld (Edges, ContoursAffineTrans, HomMat2D)
    *创建测量模型
    create_metrology_model (MetrologyHandle)
    *模板图的圆位置ROI
*     gen_circle (ROI_1, 527.164, 705.082, 255.129)
    circleRowModel:=527.164
    circleColumnModel:=705.082
    circleParam:=[527.164, 705.082, 255.129]

    *圆位置变换关系
    affine_trans_point_2d (HomMat2D, circleRowModel, circleColumnModel,circleRow, circleColumn)
    *添加当前圆的测量模型
    *(1)_generic 通用工具 可为圆 矩形 等 'circle' 找圆
*     add_metrology_object_generic (MetrologyHandle, 'circle', circleParam, 20, 5, 1, 30, [], [], Index1)
    *(2)_circle  添加圆工具   'measure_select'  'measure_select'
    add_metrology_object_circle_measure (MetrologyHandle, circleRow, circleColumn, 255.129, 20, 5, 1, 30, 'measure_select', 'first', Index1)
   *适应模型
    apply_metrology_model (Image, MetrologyHandle)
    *获取结果
    get_metrology_object_result (MetrologyHandle, Index1, 'all', 'result_type', 'all_param', Parameter)
    *获取模型用到的点
    get_metrology_object_measures (Contours, MetrologyHandle, 'all', 'all', Row, Column)
    *取每个卡尺取到的点
    gen_cross_contour_xld (Cross, Row, Column, 6, 0.785398)
    *点——>轮廓
    gen_contour_polygon_xld (Contour, Row, Column)
    *拟合圆
    fit_circle_contour_xld (Contour, 'algebraic', -1, 0, 0, 3, 2, Row1, Column1, Radius, StartPhi, EndPhi, PointOrder)
    gen_circle (Circle, Row1, Column1, Radius)
    dev_display (Circle)
    stop()
endfor

Halcon练手:定位并找圆_第1张图片

你可能感兴趣的:(halcon)