* Prepare the local deformable model of the MVTec logo.
dev_update_off ()
Width := 256
Height := 256
*创建MVT图像(可以取代)
create_mvtec_logo_broadened (LogoImage, 0, 200, Width, Height)
*分三通道
decompose3 (LogoImage, ImageR, ImageG, ImageB)
WindowWidth := 2 * Width
WindowHeight := 2 * Height
*平滑度
Smoothness := 21
dev_close_window ()
dev_open_window (0, 0, WindowWidth, WindowHeight, 'black', WindowHandle1)
dev_display (LogoImage)set_display_font (WindowHandle1, 16, 'mono', 'true', 'false')
disp_message (WindowHandle1, 'Model image', 'window', 12, 12, 'black', 'true')
*灰度化
rgb1_to_gray (LogoImage, GrayImage)
*矩形裁剪
gen_rectangle1 (Rectangle, 82, 17, 177, 235)
*裁剪图片
reduce_domain (GrayImage, Rectangle, ImageReduced)
* 提取矩形模板域,稍后将其与搜索图像的校正版本进行比较。
crop_domain (ImageReduced, ImagePart)
* 使用use_polarity作为度量创建局部可变形模型。
create_local_deformable_model (ImageReduced, 'auto', [], [], 'auto', 1, [], 'auto', 1, [], 'auto', 'none', 'use_polarity', 'auto', 'auto', [], [], ModelID)
get_deformable_model_contours (ModelContours, ModelID, 1)
disp_continue_message (WindowHandle1, 'black', 'true')
stop ()
* 打开一个窗口,将校正后的匹配与裁剪后的模型图像进行比较。
get_image_size (ImagePart, WidthPart, HeightPart)
WindowWidthPart := 2 * WidthPart
WindowHeightPart := 2 * HeightPart
dev_open_window (0, WindowWidth + 8, WindowWidthPart * 1.5, WindowHeightPart * 1.5, 'black', WindowHandle2)
set_display_font (WindowHandle2, 16, 'mono', 'true', 'false')
* 出于演示目的,匹配被应用于搜索显示徽标的人为变形越来越大的图像。
gen_random_vector_field (VectorFieldFrom, Width, Height, 0, 2)
vector_field_to_real (VectorFieldFrom, VFFromRow, VFFromCol)
for Index := 1 to 5 by 1gen_random_vector_field (VectorFieldTo, Width, Height, 40, 2)vector_field_to_real (VectorFieldTo, VFToRow, VFToCol)for T := 0.0 to 0.95 by 0.05* 创建具有人工变形的搜索图像。create_deformed_mvtec_logo (VFFromRow, VFFromCol, VFToRow, VFToCol, ImageR, ImageG, ImageB, SearchImage, T)rgb1_to_gray (SearchImage, GrayImage)* 在变形的搜索图像中查找模型。因此校正图像、相应的矢量场和变形图像应返回已找到的模型实例的轮廓。count_seconds (Seconds1)find_local_deformable_model (GrayImage, ImageRectified, VectorField, DeformedContours, ModelID, 0, 0, 1, 1, 1, 1, 0.5, 1, 1, 4, 0.9, ['image_rectified', 'vector_field', 'deformed_contours'], ['deformation_smoothness', 'expand_border', 'subpixel'], [Smoothness,0, 1], Score, Row, Column)count_seconds (Seconds2)Time := 1000.0 * (Seconds2 - Seconds1)dev_set_window (WindowHandle1)* 对于可视化,我们使用返回的向量场并在搜索图像中生成变形网格。gen_warped_mesh (VectorField, WarpedMesh, 10)dev_display (SearchImage)dev_set_color ('yellow')dev_set_line_width (1)dev_display (WarpedMesh)dev_set_line_width (3)dev_set_color ('green')dev_display (DeformedContours)disp_message (WindowHandle1, 'Match found in ' + Time$'3.1f' + ' ms (Score: ' + Score$'3.2f' + ')', 'window', 12, 12, 'black', 'true')dev_set_window (WindowHandle2)* 为了可视化校正后的搜索图像和原始模型区域之间的差异,使用了绝对差异。abs_diff_image (ImagePart, ImageRectified, ImageAbsDiff1, 1)dev_display (ImageAbsDiff1)disp_message (WindowHandle2, 'Difference between model image and rectified image', 'window', 12, 12, 'black', 'true')* 通过使用光流(取消以下行的注释),可以进一步减小校正后的搜索图像和l模型区域之间的差异。optical_flow_mg (ImagePart, ImageRectified, VectorField1, 'clg', 0.8, 1, 20, 5, 'default_parameters', 'very_accurate')unwarp_image_vector_field (ImageRectified, VectorField1, ImageUnwarped)vector_field_to_real (VectorField1, RowField, ColField)vector_field_length (VectorField1, Length, 'length')abs_diff_image (ImagePart, ImageUnwarped, ImageAbsDiff, 1)
* stop ()dev_display (ImageAbsDiff1)endforcopy_obj (VFToRow, VFFromRow, 1, 1)copy_obj (VFToCol, VFFromCol, 1, 1)
endfor
二.简化代码和资料包
read_image (Jl01, 'C:/Users/user/Desktop/Halcon-Study/可变形模板匹配/JL_01.bmp')
gen_rectangle2 (ROI_0, 2123.53, 2151.85, rad(-80.7904), 275.892, 249.703)
rgb1_to_gray(Jl01, GrayImage1)
reduce_domain(GrayImage1, ROI_0, ImageReduced)crop_domain (ImageReduced, ImagePart)
create_local_deformable_model (ImageReduced, 'auto', [], [], 'auto', 1, [], 'auto', 1, [], 'auto', 'none', 'use_polarity', 'auto', 'auto', [], [], ModelID)
get_deformable_model_contours (ModelContours, ModelID, 1)
find_local_deformable_model (GrayImage1, ImageRectified, VectorField, DeformedContours, ModelID, 0, 359, 1,1,1, 1, 0.9, 8, 0.01, 6, 0.9, ['image_rectified', 'vector_field', 'deformed_contours'], ['deformation_smoothness', 'expand_border', 'subpixel'], [21,0, 1], Score, Row, Column)Step:=50
gen_empty_obj (WarpedMesh)
count_obj (VectorField, Number)
for Index := 1 to Number by 1* For visualization we use the returned vector field* and generate a grid of the deformation in the search image.select_obj (VectorField, ObjectSelected, Index)vector_field_to_real (ObjectSelected, DRow, DCol)vector_field_to_hom_mat2d(ObjectSelected, HomMat2D)hom_mat2d_to_affine_par(HomMat2D, Sx, Sy, Phi, Theta, Tx, Ty)get_image_size (VectorField, Width, Height)for ContR := 0.5 to Height[0] - 1 by StepCol1 := [0.5:Width[0] - 1]tuple_gen_const (Width[0] - 1, ContR, Row1)get_grayval_interpolated (DRow, Row1, Col1, 'bilinear', GrayRow)get_grayval_interpolated (DCol, Row1, Col1, 'bilinear', GrayCol)gen_contour_polygon_xld (Contour, GrayRow, GrayCol)concat_obj (WarpedMesh, Contour, WarpedMesh)endforfor ContC := 0.5 to Width[0] - 1 by StepRow1 := [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_contour_polygon_xld (Contour, GrayRow, GrayCol)concat_obj (WarpedMesh, Contour, WarpedMesh)endfor
endfordev_display (GrayImage1)dev_set_color ('yellow')dev_set_line_width (1)dev_display (WarpedMesh)dev_set_line_width (3)dev_set_color ('green')dev_display (DeformedContours)