****************************************************************************************************
* Halcon例程学习:print_check.hev(光学字符检测)
* 光学字符检测:如何将变异模式(variation model)应用于典型印刷质量检测.
* 具体内容:例程用于检测钢笔笔夹上的不正确印刷.
*         首先,从正确印刷中创建变异模型。由于目标位置可以多种多样,因此需要先将图像变换到相关位置(如例子中首幅图像的
*         印刷目标位置)。HALCON的基于形状匹配算法可用于检测图像中印刷目标的位置和角度,计算出的位置和角度可用于变换图像
*         到参考位置。算法第二部分是对正确印刷图案或几种错误类型的印刷图案的检测和分类。
* 联系扣扣:370711753
* 创作时间:2014/11/25
* 修改时间:2014/11/25
****************************************************************************************************
dev_update_pc ('off')   // 停止刷新PC
dev_update_window ('off')   // 停止刷新窗口
dev_update_var ('off')   // 停止刷新变量
read_image (Image, './images/pen/pen-01')  // 读取图像
get_image_size (Image, Width, Height)    // 获得图像大小
dev_close_window ()   // 关闭窗口
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)   // 创建新窗口
get_system ('operating_system', OS)   // 获取操作系统类型
if (OS{0:2} = 'Win')   // Windows操作系统
    set_font (WindowHandle, '-Courier New-18-*-*-*-*-')
else   // 其他操作系统
    set_font (WindowHandle, '-adobe-courier-bold-r-normal--25-*-*-*-*-*-*-*')
endif
dev_set_color ('red')   // 设置显示颜色
dev_display (Image)    // 显示图像
****************************************************************************************************
* 注意:形状模型(shape model)将由ROI区域创建,该ROI是根据简单阈值分割自动计算出的区域.
* Note: the shape model will be constructed from a ROI that is computed
* automatically based on a simple image segmentation.
****************************************************************************************************
* threshold(Image : Region : MinGray, MaxGray : ) 
* MinGray <= g <=  MaxGray
threshold (Image, Region, 100, 255)  // 固定阈值分割
fill_up (Region, RegionFillUp)   // 区域填充
difference (RegionFillUp, Region, RegionDifference)   // 区域做差
* shape_trans(Region : RegionTrans : Type : ) 
* List of values: 'convex', 'ellipse', 'outer_circle', 'inner_circle',
*                 'rectangle1', 'rectangle2', 'inner_rectangle1', 'inner_center'
shape_trans (RegionDifference, RegionTrans, 'convex')   // 区域形状变换
dilation_circle (RegionTrans, RegionDilation, 8.5)      // 区域圆形膨胀
reduce_domain (Image, RegionDilation, ImageReduced)     // 区域缩小
* inspect_shape_model:创建形状模型.
* inspect_shape_model(Image : ModelImages, ModelRegions : NumLevels, Contrast : ) 
* NumLevels:金字塔水平,默认为4;
* Contrast:Threshold or hysteresis thresholds for the contrast of the object in the image and optionally minimum size of the object parts.
inspect_shape_model (ImageReduced, ModelImages, ModelRegions, 1, 20)
* gen_contours_skeleton_xld:将骨架转换为XLD轮廓.
* gen_contours_skeleton_xld(Skeleton : Contours : Length, Mode : ) 
gen_contours_skeleton_xld (ModelRegions, Model, 1, 'filter')
* area_center:获得区域面积和区域中心.
area_center (RegionDilation, Area, RowRef, ColumnRef)
* create_shape_model:创建用于匹配的形状模型.
*create_shape_model(Template : : NumLevels, AngleStart, AngleExtent, AngleStep, Optimization, Metric, Contrast, MinContrast : ModelID) 
* Template:用于创建模型的图像区域.
* NumLevels:金字塔水平的最大值.默认值为:'auto'.
* AngleStart:模型的最小旋转弧度,默认值为:-0.39.
* AngleExtent:模型旋转弧度的范围,默认值为:0.79.
* AngleStep:旋转弧度的步长(分辨率),默认值为:'auto',约束条件:(AngleStep >= 0) && (AngleStep <= (pi / 16)) 
* Optimization:优化选项,默认值为:'auto'.
* Metric:匹配度量(Match metric),默认值为:'use_polarity'.
* Contrast:Threshold or hysteresis thresholds for the contrast of the object in the template image and optionally minimum size of the object parts.
* MinContrast:Minimum contrast of the objects in the search images. Restriction: MinContrast < Contrast.
* ModelID:Handle of the model.
create_shape_model (ImageReduced, 5, rad(-10), rad(20), 'auto', 'none', 'use_polarity', 20, 10, ShapeModelID)
* create_variation_model:创建图像比较的变异模型(variation model).
create_variation_model (Width, Height, 'byte', 'standard', VariationModelID)
for I := 1 to 15 by 1
    read_image (Image, './images/pen/pen-'+I$'02d')  // 读取图像
    * find_shape_model:查找图像中形状模型的最佳匹配.
    * find_shape_model(Image : : ModelID, AngleStart, AngleExtent, MinScore, NumMatches, MaxOverlap, SubPixel, NumLevels, Greediness : Row, Column, Angle, Score) 
    find_shape_model (Image, ShapeModelID, rad(-30), rad(60), 0.5, 1, 0.5, 'least_squares', 0, 0.9, Row, Column, Angle, Score)
    if (|Score| = 1)
        * vector_angle_to_rigid:根据点和角度进行刚性仿射变换(rigid affine transformation).
        * vector_angle_to_rigid( : : Row1, Column1, Angle1, Row2, Column2, Angle2 : HomMat2D) 
        vector_angle_to_rigid (Row, Column, Angle, RowRef, ColumnRef, 0, HomMat2D)
        * affine_trans_image:对图像进行任意2D仿射变换.
        * affine_trans_image(Image : ImageAffinTrans : HomMat2D, Interpolation, AdaptImageSize : ) 
        affine_trans_image (Image, ImageTrans, HomMat2D, 'constant', 'false')
        * train_variation_model:训练变异模型(variation model).
        * train_variation_model(Images : : ModelID : ) 
        train_variation_model (ImageTrans, VariationModelID)
        dev_display (ImageTrans) // 显示仿射变换后图像
        dev_display (Model)   // 显示轮廓线
    endif
