前言
在实际项目中,印刷品缺陷检测是缺陷检测中的难点项目。通常印刷品检测往往具备缺陷小,缺陷所在位置不固定,出现少印或者多印的情况。并且由于产线原因,大量的印刷品在视野中的大小并不是完全一致的,可能出现细微的歪斜,亮度差异等多种不确定的因素。
所以在印刷品检测时,常用的blob分析+差值算法,往往不能满足实际的要求。所以使用差异化模型,对图像区域进行训练,在可靠的精度下,仍然可以达到极快的检测速度。
1:halcon程序
************************************创建训练图像和训练模型***************************
dev_get_window (WindowHandle)
*初始化区域尺寸
ImageSizeWidth1:=[]
ImageSizeHeight1:=[]
*获取20个ROI的区域
Row1:=[394.115, 397.311, 390.92, 512.34, 515.536, 512.34, 630.565, 633.76, 630.565, 748.79, 755.18, 755.18, 173.642, 259.914, 263.109, 173.642, 263.109, 263.109, 493.169, 585.831, 681.689]
Row2:=[465.388, 472.192, 472.192, 590.417, 593.612, 590.417, 708.642, 711.837, 710.033, 830.062, 830.062, 826.867, 219.766, 318.82, 312.429, 222.962, 315.624, 318.82, 545.683, 641.541, 735.399]
Column1:=[341.424, 485.187, 628.95, 344.618, 481.992, 616.171, 335.034, 472.408, 616.171, 341.424, 475.603, 616.171, 200.855, 127.376, 280.724, 756.739, 683.261, 830.218, 827.024, 830.218, 830.218]
Column2:=[424.487, 565.055, 705.624, 434.071, 565.055, 705.624, 424.487, 568.25, 708.818, 421.292, 568.25, 708.818, 296.697, 220.024, 373.371, 842.997, 766.324, 922.866, 920.255, 916.476, 919.671]
list_files ('D:/2024Work/work/4.2/键盘160/键盘160', ['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)
for Index := 0 to |ImageFiles| - 1 by 1
read_image (Image, ImageFiles[Index])
*先根据模板匹配,将所要识别的区域矫正在图像中心,便于绘制ROI
read_shape_model ('D:/模板正面.shm',ShapeModelID)
get_shape_model_contours (ModelContours, ShapeModelID, 1)
find_scaled_shape_model (Image, ShapeModelID, rad(0), rad(90), 0.9, 1.1, 0.1, 1, 0.5, 'least_squares', 0, 0.9, Row, Column, Angle, Scale, Score)
get_image_size (Image, Width, Height)
vector_angle_to_rigid (Row, Column , Angle ,Height/2,Width/2-200,0, HomMat2D)
affine_trans_image (Image, ImageAffineTrans, HomMat2D, 'constant', 'false')
*****************绘制区域ROI*************
* for j := 0 to 20 by 1
* draw_rectangle1 (WindowHandle, Row12, Column12, Row22, Column22)
* Row1[j]:=Row12
* Row2[j]:=Row22
* Column1[j]:=Column12
* Column2[j]:=Column22
* endfor
*********************************************
*初始化被测试区域的区域大小
Row111:=[]
Column111:=[]
Row112:=[]
Colum112:=[]
* affRow:=[]
* affColumn:=[]
* affPhi:=[]
for i := 0 to 20 by 1
gen_rectangle1 (ROI_0, Row1[i], Column1[i], Row2[i], Column2[i])
reduce_domain (ImageAffineTrans, ROI_0, ImageReduced3)
crop_domain (ImageReduced3, ImagePart1)
binary_threshold (ImagePart1, Region, 'max_separability', 'light', UsedThreshold)
area_center (Region, Area, Row3, Column3)
smallest_rectangle1 (Region, Row11, Column11, Row21, Column21)
* Row3:=(Row11+Row21)/2
* Column3:=(Column11+Column21)/2
* affRow[i]:=[Row3]
* affColumn[i]:=[Column3]
Row111[i]:=[Row11]
Column111[i]:=[Column11]
Row112[i]:=[Row21]
Colum112[i]:=[Column21]
*绘制比二值化选中区域更大一些的矩形区域,用于训练模板
gen_rectangle1 (Rectangle, Row11-1, Column11-1, Row21+1, Column21+1)
reduce_domain (ImagePart1, Rectangle, ImageReduced1)
crop_domain (ImageReduced1, ImagePart2)
*由于差异化模板需要保持每个输入图像的大小保持一致
*所以将第一个训练图像作为基准,提取每个图像的尺寸,便于后续图像进行矫正
get_image_size (ImagePart2, Width1, Height1)
if (Index!=0)
zoom_image_size (ImagePart2, ImagePart2, ImageSizeWidth1[i], ImageSizeHeight1[i], 'constant')
else
ImageSizeWidth1[i]:=Width1
ImageSizeHeight1[i]:=Height1
endif
*创建文件夹,并将训练图像保存在文件夹中,便于后续提取训练
file_exists ('D:/2024Work/work/4.2/键盘160/键盘160/'+i, FileExists)
if (not(FileExists))
make_dir('D:/2024Work/work/4.2/键盘160/键盘160/'+i)
endif
write_image (ImagePart2, 'bmp', 0, 'D:/2024Work/work/4.2/键盘160/键盘160/'+i+'/'+Index)
endfor
*创建模型数组,对每个区域进行单独的创建模型
varModelID:=[]
for Index1 := 0 to 20 by 1
*根据我们所需要的区域尺寸,创建对应的差异化模型
create_variation_model (ImageSizeWidth1[Index1], ImageSizeHeight1[Index1], 'byte', 'standard', varModelID1)
*读取对应文件夹的所有图像
list_files ('D:/2024Work/work/4.2/键盘160/键盘160/'+Index1, ['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)
for Index := 0 to |ImageFiles| - 1 by 1
read_image (Image, ImageFiles[Index])
*训练模型
train_variation_model (Image, varModelID1)
*设置模型的最小灰度差值为60,差异化为2
prepare_variation_model (varModelID1, 60, 2)
*显示模型图案,用于检查
get_variation_model (Image, VarImage, varModelID1)
dev_display (VarImage)
endfor
varModelID[Index1]:=varModelID1
endfor
*对创建好的模型数组,逐一写入文件夹中。待使用时提取
for Index2 := 0 to 20 by 1
write_variation_model (varModelID[Index2], 'D:/2024Work/work/4.2/键盘160/键盘160/var/'+Index2+'.var')
endfor
stop ()
endfor
******************************************对训练模型进行测试******************************
*初始化所有检测区域的尺寸
ImageSizeWidth1:=[30, 40, 35, 33, 33, 45, 52, 38, 56, 37, 28, 28, 53, 43, 44, 29, 41, 50, 22, 41, 34]
ImageSizeHeight1:=[62, 62, 62, 61, 61, 62, 61, 61, 62, 56, 60, 36, 24, 42, 35, 29, 32, 34, 32, 46, 42]
*初始化检测区域ROI的位置参数
Row1:=[394.115, 397.311, 390.92, 512.34, 515.536, 512.34, 630.565, 633.76, 630.565, 748.79, 755.18, 755.18, 173.642, 259.914, 263.109, 173.642, 263.109, 263.109, 493.169, 585.831, 681.689]
Row2:=[465.388, 472.192, 472.192, 590.417, 593.612, 590.417, 708.642, 711.837, 710.033, 830.062, 830.062, 826.867, 219.766, 318.82, 312.429, 222.962, 315.624, 318.82, 545.683, 641.541, 735.399]
Column1:=[341.424, 485.187, 628.95, 344.618, 481.992, 616.171, 335.034, 472.408, 616.171, 341.424, 475.603, 616.171, 200.855, 127.376, 280.724, 756.739, 683.261, 830.218, 827.024, 830.218, 830.218]
Column2:=[424.487, 565.055, 705.624, 434.071, 565.055, 705.624, 424.487, 568.25, 708.818, 421.292, 568.25, 708.818, 296.697, 220.024, 373.371, 842.997, 766.324, 922.866, 920.255, 916.476, 919.671]
*初始化差异化模型数组,用于存放读取的全部模型
varmodel:=[]
*将全部模型进行读取并存放在数组中
for Index1 := 0 to 20 by 1
read_variation_model ('D:/2024Work/work/4.2/键盘160/键盘160/var/'+Index1+'.var', ModelID)
varmodel[Index1]:=ModelID
endfor
*读取产品模板匹配的模板
read_shape_model ('D:/模板正面.shm',ShapeModelID)
list_files ('D:/2024Work/work/4.2/键盘160/键盘160', ['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)
for Index := 0 to |ImageFiles| - 1 by 1
read_image (Image, ImageFiles[Index])
*********************
rgb1_to_gray (Image, Image2)
*进行模板匹配。寻找图中的形状,并将所要检测的区域矫正到图像中心
find_scaled_shape_model (Image2, ShapeModelID, rad(0), rad(90), 0.9, 1.1, 0.1, 1, 0.5, 'least_squares', 0, 0.9, Row9, Column9, Angle9, Scale, Score)
get_image_size (Image, Width, Height)
vector_angle_to_rigid (Row9, Column9 , Angle9 ,Height/2,Width/2-200,0, HomMat2D1)
affine_trans_image (Image2, ImageAffineTrans, HomMat2D1, 'constant', 'false')
for i := 0 to 20 by 1
*根据ROI的参数,获取到图像区域,并对图像区域进行裁切
gen_rectangle1 (ROI_0, Row1[i], Column1[i], Row2[i], Column2[i])
reduce_domain (ImageAffineTrans, ROI_0, ImageReduced4)
crop_domain (ImageReduced4, ImagePart)
binary_threshold (ImagePart, Region1, 'max_separability', 'light', UsedThreshold1)
smallest_rectangle1 (Region1, Row12, Column12, Row22, Column22)
*根据图像区域对图像进行二次裁切,使得图像满足差异化模型
gen_rectangle1 (Rectangle1, Row12-1, Column12-1, Row22+1, Column22+1)
reduce_domain (ImagePart, Rectangle1, ImageReduced2)
crop_domain (ImageReduced2, ImagePart3)
*根据差异化模型的区域大小,对裁切的区域进行缩放,将区域的大小与差异化模型进行相匹配
zoom_image_size (ImagePart3, ImageZoom, ImageSizeWidth1[i], ImageSizeHeight1[i], 'constant')
*将区域应用在差异化模型中
compare_variation_model (ImageZoom, RegionDiff, varmodel[i])
*将差异部分的区域分割开来
connection (RegionDiff, ConnectedRegions)
*将区域面积大于30的作为缺陷进行输出
select_shape (ConnectedRegions, SelectedRegions1, 'area', 'and', 30, 10000000)
dev_display (ImagePart3)
dev_display (SelectedRegions1)
stop ()
endfor
endfor
2:halcon程序解析
2.1创建训练模型和训练图像
2.1.1图像矫正
可以预先创建好模板后,直接读取模板文件,然后进行图像仿射变换将图像区域矫正到图像的中央,便于后续绘制ROI。
其中需要注意,矫正后的图像应该保持每个字符识别的区域都要可见。
read_shape_model ('D:/模板正面.shm',ShapeModelID)
get_shape_model_contours (ModelContours, ShapeModelID, 1)
find_scaled_shape_model (Image, ShapeModelID, rad(0), rad(90), 0.9, 1.1, 0.1, 1, 0.5, 'least_squares', 0, 0.9, Row, Column, Angle, Scale, Score)
get_image_size (Image, Width, Height)
vector_angle_to_rigid (Row, Column , Angle ,Height/2,Width/2-200,0, HomMat2D)
affine_trans_image (Image, ImageAffineTrans, HomMat2D, 'constant', 'false')
矫正前
矫正后
2.1.2绘制ROI
对图像上的每个需要识别的单独区域进行ROI的绘制
*****************绘制区域ROI*************
for j := 0 to 20 by 1
draw_rectangle1 (WindowHandle, Row12, Column12, Row22, Column22)
Row1[j]:=Row12
Row2[j]:=Row22
Column1[j]:=Column12
Column2[j]:=Column22
endfor
*********************************************
在绘制ROI时,应该将ROI尽可能的覆盖到全部识别的区域,比识别的区域稍大,但又不会覆盖到其他区域的大小,并且尽可能的大一些可以有效的防止矫正精度不够时,仍然可以准确的识别的所需要识别的区域。
将绘制完成后的区域坐标复制并粘贴到程序中,后续就不需要重复的绘制ROI
2.1.3生成被检测的区域
通过对绘制好的ROI进行裁剪,使用自动二值化阈值,提取到模型的区域,然后再根据实际物体的大小绘制比实际物体稍大一点的图像作为训练图像进行输出
gen_rectangle1 (ROI_0, Row1[i], Column1[i], Row2[i], Column2[i])
reduce_domain (ImageAffineTrans, ROI_0, ImageReduced3)
crop_domain (ImageReduced3, ImagePart1)
binary_threshold (ImagePart1, Region, 'max_separability', 'light', UsedThreshold)
area_center (Region, Area, Row3, Column3)
smallest_rectangle1 (Region, Row11, Column11, Row21, Column21)
*绘制比二值化选中区域更大一些的矩形区域,用于训练模板
gen_rectangle1 (Rectangle, Row11-1, Column11-1, Row21+1, Column21+1)
reduce_domain (ImagePart1, Rectangle, ImageReduced1)
crop_domain (ImageReduced1, ImagePart2)
2.1.4保存图像
对首张图像,我们需要获取到第一张图像的全部被识别的区域的大小,并对后续的所有图像区域进行对应的缩放。差异化模型需要保证所有测试图像和训练图像都保持同一个大小。
*由于差异化模板需要保持每个输入图像的大小保持一致
*所以将第一个训练图像作为基准,提取每个图像的尺寸,便于后续图像进行矫正
get_image_size (ImagePart2, Width1, Height1)
if (Index!=0)
zoom_image_size (ImagePart2, ImagePart2, ImageSizeWidth1[i], ImageSizeHeight1[i], 'constant')
else
ImageSizeWidth1[i]:=Width1
ImageSizeHeight1[i]:=Height1
endif
*创建文件夹,并将训练图像保存在文件夹中,便于后续提取训练
file_exists ('D:/2024Work/work/4.2/键盘160/键盘160/'+i, FileExists)
if (not(FileExists))
make_dir('D:/2024Work/work/4.2/键盘160/键盘160/'+i)
endif
write_image (ImagePart2, 'bmp', 0, 'D:/2024Work/work/4.2/键盘160/键盘160/'+i+'/'+Index)
同样,将所有获取到的尺寸保存的数组中。
2.1.5训练和保存模型
*创建模型数组,对每个区域进行单独的创建模型
varModelID:=[]
for Index1 := 0 to 20 by 1
*根据我们所需要的区域尺寸,创建对应的差异化模型
create_variation_model (ImageSizeWidth1[Index1], ImageSizeHeight1[Index1], 'byte', 'standard', varModelID1)
*读取对应文件夹的所有图像
list_files ('D:/2024Work/work/4.2/键盘160/键盘160/'+Index1, ['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)
for Index := 0 to |ImageFiles| - 1 by 1
read_image (Image, ImageFiles[Index])
*训练模型
train_variation_model (Image, varModelID1)
*设置模型的最小灰度差值为60,差异化为2
prepare_variation_model (varModelID1, 60, 2)
*显示模型图案,用于检查
get_variation_model (Image, VarImage, varModelID1)
dev_display (VarImage)
endfor
varModelID[Index1]:=varModelID1
endfor
*对创建好的模型数组,逐一写入文件夹中。待使用时提取
for Index2 := 0 to 20 by 1
write_variation_model (varModelID[Index2], 'D:/2024Work/work/4.2/键盘160/键盘160/var/'+Index2+'.var')
endfor
2.2测试模型
2.2.1初始化参数和模型
检测区域的尺寸,ROI位置参数,差异化模型,模板均可以在前面进行复制粘贴即可
******************************************对训练模型进行测试******************************
*初始化所有检测区域的尺寸
ImageSizeWidth1:=[30, 40, 35, 33, 33, 45, 52, 38, 56, 37, 28, 28, 53, 43, 44, 29, 41, 50, 22, 41, 34]
ImageSizeHeight1:=[62, 62, 62, 61, 61, 62, 61, 61, 62, 56, 60, 36, 24, 42, 35, 29, 32, 34, 32, 46, 42]
*初始化检测区域ROI的位置参数
Row1:=[394.115, 397.311, 390.92, 512.34, 515.536, 512.34, 630.565, 633.76, 630.565, 748.79, 755.18, 755.18, 173.642, 259.914, 263.109, 173.642, 263.109, 263.109, 493.169, 585.831, 681.689]
Row2:=[465.388, 472.192, 472.192, 590.417, 593.612, 590.417, 708.642, 711.837, 710.033, 830.062, 830.062, 826.867, 219.766, 318.82, 312.429, 222.962, 315.624, 318.82, 545.683, 641.541, 735.399]
Column1:=[341.424, 485.187, 628.95, 344.618, 481.992, 616.171, 335.034, 472.408, 616.171, 341.424, 475.603, 616.171, 200.855, 127.376, 280.724, 756.739, 683.261, 830.218, 827.024, 830.218, 830.218]
Column2:=[424.487, 565.055, 705.624, 434.071, 565.055, 705.624, 424.487, 568.25, 708.818, 421.292, 568.25, 708.818, 296.697, 220.024, 373.371, 842.997, 766.324, 922.866, 920.255, 916.476, 919.671]
*初始化差异化模型数组,用于存放读取的全部模型
varmodel:=[]
*将全部模型进行读取并存放在数组中
for Index1 := 0 to 20 by 1
read_variation_model ('D:/2024Work/work/4.2/键盘160/键盘160/var/'+Index1+'.var', ModelID)
varmodel[Index1]:=ModelID
endfor
*读取产品模板匹配的模板
read_shape_model ('D:/模板正面.shm',ShapeModelID)
2.2.2测试模型并检测
rgb1_to_gray (Image, Image2)
*进行模板匹配。寻找图中的形状,并将所要检测的区域矫正到图像中心
find_scaled_shape_model (Image2, ShapeModelID, rad(0), rad(90), 0.9, 1.1, 0.1, 1, 0.5, 'least_squares', 0, 0.9, Row9, Column9, Angle9, Scale, Score)
get_image_size (Image, Width, Height)
vector_angle_to_rigid (Row9, Column9 , Angle9 ,Height/2,Width/2-200,0, HomMat2D1)
affine_trans_image (Image2, ImageAffineTrans, HomMat2D1, 'constant', 'false')
for i := 0 to 20 by 1
*根据ROI的参数,获取到图像区域,并对图像区域进行裁切
gen_rectangle1 (ROI_0, Row1[i], Column1[i], Row2[i], Column2[i])
reduce_domain (ImageAffineTrans, ROI_0, ImageReduced4)
crop_domain (ImageReduced4, ImagePart)
binary_threshold (ImagePart, Region1, 'max_separability', 'light', UsedThreshold1)
smallest_rectangle1 (Region1, Row12, Column12, Row22, Column22)
*根据图像区域对图像进行二次裁切,使得图像满足差异化模型
gen_rectangle1 (Rectangle1, Row12-1, Column12-1, Row22+1, Column22+1)
reduce_domain (ImagePart, Rectangle1, ImageReduced2)
crop_domain (ImageReduced2, ImagePart3)
*根据差异化模型的区域大小,对裁切的区域进行缩放,将区域的大小与差异化模型进行相匹配
zoom_image_size (ImagePart3, ImageZoom, ImageSizeWidth1[i], ImageSizeHeight1[i], 'constant')
*将区域应用在差异化模型中
compare_variation_model (ImageZoom, RegionDiff, varmodel[i])
*将差异部分的区域分割开来
connection (RegionDiff, ConnectedRegions)
*将区域面积大于30的作为缺陷进行输出
select_shape (ConnectedRegions, SelectedRegions1, 'area', 'and', 30, 10000000)
dev_display (ImagePart3)
dev_display (SelectedRegions1)
stop ()
endfor
实际检测效果
3.总结
上面其中重点提到了对ROI的选取。核心的关键在于如何去矫正图像,矫正区域。这个图像,由于已经预先固定在一个指定的区域,所以在检测时可以通过ROI去框选检测。而且每个检测区域有着较大的距离,所以在框选ROI时误差相对会比较的少。
对于上图而言20个检测区域,实际总共检测耗时在70ms左右。检测速度可以达到相当快,并且检测精度高。