二维测量--检测钻石的边缘以及角度

对应示例程序:
apply_metrology_model_diamond.hdev

目标:使用亚像素精度检测钻石的边缘,并计算出它们之间的角度。

*首先,对钻石顶部进行粗略的分割,以对齐计量对象。

*然后,应用计量模型,返回拟合线的参数,最后计算两条线之间的夹角。
思路为:
      1.创建计量模型模板
      2.根据先验知识生成两条直线
      3.设置模板的相关参数,包括图像大小,容忍度等
      4.截取一部分区域用于校正模板,并生成参考系句柄
      5.读取检测图像,先校正计量模型,对钻石顶部进行粗略的分割,以对齐计量对象。然后,应用计量模型,返回拟合线的参数,最后计算两条线之间的夹角
      6.提取测量结果,测量的轮廓,定位区域,后补区域

图像:
在这里插入图片描述

代码:

dev_update_off ()
dev_close_window ()
read_image (Image, 'diamond/diamond_01')
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
get_image_size (Image, Width, Height)
* 
* Create the metrology model data structure
create_metrology_model (MetrologyHandle)  //生成计量模型
* The image size is set in advance to speed up the
* first call of apply_metrology_model.
set_metrology_model_image_size (MetrologyHandle, Width, Height)  //输入图像的大小 用于加速

* Define the parameters of the metrology line objects   定义计量线对象的参数
LineRow1 := [155,155]
LineColumn1 := [400,400]
LineRow2 := [290,290]
LineColumn2 := [230,570]

*显示定义的计量线
disp_line(WindowHandle,LineRow1[0],LineColumn1[0],LineRow2[0],LineColumn2[0])
disp_line(WindowHandle,LineRow1[1],LineColumn1[1],LineRow2[1],LineColumn2[1])

Tolerance := 20    //设置容忍度
* 
* Create two metrology line objects and set parameters  加线对象加入到计量模型中
add_metrology_object_line_measure (MetrologyHandle, LineRow1, LineColumn1, LineRow2, LineColumn2, Tolerance, 10, 1, 20, [], [], Index1)
* Create region of interest for the alignment  创建ROI 用于图像的对齐(校正)

gen_rectangle1 (Rectangle, LineRow1[0] - 40, LineColumn1[0] - 50, LineRow1[0] + 20, LineColumn1[0] + 50)
* Change the reference coordinate system in which the
* metrology model is given to be situated at the top of the diamond
*更改计量模型位于金刚石顶部的参考坐标系。
reduce_domain (Image, Rectangle, ImageReduced)
threshold (ImageReduced, Region, 128, 255)
get_region_points (Region, Rows, Columns)  //访问区域的像素
set_metrology_model_param (MetrologyHandle, 'reference_system', [Rows[0],Columns[0],0])  //设置参考系
* 
* Main loop  循环
* 
for I := 1 to 5 by 1
    read_image (Image, 'diamond/diamond_' + I$'02')
    * Roughly segment the diamond's position   大致分割钻石的位置
    reduce_domain (Image, Rectangle, ImageReduced)
    threshold (ImageReduced, Region, 128, 255)
    * Extract the top of the diamond
    get_region_points (Region, Rows, Columns)
    * 
    * Use the top of the diamond to align the metrology model in
    * the current image
    * 使用菱形顶部对齐计量模型
    align_metrology_model (MetrologyHandle, Rows[0], Columns[0], 0)  //校准计量模型。
    * 
    * 
    * Perform the measurement for both lines in one call
    * 
    apply_metrology_model (Image, MetrologyHandle)   //测量并拟合计量模型中所有计量对象的几何形状。
    * 
    * Access results
    * 
    get_metrology_object_result (MetrologyHandle, 'all', 'all', 'result_type', 'all_param', LineParameter) //获取计量模型的测量结果
    *计算两条直线间的夹角
    angle_ll (LineParameter[0], LineParameter[1], LineParameter[2], LineParameter[3], LineParameter[4], LineParameter[5], LineParameter[6], LineParameter[7], Angle)
    Angle := deg(Angle)
    * 
    * Display results  结果显示
    * 
    * Create line contours
    get_metrology_object_result_contour (ResultContour, MetrologyHandle, 'all', 'all', 1.5)  //查询计量对象的结果轮廓。
    
    //计算两条线的交点
    intersection_lines (LineParameter[0], LineParameter[1], LineParameter[2], LineParameter[3], LineParameter[4], LineParameter[5], LineParameter[6], LineParameter[7], Row, Column, IsOverlapping1)
    * Calculate the orientation of the two lines
    //计算两条线的方向
    line_orientation (LineParameter[0], LineParameter[1], LineParameter[2], LineParameter[3], Orientation1)
    if (Orientation1 > 0)
        Orientation1 := Orientation1 - rad(180)
    endif
    line_orientation (LineParameter[4], LineParameter[5], LineParameter[6], LineParameter[7], Orientation2)
    * 
    * Visualize the angle between the lines  可视化角度
    
    gen_circle_contour_xld (ContCircle, Row, Column, 100, Orientation1, Orientation2, 'positive', 1)
    * Get the used measure regions and the measured points
    * for visualization
    //获取用于可视化的测量区域和测量点
    get_metrology_object_measures (Contour, MetrologyHandle, 'all', 'all', MRow, MColumn)
    gen_cross_contour_xld (Cross, MRow, MColumn, 6, rad(45)) //为每个输入点生成一个十字形状的XLD轮廓。
    * Display everything  窗口显示
    dev_display (Image)
    dev_set_line_width (1)
    dev_set_color ('yellow')
    dev_display (Contour)
    dev_display (Cross)
    dev_set_line_width (2)
    dev_set_color ('green')
    dev_display (ResultContour)
    dev_set_color ('blue')
    dev_display (ContCircle)
    disp_message (WindowHandle, 'Angle = ' + Angle$'.5' + '°', 'window', 12, 12, 'black', 'true')
    if (I < 5)
        disp_continue_message (WindowHandle, 'black', 'true')
    endif
    stop ()
endfor
* Clean up memory
clear_metrology_model (MetrologyHandle)  //清理计量化模型

用到的几个算子:
      create_metrology_model (MetrologyHandle)–创建计量模型句柄
      set_metrology_model_image_size --设置计量对象图像的大小。
      add_metrology_object_line_measure–将线对象加入到计量模型中
      get_region_points–访问区域的像素
      set_metrology_object_param–设置计量模型的相关参数
      align_metrology_model–校准计量模型
      apply_metrology_model—测量并拟合计量模型中所有计量对象的几何形状
      get_metrology_object_result–获取计量模型的测量结果
      get_metrology_object_result_contour --查询计量对象的结果轮廓
      get_metrology_object_measures --得到计量模型的测量区域和边缘定位结果。
      clear_metrology_model --释放计量模型的句柄
      angle_ll–计算两条直线的夹角
      intersection_lines–计算两条直线的交点
      line_orientation–计算直线的方向

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值