基于形状的模板匹配:
经过一系列的图像预处理之后,提取圆形对象,选取Index为1的圆形对象,自动进行模板形状的创建,随后对TextImage进行模板匹配查找,从而计算图像中的对象个数(针对大于半圆的圆形对象)。
原始图像Image:
halcon代码:
*初始化
dev_update_off()
dev_close_window()
read_image (Image, 'C:/Users/Leason/Desktop/代码试验/2.jpg')
rgb1_to_gray (Image, GrayImage)
get_image_size (GrayImage, Width, Height)
dev_open_window_fit_image (GrayImage, 0, 0, Width/3, Height/3, WindowHandle)
dev_set_draw ('margin')
dev_set_line_width(2)
dev_set_color('white')
dev_display (GrayImage)
disp_message (WindowHandle, 'original image', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop()
*阈值分割
gen_rectangle1 (ROI, 320, 1, 680, 958)
reduce_domain (GrayImage, ROI, ImageReduced)
threshold (ImageReduced, Region, 90, 255)
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, Circles, ['circularity','area'], 'and', [0.85,50], [1.0,99999])
dev_set_color ('yellow')
dev_clear_window()
dev_display (ImageReduced)
dev_display (Circles)
dev_set_line_width(3)
count_obj (Circles, Number)//计算圆形的数量
area_center (Circles, Area, Row, Column)//获取每个圆形的中心坐标和面积
gen_cross_contour_xld (Cross, Row, Column, 6, rad(45))//在圆心创建交叉点
disp_message (WindowHandle, 'threshold image', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop()
*创建模板
select_obj (Circles, PillSelected, 1)
reduce_domain (Image, PillSelected, ImageReduced2)
threshold (ImageReduced2, Region, 90, 255)
inspect_shape_model (ImageReduced2, ModelImages, ModelRegions, 4, 30)
create_shape_model (ImageReduced2, 'auto', rad(0), rad(1), 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID)
get_shape_model_contours (ModelContours, ModelID, 1)
disp_message (WindowHandle, 'the shape modle has been created', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop()
*模板匹配
for I := 1 to 4 by 1
read_image (TextImage, 'video/video_' + I$'03d')
reduce_domain (TextImage, ROI, Image)
* 计算当前过去的时间,单位是秒
count_seconds (S1)
*在搜索图像中搜索模版
find_shape_model (Image, ModelID, rad(0), rad(1), 0.5, 0, 0.0, 'least_squares', 2, 0.5, Row, Column, Angle, Score)
* 计算当前过去的时间,单位是秒
count_seconds (S2)
Runtime := (S2 - S1) * 1000
*在对象中心点 生成十字对象
gen_cross_contour_xld (Cross, Row, Column, 6, rad(45))
*显示找到的模版轮廓
dev_display_shape_matching_results (ModelID, 'lime green', Row, Column, Angle, 1, 1, 0)
count_obj (Cross, Number)
*设置输出对象的颜色
dev_clear_window()
dev_set_color ('green')
dev_set_line_width(3)
*显示图像
dev_display (Image)
*显示十字
dev_display (Cross)
gen_region_line (ROI_0, 675.556, -50.75, 675.556, 985.75)
endfor
stop()
最终结果图像: