此示例程序通过图像处理创建模型ROI
dev_update_window ('off')
* image acquisition and window size
*图像采集和窗口大小
open_framegrabber ('File', 1, 1, 0, 0, 0, 0, 'default', -1, 'default', -1, 'default', 'pendulum/pendulum.seq', 'default', -1, 1, FGHandle)
grab_image (ModelImage, FGHandle)
get_image_pointer1 (ModelImage, Pointer, Type, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_set_part (0, 0, Height - 1, Width - 1)
dev_display (ModelImage)
*可视化的颜色和其他设置
dev_set_color ('cyan')
dev_set_draw ('margin')
dev_set_line_width (2)
stop ()
第一步:Blob分析
threshold (ModelImage, BrightRegions, 200, 255)
connection (BrightRegions, ConnectedRegions)
fill_up (ConnectedRegions, FilledRegions)
dev_display (ModelImage)
dev_display (FilledRegions)
stop ()
![](https://i-blog.csdnimg.cn/blog_migrate/dbea5d41bcf96a55cbe1753a9303ca48.png)
第二步:选择卡片的区域
select_shape (FilledRegions, Card, 'area', 'and', 1800, 1900)
dev_set_part (round(0.2 * Height), round(0.1 * Width) - 1, round(0.7 * Height) - 1, round(0.6 * Width) - 1)
dev_display (ModelImage)
dev_display (Card)
stop ()
![](https://i-blog.csdnimg.cn/blog_migrate/78ca2bd91ca1c61525e3e1b7947525c1.png)
第三步:抠图
reduce_domain (ModelImage, Card, ImageCard)
stop ()
![](https://i-blog.csdnimg.cn/blog_migrate/e71e6bb968d5fbd6d813244c061cc07d.png)
第四步:提取徽标--Blob分析
dev_set_color ('blue')
threshold (ImageCard, DarkRegions, 0, 230)
connection (DarkRegions, ConnectedRegions)
select_shape (ConnectedRegions, Characters, 'area', 'and', 150, 450)
union1 (Characters, CharacterRegion)
dev_display (ModelImage)
dev_display (CharacterRegion)
stop ()
![](https://i-blog.csdnimg.cn/blog_migrate/fcb63113eba61ab43dc2b556e5617c39.png)
第五步:膨胀选择的区域
dilation_circle (CharacterRegion, ROI, 1.5)
dev_display (ModelImage)
dev_display (ROI)
stop ()
![](https://i-blog.csdnimg.cn/blog_migrate/b92a73956e8f2be172d6a351c39a432a.png)
第六步:创建模板
dev_set_part (0, 0, Height - 1, Width - 1)
dev_display (ModelImage)
reduce_domain (ModelImage, ROI, ImageROI)
*检查模板参数,此处设置了5个
inspect_shape_model (ImageROI, ShapeModelImages, ShapeModelRegions, 5, 25)
*选择第一个模板
select_obj (ShapeModelRegions, ShapeModelRegion, 1)
dev_display (ShapeModelRegion)
*创建模板
create_shape_model (ImageROI, 3, 0, rad(360), 'auto', 'none', 'use_polarity', 30, 10, ModelID)
*返回形状模型的轮廓表示形式。
get_shape_model_contours (ShapeModel, ModelID, 1)
stop ()
![](https://i-blog.csdnimg.cn/blog_migrate/a9bafe59f3a54b24e596c361b3fa67b0.png)
第七步:开始匹配
for i := 1 to 30 by 1
grab_image (SearchImage, FGHandle)
find_shape_model (SearchImage, ModelID, 0, rad(360), 0.7, 1, 0.5, 'least_squares', 0, 0.5, RowCheck, ColumnCheck, AngleCheck, Score)
if (|Score| > 0)
*仿射变换
vector_angle_to_rigid (0, 0, 0, RowCheck, ColumnCheck, AngleCheck, MovementOfObject)
affine_trans_contour_xld (ShapeModel, ModelAtNewPosition, MovementOfObject)
dev_display (SearchImage)
dev_display (ModelAtNewPosition)
endif
endfor
stop ()
![](https://i-blog.csdnimg.cn/blog_migrate/14dd2ce6ca6142530121f6b58d333d98.png)
![](https://i-blog.csdnimg.cn/blog_migrate/599a7c773aed7863ab8f1894bdc8ff39.png)
第八步:清除模板
dev_update_window ('on')
*清除模板
clear_shape_model (ModelID)
close_framegrabber (FGHandle)