该例程是基于形状的模板匹配,适用于形状特征比较明显、稳定的情况。
*模板匹配基于形状 2020/3/21
*更新
dev_update_pc ('off')
dev_update_window ('off')
dev_update_var ('off')
dev_close_window ()
*读入图像
read_image (Image, 'C:/形状匹配1.JPG')
*rgb3_to_gray (Image0, Image0, Image0, Image)
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
dev_display (Image)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_set_draw ('margin')
dev_set_color ('red')
dev_set_line_width (1)
stop()
*得到模板
gen_rectangle2 (ROI_0, 1534.18, 1498.9, rad(-8.77274), 826.145, 211.723)
reduce_domain (Image, ROI_0, ImageReduced)
*创建模板(对比度设置太高,找不到轮廓)(创建模板后,初始坐标变为0,0,0)
create_scaled_shape_model (ImageReduced, 5, rad(0),rad(360), 'auto', 0.8, 1.2, 'auto', 'none', 'ignore_global_polarity',20,10, ModelID)
*看看轮廓(这时轮廓在0,0,0)
get_shape_model_contours (Model, ModelID, 1)
*读入图像
read_image (Image2, 'C:/形状匹配2.JPG')
*rgb3_to_gray (Image1, Image1, Image1, Image2)
dev_display (Image2)
set_tposition (WindowHandle, 50, 50)
write_string (WindowHandle, '读入图像')
set_tposition (WindowHandle, 175, 50)
write_string (WindowHandle, '开始识别')
stop()
*创建时rad(0),rad(360),寻找时也要rad(0),rad(360)
find_scaled_shape_model (Image2, ModelID, rad(0), rad(360), 0.7, 1.3, 0.25,0, 0.5, 'least_squares', 5, 0.8, Row, Column, Angle, Scale, Score)
*显示
if(|Score| > 0)
for I := 0 to |Score| - 1 by 1
*仿射变换:显示出所识别物体的轮廓
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_translate (HomMat2DIdentity, Row[I], Column[I], HomMat2DTranslate)
hom_mat2d_rotate (HomMat2DTranslate, Angle[I], Row[I], Column[I], HomMat2DRotate)
hom_mat2d_scale (HomMat2DRotate, Scale[I], Scale[I], Row[I], Column[I], HomMat2DScale)
affine_trans_contour_xld (Model, ModelTrans, HomMat2DScale)
dev_display (Image2)
dev_display (ModelTrans)
*直接显示:只能显示模型轮廓
*dev_display_shape_matching_results (ModelID, 'red', Row[I], Column[I], Angle[I], 1, 1, 0)
disp_message (WindowHandle,'识别出第'+(I+1)+'个轮廓', 'window', 12, 12, 'black', 'true')
endfor
endif
*清除模型
clear_shape_model (ModelID)
识别结果
模板图像和测试图像如下