0
点赞
收藏
分享

微信扫一扫

基于Halcon实现的差异模型【常应用于印刷检测】

楠蛮鬼影 2022-04-23 阅读 41

1.Standard标准做法【适用情况:多张良品图像用于差异模型训练】

* 显示设置
dev_update_off ()
dev_get_window (WindowHandle)
dev_set_line_width (3)
set_display_font (WindowHandle, 66, 'mono', 'true', 'false')
dev_set_draw ('margin')

* 读入图像
list_files ('C:/Userslg-brain15/Desktop/新建文件夹 (2)', ['files','follow_links'], ImageFiles)
tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)

* 选择第1张图像创建形状模板
read_image (Image, ImageFiles[0])

* 把我感兴趣的区域抠出来,原则上范围越小越好,因为这样创建模板时干扰会少很多
gen_rectangle1 (ROI_0,40, 423, 953, 1070)
reduce_domain (Image, ROI_0, ImageReduced)
threshold (ImageReduced, Regions, 0, 119)
reduce_domain (ImageReduced, Regions, ImageReduced1)
opening_circle (Regions, RegionOpening, 2.5)
shape_trans (RegionOpening, RegionTrans, 'convex')
dilation_circle (RegionTrans, RegionDilation, 8.5)
reduce_domain (ImageReduced, RegionDilation, ImageReduced2)

* 获得抠图区域的中心,这是参考点
area_center (RegionDilation, Area, RowRef, ColumnRef)

* 创建模板
create_shape_model (ImageReduced2, 3, rad(-5), rad(10), 'auto', 'pregeneration', 'use_polarity', 20, 10, ModelID)
* 获得模板轮廓,看一下质量怎么样,非常完美
get_shape_model_contours (ModelContours, ModelID, 1)

* 创建可变模板
get_image_size (Image, Width, Height)
create_variation_model (Width, Height, 'byte', 'standard', VarModelID)
*这里使用'standard'

* 文件夹中前15张图片是质量良好的,可以用来训练模板
for Index := 0 to 3 by 1
       read_image (Image, ImageFiles[Index])
       * 先寻找模板的实例
       find_shape_model (Image, ModelID, rad(-5), rad(10), 0.5, 1, 0.5, 'least_squares', 0, 0.9, Row, Column, Angle, Score)
       if(|Row|>0)
              * 使用仿射变换,将当前图像平移旋转到与模板图像重合,注意是当前图像转向模板图像
              vector_angle_to_rigid (Row, Column, Angle, RowRef, ColumnRef, 0, HomMat2D)
              affine_trans_image (Image, ImageAffineTrans, HomMat2D, 'bilinear', 'false')
       endif
       * 既然当前图像已经转到和第一张图像重合了,我就可以使用第一张图像抠图使用的区域进行抠图了
       reduce_domain(ImageAffineTrans, RegionDilation, ImageReduced3)       
       * 训练可变模板
       train_variation_model (ImageReduced3, VarModelID)            
endfor


* 获得变形模型
get_variation_model (Image1, VarImage, VarModelID)
dev_display (VarImage)
stop()

* 做检测之前可以先用下面这个算子对可变模型进行设参,这是一个经验值,需要调试者调整
prepare_variation_model (VarModelID, 10, 3)

