一、单模板多模板匹配
********************** 1.单模板 **********************
dev_set_draw ('margin')
read_image (Image, '1.bmp')
*产生模板区域
gen_rectangle1 (Rectangle, 248.463, 612.313, 356.761, 733.762)
reduce_domain (Image, Rectangle, ImageReduced)
*模板匹配
create_shape_model (ImageReduced, 'auto', rad(-30), rad(60), 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID)
get_shape_model_contours (ModelContours, ModelID, 1)
find_shape_model (Image, ModelID, rad(-30), rad(60), 0.5, 20, 0.5, 'least_squares', 0, 0.9, Row, Column, Angle, Score)
*显示
if(|Row|>0)
dev_display (Image)
disp_message (3600, '圆形个数:'+|Row|, 'window', 10, 10, 'red', 'true')
dev_display_shape_matching_results (ModelID, 'red', Row, Column, Angle, 1, 1, 0)
gen_cross_contour_xld (Cross, Row, Column, 6, Angle)
endif
********************** 2.多模板 **********************
read_image (Image, '1.bmp')
gen_rectangle1 (Rectangle1, 248.463, 612.313, 356.761, 733.762)
gen_rectangle1 (Rectangle2, 48.0477, 649.082, 160.081, 759.389)
gen_rectangle1 (Rectangle3, 488.712, 640.169, 563.4, 793.929)
reduce_domain (Image, Rectangle1, ImageReduced1)
reduce_domain (Image, Rectangle2, ImageReduced2)
reduce_domain (Image, Rectangle3, ImageReduced3)
create_shape_model (ImageReduced1, 'auto', rad(-180), rad(180), 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID1)
create_shape_model (ImageReduced2, 'auto', rad(-180), rad(180), 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID2)
create_shape_model (ImageReduced3, 'auto', rad(-180), rad(180), 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID3)
get_shape_model_contours (ModelContours1, ModelID1, 1)
get_shape_model_contours (ModelContours2, ModelID2, 1)
get_shape_model_contours (ModelContours3, ModelID3, 1)
find_shape_models (Image, [ModelID1,ModelID2,ModelID3], rad(-180), rad(180), 0.5, 30, 0.5, 'least_squares', 0, 0.9, Row1, Column1, Angle1, Score1, Model)
if(|Row1|>0)
dev_display (Image)
dev_display_shape_matching_results ([ModelID1,ModelID2,ModelID3], ['red','green','blue'], Row1, Column1, Angle1, 1, 1, Model)
endif
二、仿射变换
dev_set_draw ('margin')
dev_set_line_width (3)
* 读入图像
read_image (Model, 'model.bmp')
* 指定用于创建模板的区域
threshold (Model, Region, 150, 210)
dilation_circle (Region, RegionDilation, 3.5)
reduce_domain (Model, RegionDilation, ImageReduced)
area_center (ImageReduced, Area0, Row0, Column0)
* 创建模板
create_shape_model (ImageReduced, 'auto', -rad(360), rad(360), 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID)
get_shape_model_contours (ModelContours, ModelID, 1)
* 设置检测区域
threshold (Model, RegionInspect, 80, 150)
* 循环进行测试
list_files ('./', ['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])
* 模板匹配
find_shape_model (Image, ModelID, -rad(360), rad(360), 0.5, 1, 0.5, 'least_squares', 0, 0.9, Row, Column, Angle, Score)
dev_display_shape_matching_results (ModelID, 'red', Row, Column, Angle, 1, 1, 0)
* 放射变换
vector_angle_to_rigid (Row0, Column0, 0, Row, Column, Angle, HomMat2D)
* 检测区域跟随
dev_set_color ('green')
affine_trans_region (RegionInspect, RegionAffineTrans, HomMat2D, 'false')
endfor