方案:直接用整幅图像做模板匹配
下面是过程:
原图
创建模板,下图是模板的轮廓
需要识别的图像
识别结果
代码:
dev_update_off ()
read_image (ModelImage, 'food/cocoa_package_model')
dev_close_window ()
dev_open_window_fit_image (ModelImage, 0, 0, -1, -1, WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
*
* Create and display the shape model
create_shape_model (ModelImage, 'auto', rad(-20), rad(40), 'auto', 'auto', 'use_polarity', [40,60,'auto_min_size'], 10, ModelID)
get_shape_model_contours (ModelContours, ModelID, 1)
area_center (ModelImage, Area, Row, Column)
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_translate (HomMat2DIdentity, Row, Column, HomMat2DTranslate)
affine_trans_contour_xld (ModelContours, ContoursAffinTrans, HomMat2DTranslate)
dev_set_line_width (2)
dev_set_color ('yellow')
dev_display (ModelImage)
dev_display (ContoursAffinTrans)
disp_message (WindowHandle, 'Model image and contours', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
*
NumImages := 13
for Index := 1 to NumImages by 1
read_image (Image, 'food/cocoa_packages_' + Index$'02')
* Reduce the image resolution for a speed-up
dev_resize_window_fit_image (Image, 0, 0, -1, -1)
dev_display (Image)
disp_message (WindowHandle, 'Search ...', 'window', 12, 12, 'black', 'true')
count_seconds (S1)
* Find the deformed model in the search image
* and display the results
find_shape_model (Image, ModelID, rad(-20), rad(40), 0.6, 0, 0.5, ['least_squares','max_deformation 16'], 0, 0.4, Row, Column, Angle, Score)
count_seconds (S2)
Time := (S2 - S1) * 1000
dev_set_color ('green')
dev_display_shape_matching_results (ModelID, 'green', Row, Column, Angle, 1, 1, ModelID)
disp_message (WindowHandle, |Score| + ' matches found in ' + Time$'3.1f' + ' ms', 'window', 12, 12, 'black', 'true')
for J := 0 to |Score| - 1 by 1
disp_message (WindowHandle, 'Score: ' + Score[J]$'.2f', 'image', 350, Column[J] - 80, 'black', 'true')
endfor
if (Index < NumImages)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endif
endfor
*
* Clean up
clear_shape_model (ModelID)