一、例程variation_model_single.hdev的解析
功能:检测印刷质量是否满足要求
例程的文件名:variation_model_single.hdev
代码解析
创建两个模型,一个是形状模型(用来查找兴趣区域的位置),一个是差异模型(用来对此印刷区别的)
*
* This example shows how to employ the new extensions of HALCON's variation model operators
* to perform customary print quality tests.
* In this example the variation model is built upon a single reference image.
* The example consists of three steps:
* 1. align the print objects similar to the reference image using a shape-based model
* 2. define the variation image by smoothing the object's contours
* 3. create the variation model
* Whether a print is labelled as OK or not, depends upon the size (area) of the difference to the reference image
*
dev_close_window ()
read_image (Image, 'pen/pen-01')
*原始图像的大小为451*301
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_update_off ()
set_display_font (WindowHandle, 12, 'mono', 'true', 'false')
dev_display (Image)
*
* segment the logo and create a shape model for the alignment
*将logo的区域裁剪出来,用来创建形状模型
threshold (Image, Region, 125, 255)
fill_up (Region, RegionFillUp)
difference (RegionFillUp, Region, RegionDifference)
*将区域变为贴近logo的多边形区域
shape_trans (RegionDifference, LogoArea, 'convex')
dilation_circle (LogoArea, LogoArea, 7)
reduce_domain (Image, LogoArea, ImageReduced)
*形状模型的角度从-10度到20度
create_shape_model (ImageReduced, 'auto', -rad(10), rad(20), 'auto', 'auto', 'use_polarity', [40,50], 40, ShapeModelID)
*计算logo区域的中心点坐标
area_center (LogoArea, Area, ModelRow, ModelColumn)
* define the variation image by smoothing the dilated regions obtained from the object's contours:
* Besides a binomial filter a neat trick is applied to get smoothly "polished" regions along the contours.
* In particular, the edges are enlarged and after their conversion into a dilated region the image
* is zoomed back to its original size using a weighting that smoothes the images further.
*边缘提取,Edges轮廓点为334,长度为343.83
edges_sub_pix (ImageReduced, Edges, 'sobel_fast', 0.5, 10, 20)
*提取到边缘的个数为5
count_obj (Edges, Number)
*创建一个仿射变换的矩阵,尺寸倍数等于4
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_scale (HomMat2DIdentity, 4, 4, 0, 0, HomMat2DScale)
*将提取的边缘做仿射变换,变为原来边缘的尺度的4倍,ZoomedEdges轮廓点为334,长度为1375.31
affine_trans_contour_xld (Edges, ZoomedEdges, HomMat2DScale)
*创建一张位图,大小为按时图像的4倍(宽高均为4倍关系)
gen_image_const (VarImageBig, 'byte', 4 * Width, 4 * Height)
*Width1=1804 Height1=1204
get_image_size(VarImageBig, Width1, Height1)
*计算轮廓线的个数。计算结果为5,刚好边缘检测检测的5个字母为闭环的轮廓
count_obj (ZoomedEdges, NEdges)
*将尺寸扩大后的每一个轮廓绘制在上面创建的位图上,绘制颜色为255
for i := 1 to NEdges by 1
select_obj (ZoomedEdges, ObjectSelected, i)
*得到边缘线的坐标
get_contour_xld (ObjectSelected, RowEdge, ColEdge)
*根据边缘的坐标,得到区域
gen_region_polygon (Region1, RowEdge, ColEdge)
*使用形态学膨胀算法,将区域膨胀2.5个像素
dilation_circle (Region1, RegionDilation, 2.5)
*在创建的位图上绘制区域,灰度为255
paint_region (RegionDilation, VarImageBig, VarImageBig, 255, 'fill')
endfor
*将绘者好的位图缩放回原始图像大小
*缩放支持的插值类型有五种类型'bicubic', 'bilinear', 'constant', 'nearest_neighbor', 'weighted'
zoom_image_size (VarImageBig, VarImageSmall, Width, Height, 'weighted')
*二项式滤波器对图像进行平滑处理
binomial_filter (VarImageSmall, VarImage, 3, 3)
*创建一个差异模型,创建的模型的大小为原始图像的宽和高
create_variation_model (Width, Height, 'byte', 'direct', VarModelID)
*准备用于比较图片的模板,后面的两个值是相对阈值和绝对阈值
prepare_direct_variation_model (Image, VarImage, VarModelID, 15, 4)
dev_display (VarImage)
disp_message (WindowHandle, 'Variation Image', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
*
* print inspection
for i := 1 to 30 by 1
read_image (Image, 'pen/pen-' + i$'02d')
* locate the logo and align it to the reference image
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| != 0)
*根据识别到形状模型在检测图像上的尺度和位置坐标,得到一个仿射矩阵
vector_angle_to_rigid (Row, Column, Angle, ModelRow, ModelColumn, 0, HomMat2D)
*将检测图像做仿射变换,将检测图像变换到和对比图像一个坐标系一个位置在
affine_trans_image (Image, ImageAffineTrans, HomMat2D, 'constant', 'false')
*使用裁剪模板图像的区域直接裁剪仿射变换后的检测图像
reduce_domain (ImageAffineTrans, LogoArea, ImageReduced1)
*使用差异模型对比裁剪后的图像,如果有差异,结果存放在RegionDiff中
compare_ext_variation_model (ImageReduced1, RegionDiff, VarModelID, 'absolute')
*对差异的处理,在测试过程中发现,即使通过形状模型仿射变换后,有位置偏差,也不会影响比对的结果
connection (RegionDiff, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 10, 99999)
*
dev_display (ImageAffineTrans)
count_obj (SelectedRegions, NDefects)
if (NDefects > 0)
dev_set_color ('red')
dev_set_draw ('margin')
dev_set_line_width (2)
dev_display (SelectedRegions)
dev_set_color ('green')
dev_set_line_width (1)
dev_display (Edges)
disp_message (WindowHandle, 'Image check not OK', 'window', 12, 12, 'red', 'false')
else
disp_message (WindowHandle, 'Image check OK', 'window', 12, 12, 'green', 'false')
endif
endif
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endfor
*
* clean up
clear_shape_model (ShapeModelID)
clear_variation_model (VarModelID)
未平滑滤波前的缩放回原始的图像
二项式平滑滤波后的结果图像
二、例程variation_model_illumination
功能:检查瓶盖上印刷缺陷检测的问题
代码解析:
* This example demonstrates the print inspection using
* a variation model. The variation model is trained
* using a single model image. Before applying the print inspection,
* the images are scaled to the same gray value range like the model.
*
dev_update_off ()
dev_close_window ()
*
* Read model image and init visualization
read_image (ModelImage, 'cap_illumination/cap_illumination_01')
get_image_size (ModelImage, Width, Height)
dev_open_window (0, 0, Width / 2, Height / 2, 'black', WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
*
* Segment model region
dev_display (ModelImage)
*紫色代表是内部封装函数,作用是提取检测的兴趣区域,并得到兴趣区域的图像
get_model_region (ModelImage, RegionROI, ImageReduced)
area_center (RegionROI, Area, RowModel, ColumnModel)
dev_set_draw ('margin')
dev_set_color ('green')
dev_display (RegionROI)
*
* Create model for shape-based-matching
disp_message (WindowHandle, 'Model generation...', 'window', 12, 12, 'black', 'true')
*创建一个尺度能够变换的形状模型,可变尺寸的范围为 0.8, 1.2倍,角度范围为 0, rad(360)
create_scaled_shape_model (ImageReduced, 'auto', 0,