*create_variation_model( : : Width, Height, Type, Mode : ModelID)
说明:创建一个ID为ModelID,宽为Width,高为Height,类型为Type的Variation Model,参数Mode决定了创建标准图像和相应的variation图像的方法。'standard'表示标准的训练方法,标准图像的位置是各训练图像位置的平均,'robust'表示鲁棒的训练方法,标准图像的位置是各训练图像的中值,此模式在训练图像中可能存在ERROR时使用,'direct'表示标准图像由单张图像经过处理得到,由此方法得到的标准图像只能应用prepare_direct_variation_model算子得到variation model。
*prepare_variation_model( : : ModelID, AbsThreshold, VarThreshold : )
说明:设置variation model的绝对阈值和相对阈值。绝对阈值即待检测图像与标准图像的差值,相对阈值即待检测图像与variation model与VarThreshold乘绩的差值。
Smoothness := 25
read_image (ModelImage, 'E:/项目/电芯上料现场/江苏11拉/20230703/11标定图/泡沫横1/1.bmp')
gen_rectangle1 (ROI_0, 510.681, 2581.9, 1171.39, 3286.78)
reduce_domain (ModelImage, ROI_0, ImageReduced)
crop_domain (ImageReduced, ImagePart)
get_image_size (ImagePart, Width, Height)
dev_get_window (WindowHandle)
*提取对比图像
sobel_amp (ImagePart, EdgeAmplitude, 'thin_sum_abs_binomial', 7)
threshold (EdgeAmplitude, Region1, 100, 255)
connection (Region1, ConnectedRegions1)
select_shape (ConnectedRegions1, SelectedRegions1, 'area', 'and', 20, 99999)
region_to_mean (SelectedRegions1, EdgeAmplitude, EdgeAmplitude)
//创建模板
create_variation_model (Width, Height, 'byte', 'direct', VariationModelID)
prepare_direct_variation_model (ImagePart, EdgeAmplitude, VariationModelID, 40, 2.5)
create_local_deformable_model (ImagePart, 'auto', [], [], 'auto', 0.9, [], 'auto', 0.9, [], 'auto', 'none', 'use_polarity', 'auto', 'auto', [], [], ModelID)
get_deformable_model_contours (ModelContours, ModelID, 1)
area_center (ImagePart, Area, Row, Column)
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_translate (HomMat2DIdentity, Row, Column, HomMat2DTranslate)
affine_trans_contour_xld (ModelContours, ContoursAffineTrans, HomMat2DTranslate)
dev_set_window (WindowHandle)
dev_set_line_width (2)
dev_set_color ('yellow')
dev_display (ImagePart)
dev_display (ContoursAffineTrans)
//查找模板,标记缺陷位置
list_files ('E:/项目/电芯上料现场/标定图/泡沫横1', ['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])
count_seconds (S1)
find_local_deformable_model (Image, ImageRectified, VectorField, DeformedContours, ModelID, rad(-10), rad(20), 1, 1, 1, 1, 0.7, 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_empty_obj (MeshRegion)
count_obj (VectorField, Number)
for IndexI := 1 to Number by 1
select_obj (VectorField, ObjectSelected, IndexI)
vector_field_to_real (ObjectSelected, DRow, DCol)
get_image_size (VectorField, Width1, Height1)
for ContR := 0.5 to Height1[0] - 1 by Smoothness
Col1 := [0.5:Width[0] - 1]
tuple_gen_const (Width[0] - 1, ContR, Row1)
*get_grayval_interpolated— 返回图像在元组给定位置的灰度值 行和列。
get_grayval_interpolated (DRow, Row1, Col1, 'bilinear', GrayRow)
get_grayval_interpolated (DCol, Row1, Col1, 'bilinear', GrayCol)
gen_region_points (Region, GrayRow, GrayCol)
concat_obj (MeshRegion, Region, MeshRegion)
endfor
for ContC := 0.5 to Width1[0] - 1 by Smoothness
Row1 := [0.5:Height[0] - 1]
tuple_gen_const (Height[0] - 1, ContC, Col1)
get_grayval_interpolated (DRow, Row1, Col1, 'bilinear', GrayRow)
get_grayval_interpolated (DCol, Row1, Col1, 'bilinear', GrayCol)
gen_region_points (Region, GrayRow, GrayCol)
concat_obj (MeshRegion, Region, MeshRegion)
endfor
endfor
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)
dev_set_window (WindowHandle)
dev_display (ImageRectified)
disp_message (WindowHandle, ['模板时间 ' + Time$'1.2f' + ' s','模板分数: ' + Score$'.2f'], 'window', 12, 12, 'black', 'true')
disp_message (WindowHandle, Index, 'window', 60, 12, 'forest green', 'true')
wait_seconds (2)
*标记缺陷位
*compare_variation_model- 将图像与变体模型进行比较。
compare_variation_model (ImageRectified, Region, VariationModelID)
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 100, 299)
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 (WindowHandle, '模板NG', 'window', 80, 12, 'red', 'true')
else
disp_message (WindowHandle, '模板OK', 'window', 80, 12, 'forest green', 'true')
endif
else
disp_message (WindowHandle, '未找到模板', 'window', 80, 12, 'black', 'true')
endif
wait_seconds (2)
endfor
标签:get,变异,创建,variation,Halcon,dev,图像,model,gen
From: https://blog.csdn.net/2202_75557553/article/details/143088043