Halcon印字缺陷检测

印字缺陷检测

在半导体行业,印字缺陷检测占了很大比例。打印标签上字符的缺失、字符的脏污等印字不良都需要检出。基于匹配和印字区域作差的检测算法思路如下:

读入图像
设置定位区域ROI1
创建模板
设置印字区域ROI2
阈值分割 连通域处理
计算印字外接矩形
截取模板印字区域
读取检测图像 进行匹配
匹配成功 仿射变换图像
根据印字外接矩形 截取模板印字区域
图像中值滤波
阈值分割 连通域处理
区域相减 blob筛选

Halcon示例代码如下

创建形状匹配模板

read_image (Image, IMGPATH)
gen_rectangle1 (ROI_1, 206.45, 433.387, 388.271, 707.601)
area_center (ROI_1, Area2, RowRef, ColumnRef)
reduce_domain (Image, ROI_1, ImageReduced3)
create_shape_model (ImageReduced3, 0, rad(0),  rad(360), 'auto', 'none', 'use_polarity', 'auto', 'auto', ModelID)

提取字符区域

gen_rectangle1 (ROI_0, 402.934, 273.432, 838.452, 911.432)
reduce_domain (Image, ROI_0, ImageReduced)
threshold (ImageReduced, Regions, 60, 255)
erosion_circle (Regions, RegionErosion, 2)
dilation_circle (RegionErosion, RegionDilation, 4)
* reduce_domain (Image, RegionDilation, ImageReduced2)
connection (RegionDilation, ConnectedRegions)
smallest_rectangle1 (ConnectedRegions, Row1, Column1, Row2, Column2)
gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2)
union1 (Rectangle, RegionUnion)

进行模板匹配 仿射变换图像

read_image (Image2, IMGPATH2)
find_shape_model (Image2, ModelID, rad(-30),  rad(80), 0.5, 1, 0.5, 'interpolation', 0, 0.5, Row4, Column4, Angle, Score1)
if(|Score1|>0)
    gen_cross_contour_xld (Cross, Row4, Column4, 15, rad(45))
    vector_angle_to_rigid (Row4, Column4, Angle,RowRef, ColumnRef, 0, HomMat2D)
    affine_trans_image (Image2, ImageAffineTrans2, HomMat2D, 'constant', 'false')
endif

字符区域提取,区域作差,筛选结果

reduce_domain (Image, RegionUnion, ImageReduced1)
reduce_domain (ImageAffineTrans2, RegionUnion, ImageReduced2)
    
     median_image (ImageReduced1, ImageMedian1, 'circle', 3, 'mirrored')
     median_image (ImageReduced2, ImageMedian2, 'circle', 3, 'mirrored')
    
*     sub_image (ImageMedian2, ImageMedian1, ImageSub, 1, 128)
*     write_image (ImageSub, 'jpeg', 0, 'ImageSub')
    
    dev_set_draw ('fill')
    gray_histo (ImageMedian1, ImageMedian1, AbsoluteHisto, RelativeHisto)
    PeakGray:=sort_index(AbsoluteHisto)[255]
    threshold (ImageMedian1, Region1, PeakGray+25, 255)
  
    gray_histo (ImageMedian2, ImageMedian2, AbsoluteHisto, RelativeHisto)
    PeakGray2:=sort_index(AbsoluteHisto)[255]
    threshold (ImageMedian2, Region2, PeakGray2+25, 255)
    
    difference (Region2, Region1, RegionDifference)
    connection (RegionDifference, ConnectedRegions4)
    select_shape (ConnectedRegions4, SelectedRegions1, 'inner_radius', 'and', 1.8, 10)

检测结果如下图所示:
Halcon印字缺陷检测_第1张图片

Halcon印字缺陷检测_第2张图片

你可能感兴趣的:(Halcon,印字检测)