Halcon示例001---varaition_model_single 利用单张图像创建可变模板

Posted yangmengke2018

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Halcon示例001---varaition_model_single 利用单张图像创建可变模板相关的知识,希望对你有一定的参考价值。

  1 * 
  2 * This example shows how to employ the new extensions of HALCONs variation model operators
  3 * to perform customary print quality tests.
  4 * In this example the variation model is built upon a single reference image.
  5 * The example consists of three steps:
  6 * 1. align the print objects similar to the reference image using a shape-based model
  7 * 2. define the variation image by smoothing the objects contours
  8 * 3. create the variation model
  9 * Whether a print is labelled as OK or not, depends upon the size (area) of the difference to the reference image
 10 * 
 11 dev_close_window ()
 12 read_image (Image, pen/pen-01)
 13 get_image_size (Image, Width, Height)
 14 dev_open_window (0, 0, Width, Height, black, WindowHandle)
 15 dev_update_off ()
 16 set_display_font (WindowHandle, 12, mono, true, false)
 17 dev_display (Image)
 18 * 
 19 * segment the logo and create a shape model for the alignment
 20 threshold (Image, Region, 125, 255)
 21 fill_up (Region, RegionFillUp)
 22 difference (RegionFillUp, Region, RegionDifference)
 23 shape_trans (RegionDifference, LogoArea, convex)
 24 dilation_circle (LogoArea, LogoArea, 7)
 25 reduce_domain (Image, LogoArea, ImageReduced)
 26 create_shape_model (ImageReduced, auto, -rad(10), rad(20), auto, auto, use_polarity, [40,50], 40, ShapeModelID)
 27 area_center (LogoArea, Area, ModelRow, ModelColumn)
 28 * 
 29 * define the variation image by smoothing the dilated regions obtained from the objects contours:
 30 * Besides a binomial filter a neat trick is applied to get smoothly "polished" regions along the contours.
 31 * In particular, the edges are enlarged and after their conversion into a dilated region the image
 32 * is zoomed back to its original size using a weighting that smoothes the images further.
 33 *由Img区域提取轮廓边缘
 34 edges_sub_pix (ImageReduced, Edges, sobel_fast, 0.5, 10, 20)
 35 *生成一个空矩阵
 36 hom_mat2d_identity (HomMat2DIdentity)
 37 *将空矩阵放大4倍,未做偏移
 38 hom_mat2d_scale (HomMat2DIdentity, 4, 4, 0, 0, HomMat2DScale)
 39 *将轮廓边缘放大四倍
 40 affine_trans_contour_xld (Edges, ZoomedEdges, HomMat2DScale)
 41 *生成一个是原来图像四倍大的空图像
 42 gen_image_const (VarImageBig, byte, 4 * Width, 4 * Height)
 43 *计算轮廓的个数
 44 count_obj (ZoomedEdges, NEdges)
 45 *遍历边缘轮廓线
 46 for i := 1 to NEdges by 1
 47     *选择每一个轮廓线
 48     select_obj (ZoomedEdges, ObjectSelected, i)
 49     *得到轮廓线坐标
 50     get_contour_xld (ObjectSelected, RowEdge, ColEdge)
 51     *利用轮廓线坐标生成Reg
 52     gen_region_polygon (Region1, RowEdge, ColEdge)
 53     *膨胀轮廓线生成的Reg
 54     dilation_circle (Region1, RegionDilation, 2.5)
 55     *将膨胀后的Reg以像素值为255,绘制在生成的空图像中
 56     paint_region (RegionDilation, VarImageBig, VarImageBig, 255, fill)
 57 endfor
 58 *又将放了四倍大的图像缩小至原来大小
 59 zoom_image_size (VarImageBig, VarImageSmall, Width, Height, weighted)
 60 *图像平滑
 61 binomial_filter (VarImageSmall, VarImage, 3, 3)
 62 *【核心算子一】创建一个可变模板句柄
 63 create_variation_model (Width, Height, byte, direct, VarModelID)
 64 *【核心算子二】准备一个变化模型,以便与图像进行比较。
 65 prepare_direct_variation_model (Image, VarImage, VarModelID, 15, 4)
 66 dev_display (VarImage)
 67 disp_message (WindowHandle, Variation Image, window, 12, 12, black, true)
 68 disp_continue_message (WindowHandle, black, true)
 69 stop ()
 70 * 
 71 * print inspection
 72 for i := 1 to 30 by 1
 73     read_image (Image, pen/pen- + i$02d)
 74     * locate the logo and align it to the reference image
 75     find_shape_model (Image, ShapeModelID, -rad(10), rad(20), 0.5, 1, 0.5, least_squares, 0, 0.9, Row, Column, Angle, Score)
 76     if (|Score| != 0)
 77         vector_angle_to_rigid (Row, Column, Angle, ModelRow, ModelColumn, 0, HomMat2D)
 78         affine_trans_image (Image, ImageAffineTrans, HomMat2D, constant, false)
 79         reduce_domain (ImageAffineTrans, LogoArea, ImageReduced1)
 80         *【核心算子三】
 81         compare_ext_variation_model (ImageReduced1, RegionDiff, VarModelID, absolute)
 82         *得到不同,后续处理
 83         connection (RegionDiff, ConnectedRegions)
 84         select_shape (ConnectedRegions, SelectedRegions, area, and, 10, 99999)
 85         * 
 86         dev_display (ImageAffineTrans)
 87         count_obj (SelectedRegions, NDefects)
 88         if (NDefects > 0)
 89             dev_set_color (red)
 90             dev_set_draw (margin)
 91             dev_set_line_width (2)
 92             dev_display (SelectedRegions)
 93             dev_set_color (green)
 94             dev_set_line_width (1)
 95             dev_display (Edges)
 96             disp_message (WindowHandle, Image check not OK, window, 12, 12, red, false)
 97         else
 98             disp_message (WindowHandle, Image check OK, window, 12, 12, green, false)
 99         endif
100     endif
101     disp_continue_message (WindowHandle, black, true)
102     stop ()
103 endfor
104 * 
105 * clean up
106 clear_shape_model (ShapeModelID)
107 clear_variation_model (VarModelID)

  可以记录的小技巧:

  1. 差分形态学创建Shape Model
  2. 生成轮廓转Region
  3. 生成新图像添加Region
  4. 图像的放大与缩小
  5. 二值化图像滤波器

  可变模板核心算子:

  1. *【核心算子一】创建一个可变模板句柄
    create_variation_model (Width, Height, ‘byte‘, ‘direct‘, VarModelID)

  2. *【核心算子二】准备一个变化模型,以便与图像进行比较。
    prepare_direct_variation_model (Image, VarImage, VarModelID, 15, 4)

  3. *【核心算子三】比较得到不同区域
    compare_ext_variation_model (ImageReduced1, RegionDiff, VarModelID, ‘absolute‘)

  4. *清除内存
    clear_variation_model (VarModelID)

  5. *保存
    write_variation_model (VarModelID, ‘vara.vam‘)

  6. *读取
    read_variation_model (‘vara.vam‘, VarModelID)

  

 

以上是关于Halcon示例001---varaition_model_single 利用单张图像创建可变模板的主要内容,如果未能解决你的问题,请参考以下文章

Halcon示例003---matching/synthetic_circle 合成圆寻找电容

Halcon示例001---varaition_model_single 利用单张图像创建可变模板

halcon 激光三角测量 (片光技术)标定 ,示例 calibrate_sheet_of_light_3d_calib_object.hdev

halcon自带实例在哪个文件

Halcon - 如何设置白平衡

机器视觉Qt联合Halcon编程之显示多图片