endfor
* get_variation_model — Return the images used for image comparison by a variation model.
* get_variation_model( : Image, VarImage : ModelID : ) 
get_variation_model (MeanImage, VarImage, VariationModelID)
* prepare_variation_model — Prepare a variation model for comparison with an image.
* prepare_variation_model( : : ModelID, AbsThreshold, VarThreshold : ) 
prepare_variation_model (VariationModelID, 20, 3)
* clear_train_data_variation_model — Free the memory of the training data of a variation model.
* clear_train_data_variation_model( : : ModelID : ) 
clear_train_data_variation_model (VariationModelID)
****************************************************************************************************
* 注意:印刷检测将被限制在笔夹范围内. 有时笔夹的印刷位置也可能是错误的,这将导致在笔夹的顶部或底部有处明显错误区域,
*      该区域很容易被检测出来.
****************************************************************************************************
* erosion_rectangle1:基于矩形结构元素的区域腐蚀.
* erosion_rectangle1(Region : RegionErosion : Width, Height : ) 
erosion_rectangle1 (RegionFillUp, RegionROI, 1, 15)
dev_display (MeanImage)   // 显示平均值图像
set_tposition (WindowHandle, 20, 20)   // 设置文本光标位置
dev_set_color ('green')   // 设置显示颜色
write_string (WindowHandle, 'Reference image')   // 输出字符串
stop ()   // 停止,按F5可继续运行
dev_display (VarImage)   // 显示方差图像
set_tposition (WindowHandle, 20, 20)   // 设置文本光标位置
dev_set_color ('green')   // 设置显示颜色
write_string (WindowHandle, 'Variation image')   // 输出字符串
stop ()   // 停止,按F5可继续运行
dev_set_draw ('margin')   // 设置绘图模式
for I := 1 to 30 by 1
    read_image (Image, './images/pen/pen-'+I$'02d')  // 读取图像
    * find_shape_model:查找图像中形状模型的最佳匹配.
    * find_shape_model(Image : : ModelID, AngleStart, AngleExtent, MinScore, NumMatches, MaxOverlap, SubPixel, NumLevels, Greediness : Row, Column, Angle, Score) 
    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:根据点和角度进行刚性仿射变换(rigid affine transformation).
        * vector_angle_to_rigid( : : Row1, Column1, Angle1, Row2, Column2, Angle2 : HomMat2D) 
        vector_angle_to_rigid (Row, Column, Angle, RowRef, ColumnRef, 0, HomMat2D)
        * affine_trans_image:对图像进行任意2D仿射变换.
        * affine_trans_image(Image : ImageAffinTrans : HomMat2D, Interpolation, AdaptImageSize : ) 
        affine_trans_image (Image, ImageTrans, HomMat2D, 'constant', 'false')
        reduce_domain (ImageTrans, RegionROI, ImageReduced)     // 区域缩小
        * compare_variation_model — Compare an image to a variation model.
        * compare_variation_model(Image : Region : ModelID : ) 
        compare_variation_model (ImageReduced, RegionDiff, VariationModelID)
        * connection — Compute connected components of a region.
        * connection(Region : ConnectedRegions : : )
        connection (RegionDiff, ConnectedRegions)
        * select_shape — Choose regions with the aid of shape features.
        * select_shape(Regions : SelectedRegions : Features, Operation, Min, Max : ) 
        select_shape (ConnectedRegions, RegionsError, 'area', 'and', 20, 1000000)
        count_obj (RegionsError, NumError)   // 计算目标个数
        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
    stop ()
