例程注释(measure_stamping_part)

* This program shows how the metrology model can be used to
* obtain calibrated measurements for various geometric forms
* lying in a plane. The parameters of the geometric forms are
* derived from a model image.
* 
* 该程序显示了度量模型如何用于获取平面中各种几何形式的校准测量结果。
* 几何形式的参数是从模型图像导出的。

* To find the objects to be measured in a new image, three different
* alignment methods are shown:
* a) Using shape-based matching
* b) Using region processing
* c) Using a rigid transformation from point correspondences
* 
* The results of a measurement are returned as contours
* and as adjusted parameters of the given geometric objects
* 
* 
* Choose an alignment method by activating the desired line

* 为了在新图像中找到要测量的物体,需要三种不同的测量方法。
*所示为对齐方法。
* a) 使用基于形状的匹配
* b) 使用区域处理
* c) 使用点对应的刚性变换。
* 
* 测量结果以等高线形式返回。
* 并作为给定几何对象的调整参数。
* 
* 
* 通过激活所需的线来选择对齐方式。

* 对齐模式 此处为基于形状匹配
* 区域处理
* 点对应的刚性变换
AlignmentMode := 'shape-based matching'
* 
* Initialize visualization
dev_update_off ()
dev_close_window ()
dev_set_draw ('margin')
* 初始化EmptyObject
gen_empty_obj (EmptyObject)
read_image (Image, 'metal-parts/circle_plate_01')
get_image_size (Image, Width, Height)
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
* 
* Part I:
* 
* Initialize metrology model
* 
* Define camera parameter
* 定义摄像机参数
CameraParam := [0.0128649,-661.434,5.30004e-006,5.3e-006,620.043,497.402,Width,Height]
* The pose of the measurement plane was derived by placing a
* calibration plate in the plane of the object to be measured
* and by determining its pose
* 测量平面的姿态是通过在被测物体的平面上放置一块校准板并确定其姿态而得出的。
MeasurementPlane := [0.00940956,-0.00481017,0.29128,0.478648,359.65,0.785,0]
* Adjust the pose of the measurement plane
* for the height of the part and the height of the calibration plate
* 根据工件的高度和校准板的高度调整测量平面的姿态。
* Thickness of the calibration plate
* 校准板的厚度
CalibPlateThickness := 0.006
* Thickness of the object to be measured
* 待测物体的厚度
PartHeight := 0.005
* 实际厚度就是校准板的厚度 - 待测物体的厚度
AdjustThickness := CalibPlateThickness - PartHeight
* 设置坐标系原点(要设置原点的坐标系,沿x轴方向平移距离,沿y轴方向平移距离,沿y轴方向平移距离,输出新原点的三维坐标系)
set_origin_pose (MeasurementPlane, 0, 0, AdjustThickness, MeasurementPlaneAdjusted)
* 
* Create the metrology model and prepare it for
* calibrated measurements
* 创建计量模型,并为校准测量做准备。
create_metrology_model (MetrologyHandle)
* Set the image size in advance to speed up the
* first call of apply_metrology_model.
* 提前设置图像大小,以加快apply_metrology_model的第一次调用。
set_metrology_model_image_size (MetrologyHandle, Width, Height)
* Set the camera parameters
* 设置摄像机参数(参数的名称,参数的值)
set_metrology_model_param (MetrologyHandle, 'camera_param', CameraParam)
* Set the plane in which the measured object lies
* 设定被测物体所在的平面
set_metrology_model_param (MetrologyHandle, 'plane_pose', MeasurementPlaneAdjusted)
* 
* Add the objects to be measured to the metrology model
* 
* Add several full circles
* 将要测量的对象添加到计量模型中。
* 增加几个完整的圆圈
dev_display (Image)
Message := '增加几个完整的圆圈'
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
draw_circle (WindowHandle, Row3, Column3, Radius)
draw_circle (WindowHandle, Row4, Column4, Radius1)
draw_circle (WindowHandle, Row5, Column5, Radius2)
draw_circle (WindowHandle, Row6, Column6, Radius3)
CircleParam:=[Row3, Column3, Radius,Row4, Column4, Radius1,Row5, Column5, Radius2,Row6, Column6, Radius3]
* 在计量模型中增加一个计量对象(要添加的计量对象的类型,要添加的计量对象的参数,垂直于边界的测量区域的一半长度,
* 测量区域到边界的一半长度,用于平滑的高斯函数的Σ,最小边缘振幅,参数的名称,参数的值,所创建的计量对象的索引)
add_metrology_object_generic (MetrologyHandle, 'circle', CircleParam, 20, 5, 1, 30, [], [], CircleIndices1)
* 增加两个局部圆圈
* Add two partial circles
Message := '增加两个局部圆圈'
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
draw_circle (WindowHandle, Row7, Column7, Radius4)
draw_circle (WindowHandle, Row8, Column8, Radius5)
CircleParam1 :=[Row7, Column7, Radius4]
CircleParam2:=[Row8, Column8, Radius5]
add_metrology_object_generic (MetrologyHandle, 'circle', CircleParam1, 20, 5, 1, 30, ['start_phi','end_phi'], [0,rad(185)], CircleIndices2)
add_metrology_object_generic (MetrologyHandle, 'circle', CircleParam2, 20, 5, 1, 30, ['start_phi','end_phi'], [rad(45),rad(185)], Index3)
CircleIndices2 := [CircleIndices2,Index3]
* 
* Add a retangle
* 增加一个矩形
Message := '矩形'
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
draw_rectangle2 (WindowHandle, Row9, Column9, Phi, Length1, Length2)
RectangleParam:=[Row9, Column9, Phi, Length1, Length2]
add_metrology_object_generic (MetrologyHandle, 'rectangle2', RectangleParam, 20, 5, 1, 30, [], [], RectIndices)
* Add two lines
* 增加两条线
Message := '增加两条线'
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
draw_line (WindowHandle, Row11, Column11, Row21, Column21)
draw_line (WindowHandle, Row12, Column12, Row22, Column22)
Line1 := [Row11, Column11, Row21, Column21]
Line2 := [Row12, Column12, Row22, Column22]
add_metrology_object_generic (MetrologyHandle, 'line', [Line1,Line2], 20, 5, 1, 30, [], [], LineIndices)
* Inspect the shapes that have been added to the metrology model
* 检查已添加到计量模型的形状。
* 以图像坐标查询计量对象的模型轮廓(模型轮廓out,计量模型的处理方式,计量对象的索引,相邻轮廓点之间的距离)
get_metrology_object_model_contour (ModelContour, MetrologyHandle, 'all', 1.5)
* 获取计量模型的计量对象的测量区域和边缘定位结果 测量区域的矩形XLD轮廓out(测量边缘的行/列坐标out)
get_metrology_object_measures (MeasureContour, MetrologyHandle, 'all', 'all', Row, Column)
*本例展示了如何使用计量模型测量几何图形。作为准备工作,它们的大致已知尺寸和公差由用户指定。
show_contours (Image, ModelContour, MeasureContour, EmptyObject, WindowHandle, Message)
stop ()
* 
* Part 2:
* 
* Prepare the alignment
* 准备对准
* 
* a) Shape-based matching
if (AlignmentMode == 'shape-based matching')
    dev_set_part (-Height / 2 - 100, -Width / 2, 1.5 * Height - 100, 1.5 * Width)
    * 
    * Create the shape model for the alignment
    * of the metrology model in images where the
    * object occurs in positions and orientations different
    * from the ones in the image used for creating the model
    * 创建形状模型,以便在图像中对计量模型进行校准,其中物体出现的位置和方向与用于创建模型的图像中的不同。
    get_system ('border_shape_models', BorderShapeModel)
    set_system ('border_shape_models', 'true')
    * 使用全局阈值对图像进行分割(细分区域out,灰色值的下/上限)
    * 把中间主要部分抠出来
    threshold (Image, Region, 0, 50)
    * 用矩形结构元素扩展一个区域(扩张区域out,矩形的宽度/高)
    * 有些没包括的加5给都选上
    dilation_rectangle1 (Region, ModelRegion, 5, 5)
    * 减少图像的域(,新定义域,缩小定义域的图像out* 裁剪
    reduce_domain (Image, ModelRegion, ImageReduced)
    * 准备形状模型进行匹配(输入图像其将被用于创建模型,最大等级,最小的图案旋转,匹配旋转角度的步长,
    * 设置模板优化和模板创建方法,匹配方法,设置对比度,输入图像中匹配物体的最小差别(对比度),输出模型句柄)
    create_shape_model (ImageReduced, 6, 0, rad(360), 'auto', 'auto', 'use_polarity', 'auto', 20, ShapeModelID)
    * The origin of the shape model is the center of the
    * input region used in create_shape_model
    * 形状模型的原点是create_shape_model中使用的输入区域的中心。
    * 获取区域的大小和中心(待测区域,输出区域的面积,区域中心y,区域中心x)
    area_center (ModelRegion, Area, RowModel, ColumnModel)
    * 返回模型轮廓的外部轮廓(轮廓out,输入匹配模型句柄,输入复制外形轮廓的金字塔)
    get_shape_model_contours (ShapeModelContours, ShapeModelID, 1)
    * 形状模型将用于计量模型的对准。形状模型(白色)和计量模型(蓝色)的轮廓如图所示。
    show_contours (Image, ModelContour, EmptyObject, ShapeModelContours, WindowHandle, Message)
    * 
    * Change the reference system in which the metrology model is
    * defined to correspond to the one used by the shape model.
    * This is necessary to be able to directly use the results of
    * find_shape_model to align the metrology model
    * 改变定义计量模型的参考系统,使之与形状模型使用的参考系统相对应。
    * 这是必要的,以便能够直接使用find_shape_model的结果来调整计量模型。
    * 设置计量模型的参数(名称,参数)
    set_metrology_model_param (MetrologyHandle, 'reference_system', [RowModel,ColumnModel,0])
    * 以图像坐标查询计量对象的模型轮廓(模型轮廓out,计量模型的处理方式,计量对象的索引,相邻轮廓点之间的距离)
    get_metrology_object_model_contour (ModelContour, MetrologyHandle, 'all', 1.5)
    * 为了准备对位,将形状模型的原点设定为计量模型的参考系'。
    show_contours (Image, ModelContour, EmptyObject, ShapeModelContours, WindowHandle, Message)
    stop ()
    dev_set_part (0, 0, Height - 1, Width - 1)
endif



* Online phase
for I := 2 to 5 by 1
    read_image (CurrentImage, 'metal-parts/circle_plate_' + I$'02d')
    dev_set_line_width (1)
    dev_display (CurrentImage)
    * 
    * a) Shape-based matching
    if (AlignmentMode == 'shape-based matching')
        * 
        * Measure the position and orientation of the object in the
        * current image using the shape model
        * 使用形状模型测量当前图像中物体的位置和方向。
        * 在图像中找出一个最佳的匹配轮廓(匹配模型的图像,输入模型句柄(ShapeModelContours),输入匹配时起始角度
        * ,输入匹配时角度范围,输入最小的匹配值(匹配质量),在图像上找到模板的最大个数,定义找到的两个目标
        * 最多重叠的系数 0是不能重叠1是找到的目标区域搜药返回,确定找到的目标是否使用亚像素精度提取,搜索时使用
        * 金字塔层数,搜索时的“贪婪程度0慢但安全1快”,输出目标外形轮廓的y坐标中心位置,输出目标外形轮廓的x坐标中心位置
        * 输出目标模型旋转后的角度,输出目标物体的匹配值(有多少匹配))
        find_shape_model (CurrentImage, ShapeModelID, 0, rad(360), 0.5, 1, 0, 'least_squares', 5, 0.9, RowAlign, ColumnAlign, AngleAlign, Score)
        dev_display_shape_matching_results (ShapeModelID, 'white', RowAlign, ColumnAlign, AngleAlign, 1, 1, 0)
    endif
    
    
  
    * 
    * Use the computed position and orientation to
    * align the metrology model with its current occurrence
    * 使用计算出的位置和方向,使计量模型与其当前出现的情况保持一致。
    * 计量模型的调整(输入变动的参数)
    align_metrology_model (MetrologyHandle, RowAlign, ColumnAlign, AngleAlign)
    * Display the aligned metrology model
    * 显示对齐的计量模型
    if (I <= 2)
        * 以图像坐标查询计量对象的模型轮廓(模型轮廓out,计量模型的处理方式,计量对象的索引,相邻轮廓点之间的距离)
        get_metrology_object_model_contour (ModelContour, MetrologyHandle, 'all', 1.5)
        stop ()
    endif
    * 
    * Perform the measurement for all metrology objects in one call
    * 在一次调用中执行所有计量对象的测量。
    * 测量并拟合计量模型中所有计量对象的几何形状。
    apply_metrology_model (CurrentImage, MetrologyHandle)
    * Get measure regions for visualization
    * 测量矩形XLD轮廓out(测量边缘的行/列坐标out)
    get_metrology_object_measures (Contour, MetrologyHandle, 'all', 'all', Row, Column)
    * Get the edge points that were actually used to fit the
    * geometric forms
    * 获取实际用于匹配几何形状的边缘点。
    * 获取计量模型的测量结果
    get_metrology_object_result (MetrologyHandle, 'all', 'all', 'used_edges', 'row', UsedRow)
    get_metrology_object_result (MetrologyHandle, 'all', 'all', 'used_edges', 'column', UsedColumn)
    * 为每个输入点生成一个十字形的XLD轮廓(输出,输入x/y,十字架的长度,十字架的角度)
    gen_cross_contour_xld (UsedEdges, UsedRow, UsedColumn, 10, rad(45))
    * 
    * Access the results of the measurement
    * Since camera parameters were set, all results are given in
    * metric coordinates relative to the coordinate system
    * defined by the measurement plane
    * 
    * 获取测量结果 由于设置了摄像机参数,所有结果都是以相对于测量平面所定义的坐标系的公制坐标给出的。
    * Get a visualization of the measured geometric forms
    * 获得测量的几何形状的可视化效果
    * 查询测量对象的结果轮廓。
    get_metrology_object_result_contour (ResultContours, MetrologyHandle, 'all', 'all', 1.5)
    * Extract the radius of the smaller circles
    * 提取小圆的半径 通过CircleIndices1找出半径out RadiusC1
    get_metrology_object_result (MetrologyHandle, CircleIndices1, 'all', 'result_type', 'radius', RadiusC1)
    * Extract the radius of the larger incomplete circles
    get_metrology_object_result (MetrologyHandle, CircleIndices2, 'all', 'result_type', 'radius', RadiusC2)
    * Extract the lengths of the rectangle sides
    get_metrology_object_result (MetrologyHandle, RectIndices, 'all', 'result_type', 'length1', Length1R)
    get_metrology_object_result (MetrologyHandle, RectIndices, 'all', 'result_type', 'length2', Length2R)
    * Get the start and end point of each measured line
    get_metrology_object_result (MetrologyHandle, LineIndices[0], 'all', 'result_type', 'all_param', ParamLine1)
    get_metrology_object_result (MetrologyHandle, LineIndices[1], 'all', 'result_type', 'all_param', ParamLine2)
    * Display the results
    dev_display (CurrentImage)
    dev_set_line_width (1)
    dev_set_color ('light gray')
    dev_display (Contour)
    dev_set_color ('green')
    dev_set_line_width (2)
    dev_display (ResultContours)
    dev_set_line_width (1)
    dev_set_color ('white')
    dev_display (UsedEdges)
    * 
    * Display the radii for each circle at the circle centers
    * Obtain the metric coordinates of the circle centers
    * 显示每个圆在圆心处的半径。
    * 获得圆心的坐标。
    get_metrology_object_result (MetrologyHandle, CircleIndices1, 'all', 'result_type', 'x', XC1)
    get_metrology_object_result (MetrologyHandle, CircleIndices1, 'all', 'result_type', 'y', YC1)
    * Project the metric coordinates of the circle centers
    * into the image to get the image coordinates of the
    * circle centers
    * 将圆心的公制坐标投影到图像中,得到圆心的图像坐标。
    *XC1 (input_control)  any → (tuple)
    *YC1 (input_control)  any → (tuple)
    *MeasurementPlaneAdjusted (input_control)  any → (tuple)
    *CameraParam (input_control)  any → (tuple)
    *Row1 (output_control)  any → (tuple)
    *Column1 (output_control)  any → (tuple)
    project_xy_to_image (XC1, YC1, MeasurementPlaneAdjusted, CameraParam, Row1, Column1)
    get_metrology_object_result (MetrologyHandle, CircleIndices2, 'all', 'result_type', 'x', XC2)
    get_metrology_object_result (MetrologyHandle, CircleIndices2, 'all', 'result_type', 'y', YC2)
    project_xy_to_image (XC2, YC2, MeasurementPlaneAdjusted, CameraParam, Row2, Column2)
    * 以下显示 显示完成以后继续找下一个旋转的图形
    for J := 0 to |RadiusC1| - 1 by 1
        disp_message (WindowHandle, 'r=' + (RadiusC1[J] * 1000)$'.2f', 'image', Row1[J], Column1[J] - 80, 'black', 'true')
    endfor
    for J := 0 to |RadiusC2| - 1 by 1
        disp_message (WindowHandle, 'r=' + (RadiusC2[J] * 1000)$'.2f', 'image', Row2[J], Column2[J] - 80, 'black', 'true')
    endfor
    get_metrology_object_result (MetrologyHandle, RectIndices, 'all', 'result_type', 'x', XRectangle)
    get_metrology_object_result (MetrologyHandle, RectIndices, 'all', 'result_type', 'y', YRectangle)
    project_xy_to_image (XRectangle, YRectangle, MeasurementPlaneAdjusted, CameraParam, RowR, ColumnR)
    Area := Length1R * Length2R * 4 * 1000 * 1000
    disp_message (WindowHandle, 'area=' + Area$'.2f', 'image', RowR, ColumnR - 120, 'black', 'true')
    Message := 'Measured metric results after alignment (r in mm, area in mm^2):'
    * 
    disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
    if (I < 5)
        disp_continue_message (WindowHandle, 'black', 'true')
    endif
    stop ()
endfor
* Clean up memory
clear_metrology_model (MetrologyHandle)
if (AlignmentMode == 'shape-based matching')
    clear_shape_model (ShapeModelID)
    set_system ('border_shape_models', BorderShapeModel)
endif
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值