halcon 代码注释 print_check.hdev

  • This example program shows how to use HALCON’s variation model operators
  • to perform a typical print quality inspection. The program detects incorrect
  • prints on the clips of pens. In the first step, the variation model is constructed
  • from images of correct prints. Since the position of the objects can vary, the
  • images must be transformed to a reference position (the position of the print
  • in the first image in this example). HALCON’s shape-based matching is 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.
  • In the second part of the program, the prints of the correct clips and of several
  • incorrect clips is checked and classified.
    dev_update_off ()*将显示模式相关的参数,设置为OFF
    read_image (Image, ‘pen/pen-01’)*读取一幅图像
    get_image_size (Image, Width, Height)*获取图像的宽和高
    dev_close_window ()*关闭活动窗口.
    dev_open_window (0, 0, Width, Height, ‘black’, WindowHandle)*打开一个新的图像窗口
    set_display_font (WindowHandle, 16, ‘mono’, ‘true’, ‘false’)*设置以来于系统的字体
    dev_set_color (‘red’)*设置输出的轮廓线条灯的颜色
    dev_display (Image)*在当前窗口中显示图像
  • Note: the shape model will be constructed from a ROI that is computed
  • automatically based on a simple image segmentation.
  • 注意:形状模型将由基于简单图像分割自动计算的 ROI 构建。
    threshold (Image, Region, 100, 255)*使用全局阈值,分割二值化图像.保留的前景区域为 在100 255 之间的像素区域
    fill_up (Region, RegionFillUp)*填充区域的空洞.
    difference (RegionFillUp, Region, RegionDifference)*求区域之间的差异
    shape_trans (RegionDifference, RegionTrans, ‘convex’)*将形状按指定方式转换成region
    dilation_circle (RegionTrans, RegionDilation, 8.5)以圆为kernel膨胀
    reduce_domain (Image, RegionDilation, ImageReduced)
    从原图中裁剪指定区域
    inspect_shape_model (ImageReduced, ModelImages, ModelRegions, 1, 20)*创建形状模型的表示. 会得到相应的图像和轮廓
    gen_contours_skeleton_xld (ModelRegions, Model, 1, ‘filter’)*将骨架转换为 XLD 轮廓。
    area_center (RegionDilation, Area, RowRef, ColumnRef)求该区域的zrea center
    create_shape_model (ImageReduced, 5, rad(-10), rad(20), ‘auto’, ‘none’, ‘use_polarity’, 20, 10, ShapeModelID)
    准备一个用于匹配的形状模型.输出的是一个模型的id
    create_variation_model (Width, Height, ‘byte’, ‘standard’, VariationModelID)*创建用于图像比较的变体模型。
    for I := 1 to 15 by 1 *意义明确的循环
    read_image (Image, ‘pen/pen-’ + I$‘02d’)读图
    find_shape_model (Image, ShapeModelID, rad(-10), rad(20), 0.5, 1, 0.5, ‘least_squares’, 0, 0.9, Row, Column, Angle, Score)
    在一个图像中,找到图像模型的最佳匹配.
    if (|Score| == 1) *|Score | 求其中元素的数量. | 0 | 也是==1的
    vector_angle_to_rigid (Row, Column, Angle, RowRef, ColumnRef, 0, HomMat2D)*从点和角度计算刚性仿射变换。
    affine_trans_image (Image, ImageTrans, HomMat2D, ‘constant’, ‘false’)对图像应用任意仿射 2D 变换。
    train_variation_model (ImageTrans, VariationModelID)
    训练变体模型
    dev_display (ImageTrans)
    dev_display (Model)
    endif
    endfor
    get_variation_model (MeanImage, VarImage, VariationModelID)*返回变体模型用于图像比较的图像。输入是变体模型的id
    prepare_variation_model (VariationModelID, 20, 3)*准备一个变体模型以与图像进行比较。
  • 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.
    erosion_rectangle1 (RegionFillUp, RegionROI, 1, 15)* 用矩形结构元素 腐蚀结构体
    dev_display (MeanImage)
    set_tposition (WindowHandle, 20, 20)设置文本光标的位置。
    dev_set_color (‘green’)
    设置输出颜色
    write_string (WindowHandle, ‘Reference image’)在窗口中打印字符串
    disp_continue_message (WindowHandle, ‘black’, ‘true’)此过程在屏幕右下角显示“单击“运行”以继续”。
    stop ()
    暂停程序
    dev_display (VarImage)
    set_tposition (WindowHandle, 20, 20)
    dev_set_color (‘green’)
    write_string (WindowHandle, ‘Variation image’)
    disp_continue_message (WindowHandle, ‘black’, ‘true’)
    stop ()
    dev_set_draw (‘margin’)
    Define the region fill mode.
    NumImages := 30* 定义一个变量表示图像数量
    for I := 1 to 30 by 1
    read_image (Image, ‘pen/pen-’ + I$‘02d’)
    find_shape_model (Image, ShapeModelID, rad(-10), rad(20), 0.5, 1, 0.5, ‘least_squares’, 0, 0.9, Row, Column, Angle, Score)
    if (|Score| == 1)
    vector_angle_to_rigid (Row, Column, Angle, RowRef, ColumnRef, 0, HomMat2D)
    affine_trans_image (Image, ImageTrans, HomMat2D, ‘constant’, ‘false’)
    reduce_domain (ImageTrans, RegionROI, ImageReduced)
    compare_variation_model (ImageReduced, RegionDiff, VariationModelID)* 将图像和变体模型比较
    connection (RegionDiff, ConnectedRegions)*计算区域的连通域
    select_shape (ConnectedRegions, RegionsError, ‘area’, ‘and’, 20, 1000000)*选择在指定标准范围内的的特征.
    count_obj (RegionsError, NumError)*返回tuple中的目标数量.
    dev_clear_window ()清空当前激活的图像窗口的内容.
    dev_display (ImageTrans)
    显示放射变换后的图像
    dev_set_color (‘red’)
    dev_display (RegionsError)
    set_tposition (WindowHandle, 20, 20)
    if (NumError == 0)
    dev_set_color (‘green’)
    write_string (WindowHandle, ‘Clip OK’)
    else
    dev_set_color (‘red’)
    write_string (WindowHandle, ‘Clip not OK’)
    endif
    endif
    if (I < NumImages)
    disp_continue_message (WindowHandle, ‘black’, ‘true’)
    stop ()
    endif
    endfor

*因为halcon将函数封装的很好,所以才有了这种情况

你可能感兴趣的:(halcon,halcon,代码注释,计算机视觉,图像处理)