* This example program shows how to use HALCON's variation model operators
* 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
* incorect clips is checked and classified.
dev_update_pc ('off')
dev_update_window ('off')
dev_update_var ('off')
read_image (Image, 'pen/pen-01')
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
get_system ('operating_system', OS)
if (OS{0:2} = 'Win')
set_font (WindowHandle, '-Courier New-18-*-*-*-*-')
else
set_font (WindowHandle, '-adobe-courier-bold-r-normal--25-*-*-*-*-*-*-*')
endif
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, 100, 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)
*查找合适的模板参数,准备创建模板
inspect_shape_model (ImageReduced, ModelImages, ModelRegions, 1, 20)
*转换一个骨架到XLD,这个只是用于显示骨架
gen_contours_skeleton_xld (ModelRegions, Model, 1, 'filter')
*计算中心点和面积
area_center (RegionDilation, Area, RowRef, ColumnRef)
*建立模板
create_shape_model (ImageReduced, 5, rad(-10), rad(20), 'auto', 'none', 'use_polarity', 20, 10, ShapeModelID)
*建立可变比较模板
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, ShapeModelID, rad(-30), rad(60), 0.5, 1, 0.5, 'least_squares', 0, 0.9, 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')
*训练可变模板
train_variation_model (ImageTrans, VariationModelID)
dev_display (ImageTrans)
dev_display (Model)
endif
endfor
*返回用于比较的图片
get_variation_model (MeanImage, VarImage, VariationModelID)
*准备比较参数
prepare_variation_model (VariationModelID, 20, 3)
* 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')
stop ()
dev_display (VarImage)
set_tposition (WindowHandle, 20, 20)
dev_set_color ('green')
write_string (WindowHandle, 'Variation image')
stop ()
dev_set_draw ('margin')
for I := 1 to 30 by 1
*读取图片
read_image (Image, 'pen/pen-'+I$'02d')
*查找模板
find_shape_model (Image, ShapeModelID, rad(-10), rad(20), 0.5, 1, 0.5, 'least_squares', 0, 0.9, 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, RegionROI, ImageReduced)
*比较图片
compare_variation_model (ImageReduced, RegionDiff, VariationModelID)
*连通区域
connection (RegionDiff, ConnectedRegions)
*选择区域
select_shape (ConnectedRegions, RegionsError, 'area', 'and', 20, 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
stop ()
endfor
stop ()
clear_shape_model (ShapeModelID)
clear_variation_model (VariationModelID)
* 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
* incorect clips is checked and classified.
dev_update_pc ('off')
dev_update_window ('off')
dev_update_var ('off')
read_image (Image, 'pen/pen-01')
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
get_system ('operating_system', OS)
if (OS{0:2} = 'Win')
set_font (WindowHandle, '-Courier New-18-*-*-*-*-')
else
set_font (WindowHandle, '-adobe-courier-bold-r-normal--25-*-*-*-*-*-*-*')
endif
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, 100, 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)
*查找合适的模板参数,准备创建模板
inspect_shape_model (ImageReduced, ModelImages, ModelRegions, 1, 20)
*转换一个骨架到XLD,这个只是用于显示骨架
gen_contours_skeleton_xld (ModelRegions, Model, 1, 'filter')
*计算中心点和面积
area_center (RegionDilation, Area, RowRef, ColumnRef)
*建立模板
create_shape_model (ImageReduced, 5, rad(-10), rad(20), 'auto', 'none', 'use_polarity', 20, 10, ShapeModelID)
*建立可变比较模板
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, ShapeModelID, rad(-30), rad(60), 0.5, 1, 0.5, 'least_squares', 0, 0.9, 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')
*训练可变模板
train_variation_model (ImageTrans, VariationModelID)
dev_display (ImageTrans)
dev_display (Model)
endif
endfor
*返回用于比较的图片
get_variation_model (MeanImage, VarImage, VariationModelID)
*准备比较参数
prepare_variation_model (VariationModelID, 20, 3)
* 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')
stop ()
dev_display (VarImage)
set_tposition (WindowHandle, 20, 20)
dev_set_color ('green')
write_string (WindowHandle, 'Variation image')
stop ()
dev_set_draw ('margin')
for I := 1 to 30 by 1
*读取图片
read_image (Image, 'pen/pen-'+I$'02d')
*查找模板
find_shape_model (Image, ShapeModelID, rad(-10), rad(20), 0.5, 1, 0.5, 'least_squares', 0, 0.9, 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, RegionROI, ImageReduced)
*比较图片
compare_variation_model (ImageReduced, RegionDiff, VariationModelID)
*连通区域
connection (RegionDiff, ConnectedRegions)
*选择区域
select_shape (ConnectedRegions, RegionsError, 'area', 'and', 20, 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
stop ()
endfor
stop ()
clear_shape_model (ShapeModelID)
clear_variation_model (VariationModelID)