endfor
stop ()
* clear_shape_model — Free the memory of a shape model.
* clear_shape_model( : : ModelID : ) 
clear_shape_model (ShapeModelID)
* clear_variation_model — Free the memory of a variation model.
* clear_variation_model( : : ModelID : ) 
clear_variation_model (VariationModelID)

Halcon例程学习:print_check.hev(光学字符检测)相关推荐

  1. Halcon例程学习:adaption_ocv.hev(光学字符检测)

    **************************************************************************************************** ...

  2. 中文OCR光学字符检测与识别二:用最先进的DBNet训练自己的数据集检测中文文本

    中文OCR光学字符检测与识别二:用最先进的DBNet训练自己的数据集检测中文文本 本文介绍 中文OCR光学字符检测与识别二:用最先进的DBNet训练自己的数据集检测中文文本 中文OCR光学字符检测与识 ...

  3. halcon例程学习笔记(8)---瓶子编号识别bottle2.hdev

    通过此例程的学习主要学会了一般字符识别的流程  主要是:图像预处理----字符分割---字符识别 本例程主要难点在与字符分割方面,例程很好的展示了如何对未知字符,位置间隔等不均匀,位置不确定的字符如何 ...

  4. halcon例程学习 一维码、二维码识别

    1.一维条码 一维条码:由一组规则排列的条.空以及对应的字符组成的标记,"条"指对光线反射率较低的部分,"空"指对光线反射率较高的部分,这些条和空组成的数据表达 ...

  5. Halcon 例程学习之频域自相关变换( correlation_fft)

    网页图片不清晰,请另存到电脑后,以图片方式学习!

  6. Halcon例程学习之距离变换(distance_transform)

    网页图片不清晰,请另存到电脑后,以图片方式学习!

  7. Halcon例程学习:align_measurements

    来源是算子-一维测量-close_measure 例程介绍的翻译: 使用基于形状的匹配来检查单个剃须刀片,以对齐测量工具的 RoI 文件头介绍的翻译: *此示例程序使用基于形状的匹配来对齐度量的 RO ...

  8. Halcon学习笔记(九)——OCR实战练习 倾斜日期检测、倒着的字符检测

    第四-八讲 OCR实战练习 在基于之前的例程分析之上,这里做具体应用,比如,食品包装袋上倾斜的日期识别,温度计上倒着的字符识别等. 倾斜日期识别 首先,对于这样一幅图片,怎样实现对日期的提取? 法一: ...

  9. 通过 Land of Lisp 中的超简短字符游戏例程学习 loop 和 format

    2019独角兽企业重金招聘Python工程师标准>>> 通过 Land of Lisp 中的超简短字符游戏例程学习 Common Lisp 的 loop 和 format 介绍 在 ...

最新文章

  1. ctf安全竞赛入门pdf_CTF安全竞赛入门
  2. Visiual Studio2012 CLR20r3问题
  3. .NET Core 使用 K8S ConfigMap的正确姿势
  4. discuz数据从godaddy主机中导出的mysql数据乱码变问号???的解决方法
  5. Leetcode--全排列(Java)
  6. DUMP文件分析4:栈溢出
  7. JVM:我就想知道我是怎么没的
  8. iOS-文件断点续传
  9. 最短路径例题(Floyd、Dijkstra)
  10. python 读取文件夹下文件,将文件名作为制作标签,训练样本
  11. java 文字串叠字检查_Java 正则表达式详细实例解析
  12. ubuntu设置自动关机
  13. App Inventor学习环境搭建
  14. mysql筛选两个不同表的数据
  15. 透过脉脉的新闻“线”场看职场
  16. 政务内网不能上网的解决办法
  17. 第一次将项目push到gitlab
  18. freemarker基于docx格式创建模板导出带图片pdf文件
  19. 微信公众号 - 下拉(展开/隐藏)
  20. 贝叶斯分类(这个讲的比较清晰,一看就明白)

热门文章

  1. Framework7——基础工具类
  2. layui之获取form表单的radio
  3. Leetcode 169 Majority Element
  4. ASP.NET学习顺序(转摘)
  5. 在线文字图标logo文章封面图生成工具
  6. Android源码分析之 JobScheduler
  7. vmware cli 修改磁盘为SSD
  8. 《Arduino开发实战指南:机器人卷》一2.4 时间函数
  9. Storm概念学习系列之Stream消息流 和 Stream Grouping 消息流组
  10. idea 使用时的一些问题