Variation Model的主要原理是将待检测的图像与一张标准图像作比较,找出待检测图像与标准图像(ideal image)的明显差异(也就是不良)。标准图像可以采用几张OK品的图像训练(training)得到,也可以通过对一张OK品图像进行处理得到。训练后得到标准图像和一张variation图像(variation image),variation图像中包含了图像中每个像素点灰度值允许变化的范围。标准图像和variation图像用来创建一个variation model,如此,其他图像就可以与variation model作比较了。
创建一个ID为ModelID,宽为Width,高为Height,类型为Type的Variation Model,参数Mode决定了创建标准图像和相应的variation图像的方法。'standard'表示标准的训练方法,标准图像的位置是各训练图像位置的平均,'robust'表示鲁棒的训练方法,标准图像的位置是各训练图像的中值,此模式在训练图像中可能存在ERROR时使用,'direct'表示标准图像由单张图像经过处理得到,由此方法得到的标准图像只能应用prepare_direct_variation_model算子得到variation model。
get_variation_model( : Image, VarImage : ModelID : )返回variation model中的标准图像(Image)和variation image(VarImage),此算子主要用来检视创建的variation model是否OK。
prepare_variation_model( : : ModelID, AbsThreshold, VarThreshold : )设置variation model的绝对阈值和相对阈值。绝对阈值即待检测图像与标准图像的差值,相对阈值即待检测图像与variation model与VarThreshold乘绩的差值。
clear_train_data_variation_model( : : ModelID : )清除variation model的训练数据所占用的内存。
compare_variation_model(Image : Region : ModelID : )待检测图像与variation model进行比较,超过阈值的区域在Rgion参数中返回。同threshold一样,返回的区域被看做一个区域,可以使用connection算子进行连通性分析,然后根据区域的特征(如面积)对区域进行选择。
clear_variation_model( : : ModelID : )释放一个variation model的内存空间
PS:在model训练和比较的时候,常常需要对图像进行模板匹配,以使图像准确对齐。
总结:
Variation Model使用标准图像与待检测图像灰度值相比较,来判断产品是否OK,适用于印刷品检测及产品表面检测。从实际算法过程可以看出,此检测实际可分为两部分,对于图像中的大面积灰度一致区域,主要利用待检测图像与标准图像(ideal image)比较得出差异区域,对于图像中的边缘位置(edges)区域,主要利用待检测图像与Variation图像(variation image)比较得出差异区域。所以在实际应用中,应根据实际情况设置AbsThreshold和VarThreshold的值。
演示代码:
* This example program shows how to use local deformable
* matching to find and inspect objects that are deformed.
*
dev_update_off ()
Smoothness := 25
read_image (ModelImage, 'gasket/gasket_model')
read_image (Image, 'gasket/gasket_01')
dev_close_window ()
dev_open_window_fit_image (ModelImage, 0, 0, 500, -1, WindowHandle1)
set_display_font (WindowHandle1, 16, 'mono', 'true', 'false')
get_window_extents (WindowHandle1, Row, Column, Width, Height)
dev_open_window_fit_image (Image, 0, Width + 12, 1024 - Width - 36, -1, WindowHandle2)
set_display_font (WindowHandle2, 16, 'mono', 'true', 'false')
*
* Create variation model
sobel_amp (ModelImage, EdgeAmplitude, 'thin_max_abs', 5)
create_variation_model (425, 410, 'byte', 'direct', VariationModelID)
prepare_direct_variation_model (ModelImage, EdgeAmplitude, VariationModelID, 30, 1.5)
*
* Create locally deformable model
create_local_deformable_model (ModelImage, 'auto', [], [], 'auto', 0.9, [], 'auto', 0.9, [], 'auto', 'none', 'use_polarity', 'auto', 'auto', [], [], ModelID)
get_deformable_model_contours (ModelContours, ModelID, 1)
area_center (ModelImage, Area, Row, Column)
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_translate (HomMat2DIdentity, Row, Column, HomMat2DTranslate)
affine_trans_contour_xld (ModelContours, ContoursAffineTrans, HomMat2DTranslate)
dev_set_window (WindowHandle1)
dev_set_line_width (2)
dev_set_color ('yellow')
dev_display (ModelImage)
dev_display (ContoursAffineTrans)
disp_message (WindowHandle1, 'Model image and contours', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle1, 'black', 'true')
stop ()
*
* Process images iteratively
NumImages := 7
for Index := 1 to NumImages by 1
read_image (Image, 'gasket/gasket_' + Index$'02')
dev_set_window (WindowHandle2)
dev_display (Image)
disp_message (WindowHandle2, 'Search ...', 'window', 12, 12, 'black', 'true')
* Find the model in the search image.
* As result, the rectified image, the respective
* vector field, and the found contours are queried.
count_seconds (S1)
find_local_deformable_model (Image, ImageRectified, VectorField, DeformedContours, ModelID, rad(-10), rad(20), 1, 1, 1, 1, 0.93, 1, 0.7, 0, 0.4, ['image_rectified','vector_field','deformed_contours'], ['deformation_smoothness','expand_border','subpixel'], [Smoothness,0,1], Score, Row, Column)
count_seconds (S2)
Time := S2 - S1
if (|Score| > 0)
gen_warped_mesh_region (VectorField, MeshRegion, Smoothness)
gen_region_contour_xld (DeformedContours, EdgeRegion, 'margin')
dilation_circle (EdgeRegion, RegionDilation, 2 * Smoothness)
intersection (RegionDilation, MeshRegion, RegionIntersection)
dev_set_line_width (1)
dev_set_color ('yellow')
dev_display (RegionIntersection)
Found[Index] := |Score|
dev_set_line_width (2)
dev_set_color ('green')
dev_display (DeformedContours)
disp_message (WindowHandle2, ['Match found in ' + Time$'1.2f' + ' s','Score: ' + Score$'.2f'], 'window', 12, 12, 'black', 'true')
dev_set_window (WindowHandle1)
dev_display (ImageRectified)
compare_variation_model (ImageRectified, Region, VariationModelID)
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 40, 99999)
count_obj (SelectedRegions, Number)
if (Number > 0)
area_center (SelectedRegions, Area, Row1, Column1)
elliptic_axis (SelectedRegions, Ra, Rb, Phi)
tuple_gen_const (Number, 1, Ones)
PointOrder := []
for Idx := 0 to Number - 1 by 1
PointOrder := [PointOrder,'positive']
endfor
gen_ellipse_contour_xld (ContEllipse, Row1, Column1, Phi, Ra + 10, Rb + 10, 0 * Ones, 6.28318 * Ones, PointOrder, 1.5)
dev_set_color ('red')
dev_display (ContEllipse)
disp_message (WindowHandle1, 'Part not OK!', 'window', 12, 12, 'red', 'true')
else
disp_message (WindowHandle1, 'Part OK', 'window', 12, 12, 'forest green', 'true')
endif
else
disp_message (WindowHandle2, 'Nothing found', 'window', 12, 12, 'black', 'true')
endif
if (Index < NumImages)
disp_continue_message (WindowHandle2, 'black', 'true')
stop ()
endif
endfor


对于这个例子可以参考很久之前勇哥有一篇贴子:
---------------------
作者:hackpig
来源:www.skcircle.com
版权声明:本文为博主原创文章,转载请附上博文链接!


少有人走的路


















