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

****************************************************************************************************
* 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)
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值