* 可变模板训练完成后,我们终于可以进入主题,马上对所有图像进行缺陷检测,思想就是差分
for Index := 0 to |ImageFiles|-1 by 1
       read_image (Image, ImageFiles[Index])
       * 要注意做差分的两幅图像分辨率相同,当然也需要通过仿射变换把待检测的图像转到与模板图像重合
       * 先寻找模板的实例
       find_shape_model (Image, ModelID, rad(-5), rad(10), 0.5, 1, 0.5, 'least_squares', 0, 0.9, Row, Column, Angle, Score)
       if(|Row|>0)
              * 使用仿射变换,将当前图像平移旋转到与模板图像重合,注意是当前图像转向模板图像
              vector_angle_to_rigid (Row, Column, Angle, RowRef, ColumnRef, 0, HomMat2D)
              affine_trans_image (Image, ImageAffineTrans, HomMat2D, 'bilinear', 'false')
       endif
       * 抠图
       reduce_domain (ImageAffineTrans, RegionDilation, ImageReduced4)
       * 差分 (就是检查两幅图像相减,剩下的区域就是不同的地方了,与模板图像不同的地方就是缺陷)
       * 这里可不能用difference做差分啊,halcon为变形模板提供了专门的差分算子:compare_variation_model
       compare_variation_model (ImageReduced4, Region, VarModelID)
       
       * 图像分割
       connection (Region, ConnectedRegions)
       
       * 特征选择:用一些特征来判断这幅图像印刷是否有缺陷,这里使用面积
       * 其实可以考虑利用区域面积的大小来判断缺陷的严重程度,这里就不过多讨论了
       select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 25, 99999)

            
       * 显示
       dev_display (ImageAffineTrans)
       count_obj (SelectedRegions, Number)
       disp_message (WindowHandle, '第'+(Index+1)+'张','image', 12, 300, 'black', 'true')
       if(Number>0)
              disp_message (WindowHandle, 'NG', 'image', 12, 12, 'red', 'false')
              dev_set_color ('red')
       else
              disp_message (WindowHandle, 'OK', 'image', 12, 12, 'green', 'false')             
              dev_set_color ('green')
       endif      
       dev_display (SelectedRegions)
       stop()
endfor
dev_update_on ()  
* 结语:如果发现前面作为训练变形模板的良好图像也被判定为NG,
*      可以调整prepare_variation_model参数
*      或者调整select_shape特征筛选的标准

dev_update_off ()
read_image (Image, './正抠图样本2/carpet_1.bmp')
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_set_color ('red')
dev_display (Image)
* Note: the shape model will be constructed from a ROI that is computed
* automatically based on a simple image segmentation.
threshold (Image, Region, 70, 255)
fill_up (Region, RegionFillUp)
difference (RegionFillUp, Region, RegionDifference)
shape_trans (RegionDifference, RegionTrans, 'convex')
dilation_circle (RegionTrans, RegionDilation, 8.5)
reduce_domain (Image, RegionDilation, ImageReduced)
create_ncc_model (ImageReduced, 'auto', 0, rad(360), 'auto', 'use_polarity', ModelID)
area_center (RegionDilation, Area, RowRef, ColumnRef)
*创建一个差异模型
create_variation_model (Width, Height, 'byte', 'standard', VariationModelID)
for I := 1 to 15 by 1
    read_image (Image, './正抠图样本2/carpet_' + I$'.')
    find_ncc_model (Image, ModelID, 0, rad(360), 0.4, 1, 0.5, 'true', 0, Row, Column, Angle, Score)
    
    if (|Score| == 1)
        vector_angle_to_rigid (Row, Column, Angle, RowRef, ColumnRef, 0, HomMat2D)
        affine_trans_image (Image, ImageTrans, HomMat2D, 'constant', 'false')
        reduce_domain (ImageTrans, RegionDilation, ImageTrans)
        train_variation_model (ImageTrans, VariationModelID)
        dev_display (ImageTrans)
        dev_display_ncc_matching_results (ModelID, 'green', Row, Column, Angle, 0)
    endif
