point_line_to_hom_mat2d.hdev

* This example program shows how to align images based on point-to-line correspondences. 
*HALCON's metrology model and point_line_to_hom_mat2d are used to detect the position and angle of the print in the images. The found position and angle are used to transform the images to the reference position (theposition of the print in the first image in this example).
* From the aligned images of correct prints, a variation model is constructed.
* This variation model is then used to check and classify images of correct and incorrect prints.
*这个示例程序展示了如何基于点对线对应来对齐图像。
*HALCON的计量模型和point_line_to_hom_mat2d用于检测图像中打印的位置和角度。
*找到的位置和角度用于将图像转换为参考位置(本例中第一个图像中的打印位置)。
*根据正确印刷品的对齐图像,构建变异模型。
*这个变异模型然后用于检查和分类正确和不正确的打印图像。
dev_update_off ()
read_image (Image, 'pen/pen-01')
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window (0, Width + 12, Width, Height, 'black', WindowHandle)
dev_open_window (0, 0, Width, Height, 'black', WindowHandleAlign)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
set_display_font (WindowHandleAlign, 14, 'mono', 'true', 'false')
dev_set_color ('red')
dev_display (Image)
* Note: the checking of the print will be restricted to the region of the clip.
* Sometimes the print is also in an incorrect position of the clip. 
*This will lead to erroneous regions at the top or bottom border of the clip and hence can be detected easily.
*注意:打印的检查将被限制在夹子的区域。
*有时打印也在一个不正确的位置的剪辑。
*这将导致在剪辑的顶部或底部边界的错误区域,因此可以很容易地检测到。
threshold (Image, Region, 100, 255)
fill_up (Region, RegionFillUp)
erosion_rectangle1 (RegionFillUp, RegionROI, 1, 15)
* Set up the metrology model with four lines.  Four lines are used since this is the minimum number of point-to-line correspondences that results in a unique rigid transformation.
**用四条线建立计量模型。使用了四条线,因为这是导致唯一刚性转换的点到线对应的最小数量。
create_metrology_model (MetrologyHandle)
set_metrology_model_image_size (MetrologyHandle, Width, Height)
add_metrology_object_line_measure (MetrologyHandle, [208,137,213,90], [49,327,139,307], [100,201,214,89], [82,318,56,375], 40, 5, 1, 30, ['measure_transition','min_score'], ['negative',0.5], Index)
dev_set_color ('green')
disp_cross (WindowHandleAlign, [208,137,213,90],[49,327,139,307], 60, 0)
disp_cross (WindowHandleAlign, [100,201,214,89],[82,318,56,375], 60, 0)

*add_metrology_object_line_measure( : : MetrologyHandle, RowBegin,ColumnBegin, RowEnd, ColumnEnd, MeasureLength1, MeasureLength2,MeasureSigma, MeasureThreshold, GenParamName, GenParamValue : Index)
   *描述:添加一个直线型测量对象到计量模型中。
   *参数:
   **MetrologyHandle:计量模型句柄
   *RowBegin:直线起始行坐标
   *ColumnBegin:直线起始列坐标
   *RowEnd:直线终止行坐标
   *ColumnEnd:直线终止列坐标
   *MeasureLength1:测量区域和直线边界垂直的边的一半
   *MeasureLength2:测量区域另一边的一半
   *MeasureSigma:高斯平滑算法的Sigma值
   *MeasureThreshold:边缘阈值
   *GenParamName:一般参数名称
   *GenParamValue :一般参数的值
   *Index:创建的测量模型的索引值
