Variation Model是用一幅或多幅测试图像同样板图像做比较,找出差异,常用来做外观缺陷检测。
创建及使用步骤:
Ø1、图像准备
Ø2、创建Variation Model
Ø3、图像摆正
Ø4、训练Variation Model
Ø5、准备Variation Model
Ø6、比较Variation Model
create_variation_model (Width, Height, 'byte', 'standard', variationModelID)
训练模式有三种mode:
standard:
Ø对所有OK品的图像的同一个的位置的像素点的和取平均。该模式的优点是可以多次训练,缺点是OK品图像中不能混入NG品图像,否则最终产生的ideal image和variation image的准确性会降级。
direct:
使用一张图片直接创建模板
robust:
Ø在实际操作中,有时会不可避免地将NG品图像混入到OK品图像中,robust模式就是为这种情况而设计。
比较亮暗缺陷 的基本原理:
ØAbsThreshold,绝对阈值。VarThreshold 相对阈值。AbsThreshold,VarThreshold 可以分别有一个值,也可以分别有两个值。当都只有一个值的时候,亮暗缺陷都是其决定的。i(x,y)表示ideal image灰度值,v(x,y)表示variation image灰度值,c(x,y)表示待检测图片的灰度值,a=AbsThreshold,b=VarThreshold 当c(x,y)>i(x,y)+ max{a,b*v(x,y)}为亮缺陷。当c(x,y)<i(x,y)- max{a,b*v(x,y)}为暗缺陷。当AbsThreshold,VarThreshold有两个值时,第一个值决定亮缺陷,第二个值决定暗缺陷。i(x,y)表示ideal image灰度值,v(x,y)表示variation image灰度值,c(x,y)表示待检测图片的灰度值,AbsThreshold=[a1,a2],VarThreshold =[b1,b2]当c(x,y)>i(x,y)+ max{a1,b1*v(x,y)}为亮缺陷。当c(x,y)<i(x,y)- max{a2,b2*v(x,y)}为暗缺陷。
代码示例:
read_image (Image, 'pen/pen-01.png')
get_image_size (Image, Width, Height)
*阈值分割
threshold (Image, Regions, 128, 255)
fill_up (Regions, RegionFillUp)
*偏差
difference(RegionFillUp,Regions,RegionDifference)
shape_trans (RegionDifference, RegionTrans, 'convex')
dilation_circle (RegionTrans, RegionDilation, 7.5)
*裁剪
reduce_domain (Image, RegionDilation, ImageReduced)
dev_display (ImageReduced)
*创建模板
create_shape_model (ImageReduced, 'auto', rad(0), rad(360), 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID)
get_shape_model_contours (ModelContours, ModelID, 1)
find_shape_model (Image, ModelID, rad(0), rad(360), 0.5, 1, 0.5, 'least_squares', 0, 0.9, RowRef, ColumnRef, AngleRef, ScoreRef)
vector_angle_to_rigid (0, 0, 0, RowRef, ColumnRef, AngleRef, HomMat2D)
affine_trans_contour_xld (ModelContours, ContoursAffinTrans, HomMat2D)
*dev_display (ContoursAffinTrans)
stop ()
*创建variation_model
create_variation_model (Width, Height, 'byte', 'standard', variationModelID)
for I := 1 to 15 by 1
read_image (Image, 'pen/pen-' + I$'02d')
find_shape_model (Image, ModelID, rad(0), rad(360), 0.5, 1, 0.5, 'least_squares', 0, 0.9, RowCheck, ColumnCheck, AngleCheck, ScoreCheck)
if(|ScoreCheck|==0)
disp_message (3600, '没有找到产品', 'window', 100, 100, 'black', 'true')
stop ()
continue
endif
*创建齐次矩阵
hom_mat2d_identity (HomMat2DIdentity)
*旋转矩阵
hom_mat2d_rotate (HomMat2DIdentity, AngleCheck, 0, 0, HomMat2DRotate)
*平移矩阵
hom_mat2d_translate (HomMat2DRotate, RowRef - RowCheck, ColumnRef - ColumnCheck, HomMat2DTranslate)
*仿射变换
affine_trans_image (Image, ImageAffineTrans, HomMat2DTranslate, 'constant', 'false')
*训练variationModel
train_variation_model (ImageAffineTrans, variationModelID)
endfor
*准备模板
get_variation_model (MeanImage, VarImage, variationModelID)
prepare_variation_model (variationModelID, 15, 4)
*检测
for J := 15 to 30 by 1
*dev_clear_window ()
read_image (Image, 'pen/pen-' + J$'02d')
*dev_display (Image)
find_shape_model (Image, ModelID, rad(0), rad(360), 0.5, 1, 0.5, 'least_squares', 0, 0.9, RowDo, ColumnDo, AngleDo, ScoreDo)
if(|ScoreDo|==0)
disp_message (3600, '没有找到产品', 'window', 100, 100, 'black', 'true')
stop ()
continue
endif
*创建齐次矩阵
hom_mat2d_identity (HomMat2DIdentityDo)
*旋转矩阵
hom_mat2d_rotate (HomMat2DIdentityDo, AngleDo, 0, 0, HomMat2DRotateDo)
*平移矩阵
hom_mat2d_translate (HomMat2DRotateDo,RowRef- RowDo,ColumnRef-ColumnDo, HomMat2DTranslateDo)
*仿射变换
affine_trans_image (Image, ImageAffinTransDo, HomMat2DTranslateDo, 'constant', 'false')
*检测
reduce_domain (ImageAffinTransDo, RegionFillUp, ImageReducedResult)
compare_variation_model (ImageReducedResult, Defects, variationModelID)
connection (Defects, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 10, 99999)
dev_display (SelectedRegions)
stop()
endfor