endfor
stop ()
get_variation_model (MeanImage, VarImage, VariationModelID)
*准备检查模板。设置两个阈值AbsThreshold, VarThreshold。
*前者定义了检测图像与检查模板的灰度差的绝对值的允许阈值。
*后者定义了与理想图像的差异程度
prepare_variation_model (VariationModelID, 18, 3.5)
* We can now free the training data to save some memory.
clear_train_data_variation_model (VariationModelID)
* Note: the checking of the print will be restricted to the region of the clip.
* Sometimes the print is also in an incorrect position of the clip.  This will lead
* to erroneous regions at the top or bottom border of the clip and hence can
* be detected easily.
erosion_rectangle1 (RegionFillUp, RegionROI, 1, 15)
dev_display (MeanImage)
set_tposition (WindowHandle, 20, 20)
dev_set_color ('green')
write_string (WindowHandle, 'Reference image')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
dev_display (VarImage)
set_tposition (WindowHandle, 20, 20)
dev_set_color ('green')
write_string (WindowHandle, 'Varia tion image')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
dev_set_draw ('margin')
NumImages := 22
for I := 1 to 22 by 1
    read_image (Image, './正抠图样本2/carpet_' + I$'.')
    find_ncc_model (Image, ModelID, 0, rad(360), 0.3, 1, 0.5, 'true', 0, Row, Column, Angle, Score)
    if (|Score| == 1)
        vector_angle_to_rigid (Row, Column, Angle, RowRef, ColumnRef, 0, HomMat2D)
        affine_trans_image (Image, ImageTrans, HomMat2D, 'constant', 'false')
        reduce_domain (ImageTrans, RegionDilation, ImageReduced)
        compare_variation_model (ImageReduced, RegionDiff, VariationModelID)
        connection (RegionDiff, ConnectedRegions)
        select_shape (ConnectedRegions, RegionsError, 'area', 'and', 5, 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
    if (I < NumImages)
        disp_continue_message (WindowHandle, 'black', 'true')
        stop ()
    endif
endfor
clear_ncc_model (ModelID)
clear_variation_model (VariationModelID)

 

2.Direct直接做法【适用情况:只有一张图像用于差异模型训练】

* to perform a typical print quality inspection.  The program detects incorrect
* prints on the clips of pens.  In the first step, the variation model is constructed
* from images of correct prints.  Since the position of the objects can vary, the
* images must be transformed to a reference position (the position of the print
* in the first image in this example).  HALCON's shape-based matching is used
* to detect the position and angle of the print in the images.  The found position
* and angle are used to transform the images to the reference position.
* In the second part of the program, the prints of the correct clips and of several
* incorrect clips is checked and classified.

dev_update_off ()
read_image (Image, './图片/carpet_1.bmp')
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_set_color ('red')
dev_display (Image)
* Note: the shape model will be constructed from a ROI that is computed
* automatically based on a simple image segmentation.
threshold (Image, Region, 70, 255)
fill_up (Region, RegionFillUp)
difference (RegionFillUp, Region, RegionDifference)
shape_trans (RegionDifference, RegionTrans, 'convex')
dilation_circle (RegionTrans, RegionDilation, 8.5)
reduce_domain (Image, RegionDilation, ImageReduced)
create_ncc_model (ImageReduced, 'auto', 0, rad(360), 'auto', 'use_polarity', ModelID)
area_center (RegionDilation, Area, RowRef, ColumnRef)
*边缘检测提取出边缘信息图像
sobel_amp (Image, VarImage, 'sum_abs', 3)
get_image_pointer1 (Image, Pointer, Type, Width, Height)
create_variation_model (Width, Height, Type, 'direct', ModelID2)
prepare_direct_variation_model (Image, VarImage, ModelID2, 20, 1)

dev_display (VarImage)
set_tposition (WindowHandle, 20, 20)
dev_set_color ('green')
write_string (WindowHandle, 'Varia tion image')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
dev_set_draw ('margin')
NumImages := 22
for I := 1 to 22 by 1
    read_image (Image, './图片/carpet_' + I$'.')
    find_ncc_model (Image, ModelID, 0, rad(360), 0.3, 1, 0.5, 'true', 0, Row, Column, Angle, Score)
    if (|Score| == 1)
        vector_angle_to_rigid (Row, Column, Angle, RowRef, ColumnRef, 0, HomMat2D)
        affine_trans_image (Image, ImageTrans, HomMat2D, 'constant', 'false')
        reduce_domain (ImageTrans, RegionDilation, ImageReduced)
        compare_variation_model (ImageReduced, RegionDiff, ModelID2)
        connection (RegionDiff, ConnectedRegions)
        select_shape (ConnectedRegions, RegionsError, 'area', 'and', 2, 50000)
        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
    if (I < NumImages)
        disp_continue_message (WindowHandle, 'black', 'true')
        stop ()
    endif
endfor
clear_ncc_model (ModelID)
clear_variation_model (ModelID2)

差异模型常见用法如上述所示。

举报

相关推荐

0 条评论