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

  1 *
  2 * This example shows how to employ the new extensions of HALCON‘s 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 object‘s 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 object‘s 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)

  

原文地址:https://www.cnblogs.com/yangmengke2018/p/11734205.html

时间: 2024-10-12 13:42:50

【Halcon示例001---varaition_model_single】 利用单张图像创建可变模板的相关文章

Expression Blend 的点滴(2)--利用可视化状态创建神奇翻转动画

原文:Expression Blend 的点滴(2)--利用可视化状态创建神奇翻转动画 首先,来看下实现后的效果: 关于VisulaState VisualState 指定控件处于特定状态时的外观.例如,按下 Button 时,它的边框颜色可能与正常时的颜色不同.VisualState 类具有更改控件外观的 Storyboard属性.控件进入 VisualState.Name 属性指定的状态时,Storyboard 开始.控件退出该状态时,Storyboard 停止. 以上是silverligh

利用Zynq Soc创建一个嵌入式工程

英文题目:Using the Zynq SoC Processing System,参考自ADI的ug1165文档. 利用Zynq Soc创建一个嵌入式工程,该工程总体上包括五个步骤: 步骤一.新建空白工程 步骤二.创建一个Embedded Processor工程 步骤三.Zynq7 Processing System的管理 步骤四.综合仿真.编译运行.生成二进制文件 步骤五.Exporting Hardware to SDK 步骤一.新建工程 1. 点击Vivado图标启动软件,Create

Linux上利用nmcli命令创建网络组

网络组:是将多个网卡聚合在一起方法,从而实现冗错和提高吞吐量 网络组不同于旧版中bonding技术,提供更好的性能和扩展性 网络组由内核驱动和teamd守护进程实现. 下面我们以CentOS7系统为环境,演示下如何在Linux系统上利用nmcli命令创建网络组 nmcli connection add con-name team0 type team ifname team0 config '{"runner":{"name":"activebackup&

MVVM下 利用反射动态创建ViewModel 实现单例

在MVVM一般情况下都会希望ViewModel 在整个应用程序中只有一份实例 传统的做法是用单例模式去实现 : public class ViewModelTest { private ViewModelTest() { } private static ViewModelTest viewModelInstace; public static ViewModelTest GetViewModelTestInstace() { if (viewModelInstace == null) { vi

利用JavaScript如何创建一个table表格[第2种方法]

创建一个五行五列的表格(使用循环) <style> td{border:1px solid #ccc;} </style> <script> window.onload=function(){ var oTable=document.createElement("table"); var row; var cell; for(var i=0;i<5;i++){ row=document.createElement("tr")

利用sc命令创建服务

创建 >sc create TestService binPath= "D:\EngineService\TestService.exe" start= auto[SC] CreateService 成功>net start TestService 注意:binPath= 后面有一个空格 删除 >sc delete TestService 利用sc命令创建服务,布布扣,bubuko.com

利用JavaScript如何创建一个table表格

创建Table标签和定义表格头部份代码: window.onload=function(){ var arr=["编号","性别","姓名","年龄"]; var oDatas=[ { "id":1 , "gender":"男", "name" : "Javascript" , "age":30 }, { &

Jquery利用JSON数组创建表格

$(function () { var json = [ { "name": "张三", "age": "20", "gender": "男" }, { "name": "李四", "age": "21", "gender": "女" }, { "name&q

sap利用VMD_EI_API=&gt;MAINTAIN创建供应商主数据

FRM_VENDOR_CREATE. DATA:LV_FLG. DATA:LS_MAIN TYPE VMDS_EI_MAIN, LS_MESG TYPE CVIS_MESSAGE, LT_MSG TYPE BAPIRET2_T, LWA_MSG TYPE BAPIRET2, LT_VEND TYPE VMDS_EI_EXTERN_T, LWA_VEND TYPE VMDS_EI_EXTERN, LT_COMPANY TYPE VMDS_EI_COMPANY_T, LWA_COMPANY TYPE