* Apply the metrology model to the reference image and read out the results.
*将计量模型应用于参考图像并读出结果。
apply_metrology_model (Image, MetrologyHandle)
get_metrology_object_result (MetrologyHandle, 'all', 'all', 'result_type', 'row_begin', RowBegin)
get_metrology_object_result (MetrologyHandle, 'all', 'all', 'result_type', 'column_begin', ColBegin)
get_metrology_object_result (MetrologyHandle, 'all', 'all', 'result_type', 'row_end', RowEnd)
get_metrology_object_result (MetrologyHandle, 'all', 'all', 'result_type', 'column_end', ColEnd)
* The reference points of the model are the center points of the detected line segments.  They will be used to compute the transformation from the current image to the reference image using point_line_to_hom_mat2d below.
*模型的参考点为检测到的线段中心点。它们将用于使用下面的point_line_to_hom_mat2d计算从当前图像到参考图像的转换。
RowRef := 0.5 * (RowBegin + RowEnd)
ColRef := 0.5 * (ColBegin + ColEnd)
dev_set_window (WindowHandle)
dev_display (Image)
dev_set_window (WindowHandleAlign)
dev_display (Image)
*搜索框显示
get_metrology_object_measures (MeasureContours, MetrologyHandle, 'all', 'all', Row, Column)// 获取计量模型计量对象的测量区域和边缘定位结果。
* 1.Index(输入控制)计量对象的索引。建议值:'all' , 0, 1, 2    2. 选择亮/暗或暗/亮边缘。 值列表:'all'、'negative'、'positive'  
dev_set_color ('red')
dev_display (MeasureContours)
dev_set_color ('green')
disp_message (WindowHandleAlign, ['Measure objects of the four','metrology line objects'], 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
dev_set_window (WindowHandleAlign)
dev_display (Image)
*显示搜索到的线
get_metrology_object_result_contour (MeasuredLines, MetrologyHandle, 'all', 'all', 1.5)//查询测量对象的结果轮廓。
*1..Index(输入控制)计量对象的索引。建议值:'all' , 0, 1, 2   2. Instance(输入控制)  计量对象的实例。建议值:'all' , 0, 1, 2
*3.Resolution(输入控制)  相邻轮廓点之间的距离。默认值: 1.5 限制:Resolution >= 1.192e-7
gen_cross_contour_xld (RefPoints, RowRef, ColRef, 16, rad(45))// 为每个输入点生成一个十字形 XLD 轮廓。
dev_set_line_width (2)
dev_set_color ('green')
dev_display (MeasuredLines)
dev_set_color ('blue')
dev_display (RefPoints)
dev_set_line_width (1)
disp_message (WindowHandleAlign, ['Lines measured in the reference image','and reference points on the lines'], 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 创建变量模型。
* Create the variation model.
create_variation_model (Width, Height, 'byte', 'standard', VariationModelID)// 创建用于图像比较的变异模型。 Mode(输入控制)用于计算变异模型的方法。建议值:'standard'、'robust'、'direct'

*变化模型用于区分正确制造的对象(“好对象”)和不正确制造的对象(“坏对象”)。假设可以仅根据对象的灰度值来进行区分。
for I := 1 to 15 by 1
    read_image (Image, 'pen/pen-' + I$'02d')
    * Apply the metrology model to the current image and read out the line segment coordinates.
    *将计量模型应用于当前图像并读出线段坐标。
    apply_metrology_model (Image, MetrologyHandle)
    get_metrology_object_result (MetrologyHandle, 'all', 'all', 'result_type', 'row_begin', RowBegin)
    get_metrology_object_result (MetrologyHandle, 'all', 'all', 'result_type', 'column_begin', ColBegin)
    get_metrology_object_result (MetrologyHandle, 'all', 'all', 'result_type', 'row_end', RowEnd)
    get_metrology_object_result (MetrologyHandle, 'all', 'all', 'result_type', 'column_end', ColEnd)
    * Determine a rigid transformation based on the point-to-line correspondences from the reference points to the extracted lines.  Note that this determines a transformation from the reference points to the lines in the current image.
    * Therefore, we must invert this transformation to obtain the transformation from the current image to the rerefence image.
    *根据参考点到提取线的点对线对应关系确定刚性变换。注意,这决定了从参考点到当前图像中的线的转换。
    *因此,我们必须对这个变换进行反求,才能得到当前图像到参考图像的变换。
    *近似从点到线对应的仿射变换。
    point_line_to_hom_mat2d ('rigid', RowRef + 0.5, ColRef + 0.5, RowBegin + 0.5, ColBegin + 0.5, RowEnd + 0.5, ColEnd + 0.5, HomMat2D)
   *TransformationType(输入控制)值列表:'affine'、'aniso'、'rigid'、'similarity'、'translation'
   *原点的 X 坐标。 原点的 Y 坐标。相应线上第一个点的 X 坐标。 相应线上第一个点的 Y 坐标。相应线上第二个点的 X 坐标。 相应线上第二个点的 Y 坐标。输出变换矩阵。
   disp_cross (WindowHandleAlign,  RowRef + 0.5, ColRef + 0.5, 20, 0)
   dev_set_color ('red')
   disp_cross (WindowHandleAlign,  RowBegin + 0.5, ColBegin + 0.5, 20, 0)
   disp_cross (WindowHandleAlign,   RowEnd + 0.5, ColEnd + 0.5, 20, 0)
   hom_mat2d_invert (HomMat2D, HomMat2DInvert)
    affine_trans_image (Image, ImageTrans, HomMat2DInvert, 'constant', 'false')
    * Train the variation model with the transformed, i.e., aligned image.
    *用变换后的,即对齐后的图像训练变异模型。
    train_variation_model (ImageTrans, VariationModelID)// 训练变异模型
    *待训练对象的图像。 变体模型的 ID。。
    *如 所描述的,使用“标准”模式创建的变体模型可以进行迭代训练,即,一旦良好对象的图像可用,就可以使用 进行训练。
    *对象的理想图像被计算为所有先前训练图像和传入图像的平均值 。相应的变化图像被计算为训练图像和传入图像的标准差。
    dev_set_window (WindowHandleAlign)
    dev_display (Image)
    get_metrology_object_result_contour (MeasuredLines, MetrologyHandle, 'all', 'all', 1.5)
    dev_set_line_width (2)
    dev_set_color ('green')
    dev_display (MeasuredLines)
    dev_set_color ('blue')
    dev_display (RefPoints)
    dev_set_line_width (1)
    disp_message (WindowHandleAlign, ['Lines measured in the current测量电流的线','image and reference points图像和参考点'], 'window', 12, 12, 'black', 'true')
    dev_set_window (WindowHandle)
    dev_clear_window ()
    dev_display (ImageTrans)
    affine_trans_contour_xld (MeasuredLines, MeasuredLinesTrans, HomMat2DInvert)//对XLD轮廓应用任意仿射二维变换。
    dev_set_line_width (2)
    dev_set_color ('green')
    dev_display (MeasuredLinesTrans)
    dev_set_color ('blue')
    dev_display (RefPoints)
    dev_set_line_width (1)
    disp_message (WindowHandle, ['Measured lines transformed to the测量线转换为参考图像姿态和参考点','reference image pose and reference points'], 'window', 12, 12, 'black', 'true')
    disp_continue_message (WindowHandle, 'black', 'true')
    stop ()
endfor
get_variation_model (MeanImage, VarImage, VariationModelID)//返回变量模型用于图像比较的图像。
*1.输出 受训练对象的图像。2.输出 训练对象的变化图像。 ModelID(输入控制) 变体模型的 ID。
prepare_variation_model (VariationModelID, 20, 3) //准备一个变异模型以与图像进行比较。
*图像和变异模型之间差异的绝对最小阈值。   基于变异模型的变异的差异阈值。
*l准备一个变异模型,用于与或 进行图像比较。这是通过将经过训练的理想图像和变异图像转换 为两个阈值图像并将它们存储在变异模型中来完成的。这些阈值图像用于 或 加速当前图像与变化模型的比较。
*使用两个阈值来计算阈值图像。该参数确定当前对象的图像与理想对象的图像必须相差的最小灰度级量。
*该参数 确定当前图像与理想图像的最小差异相对于变化图像的因子。 每个可以包含一个或两个值。
*如果指定两个值,则可以为太亮和太暗的像素确定不同的阈值。在此模式下,第一个值指的是太亮的像素,而第二个值指的是太暗的像素。
*如果指定一个值,则该值指的是太亮和太暗的像素。设 i(x,y) 为理想图像,v(x,y) 为变异图像,
dev_set_window (WindowHandleAlign)
dev_display (MeanImage)
disp_message (WindowHandleAlign, 'Reference image', 'window', 12, 12, 'black', 'true')
dev_set_window (WindowHandle)
dev_display (VarImage)
disp_message (WindowHandle, 'Variation image', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 检查和分类正确和不正确的打印图像。
* Check and classify images of correct and incorrect prints.
NumImages := 30
for I := 1 to 30 by 1
    read_image (Image, 'pen/pen-' + I$'02d')
    apply_metrology_model (Image, MetrologyHandle)
    get_metrology_object_result (MetrologyHandle, 'all', 'all', 'result_type', 'row_begin', RowBegin)
    get_metrology_object_result (MetrologyHandle, 'all', 'all', 'result_type', 'column_begin', ColBegin)
    get_metrology_object_result (MetrologyHandle, 'all', 'all', 'result_type', 'row_end', RowEnd)
    get_metrology_object_result (MetrologyHandle, 'all', 'all', 'result_type', 'column_end', ColEnd)
    point_line_to_hom_mat2d ('rigid', RowRef + 0.5, ColRef + 0.5, RowBegin + 0.5, ColBegin + 0.5, RowEnd + 0.5, ColEnd + 0.5, HomMat2D)
    hom_mat2d_invert (HomMat2D, HomMat2DInvert)
    affine_trans_image (Image, ImageTrans, HomMat2DInvert, 'constant', 'false')
    reduce_domain (ImageTrans, RegionROI, ImageReduced)
    compare_variation_model (ImageReduced, RegionDiff, VariationModelID)// 将图像与变异模型进行比较。
    *1.Image(输入对象)要比较的对象的图像。2.Region(输出对象)包含与模型显着不同的点的区域 3. ModelID(输入控制)变体模型的 ID。
    connection (RegionDiff, ConnectedRegions)
    select_shape (ConnectedRegions, RegionsError, 'area', 'and', 20, 1000000)
    count_obj (RegionsError, NumError)
    dev_set_window (WindowHandle)
    dev_clear_window ()
    dev_display (ImageTrans)
    dev_set_color ('red')
    dev_set_draw ('margin')
    dev_display (RegionsError)
    disp_message (WindowHandle, 'Aligned image', 'window', 12, 12, 'black', 'true')
    if (NumError == 0)
        disp_message (WindowHandle, 'Print OK', 'window', 12, 300, 'green', 'false')
    else
        disp_message (WindowHandle, 'Print not OK', 'window', 12, 300, 'red', 'false')
    endif
    dev_set_window (WindowHandleAlign)
    dev_display (Image)
    get_metrology_object_result_contour (MeasuredLines, MetrologyHandle, 'all', 'all', 1.5)
    dev_set_line_width (2)
    dev_set_color ('green')
    dev_display (MeasuredLines)
    dev_set_color ('blue')
    dev_display (RefPoints)
    dev_set_line_width (1)
    disp_message (WindowHandleAlign, ['Lines measured in the current','image and reference points'], 'window', 12, 12, 'black', 'true')
    if (I < NumImages)
        disp_continue_message (WindowHandle, 'black', 'true')
        stop ()
    endif
endfor

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值