Hello Mat

 找回密码
 立即注册
查看: 7067|回复: 7

基于组件建模的匹配定位方法

[复制链接]

1323

主题

1551

帖子

0

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22647
发表于 2020-2-11 14:15:12 | 显示全部楼层 |阅读模式
基于组件建模的匹配定位方法,具体代码如下:
  1. * 基于组件之间的相对关系进行匹配component-based matching
  2. * 本程序展示了如何创建输入和输出的关系,训练,测试
  3. dev_update_off ()
  4. dev_set_draw ('margin')
  5. read_image (ModelImage, 'dip_switch/dip_switch_model')
  6. get_image_size (ModelImage, Width, Height)
  7. dev_close_window ()
  8. dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
  9. * 初始化组件,可手动框出目标的位置Define the initial components
  10. * 不变的两部分
  11. gen_rectangle2 (InitialComponents, 265, 138, -0.02, 23, 13)
  12. gen_rectangle2 (InitialComponent, 342, 286, -0.02, 168, 13)
  13. concat_obj (InitialComponents, InitialComponent, InitialComponents)
  14. * 变化的部分
  15. for I := 0 to 11 by 1
  16.     gen_rectangle2 (InitialComponent, 311 + I * 0.5, 128 + I * 28.2, 0, 10, 12)
  17.     concat_obj (InitialComponents, InitialComponent, InitialComponents)
  18. endfor
  19. stop()
  20. * 获取需要训练的样本图库,同一个物件(最好包含全部的不同状态),背景单一Get the training images
  21. gen_empty_obj (TrainingImages)
  22. for I := 1 to 12 by 1
  23.     read_image (TrainingImage, 'dip_switch/dip_switch_training_' + I02')
  24.     concat_obj (TrainingImages, TrainingImage, TrainingImages)
  25.     dev_display (TrainingImage)
  26. endfor
  27. stop()
  28. * 以下两句代码可以辅助我们找到最佳的参数:ContrastLow, ContrastHigh, and MinSize
  29. * add_channels (InitialComponents, ModelImage, ModelImageInitComp)
  30. * gen_initial_components (ModelImageInitComp, InitialComponentEdges, 45, 45, 30, 'connection', [], [])
  31. * 训练模型,并保存相应的模型方便下次直接读取 Extract the model components and train the relations
  32. * read_training_components ('train_dip_switch.ct', ComponentTrainingID)
  33. train_model_components (ModelImage, InitialComponents, TrainingImages, ModelComponents, 45, 45, 30, 0.95, -1, -1, rad(20), 'speed', 'rigidity', 0.2, 0.5, ComponentTrainingID)
  34. write_training_components (ComponentTrainingID, 'train_dip_switch.ct')
  35. get_training_components (ModelComponents, ComponentTrainingID, 'model_components', 'model_image', 'false', RowRef, ColumnRef, AngleRef, ScoreRef)
  36. dev_display (ModelComponents)
  37. stop()
  38. * 显示训练样本的组件相对关系
  39. count_obj (ModelComponents, NumComp)
  40. for I := 0 to NumComp - 1 by 1
  41.     dev_display (ModelImage)
  42.     get_component_relations (Relations, ComponentTrainingID, I, 'model_image', Row, Column, Phi, Length1, Length2, AngleStart, AngleExtent)
  43.     dev_display (Relations)
  44. endfor
  45. stop()
  46. * 增加训练模型的鲁棒性 to receive a higher robustness.
  47. modify_component_relations (ComponentTrainingID, 'all', 'all', 0, rad(4))
  48. ******** 测试
  49. MinScoreComp := gen_tuple_const(13,0.8)
  50. MinScoreComp[0] := 0.4
  51. NumLevelsComp := gen_tuple_const(13,0)
  52. NumLevelsComp[0] := 4
  53. dev_display (ModelImage)
  54. create_trained_component_model (ComponentTrainingID, 0, rad(360), 10, MinScoreComp, NumLevelsComp, 'auto', 'none', 'use_polarity', 'false', ComponentModelID, RootRanking)
  55. get_component_model_tree (Tree, Relations, ComponentModelID, RootRanking, 'model_image', StartNode, EndNode, Row, Column, Phi, Length1, Length2, AngleStart, AngleExtent)
  56. dev_display (ModelImage)
  57. dev_set_colored (12)
  58. dev_display (Tree)
  59. dev_display (Relations)
  60. clear_training_components (ComponentTrainingID)
  61. * 实时测试,采用已经创建好的模型ComponentModelID, RootRanking
  62. ImgNo := 1
  63. for i:=0 to 100 by 1
  64.     read_image (SearchImage, 'dip_switch/dip_switch_' + ImgNo02d')
  65.     count_seconds (Seconds1)
  66.     find_component_model (SearchImage, ComponentModelID, RootRanking, 0, rad(360), 0, 0, 0.5, 'stop_search', 'prune_branch', 'none', MinScoreComp, 'least_squares', 0, 0.9, ModelStart, ModelEnd, Score, RowComp, ColumnComp, AngleComp, ScoreComp, ModelComp)
  67.     count_seconds (Seconds2)
  68.     dev_display (SearchImage)
  69.     NumFound := |ModelStart|
  70.     Time := (Seconds2 - Seconds1) * 1000
  71.     for Match := 0 to |ModelStart| - 1 by 1
  72.         get_found_component_model (FoundComponents, ComponentModelID, ModelStart, ModelEnd, RowComp, ColumnComp, AngleComp, ScoreComp, ModelComp, Match, 'false', RowCompInst, ColumnCompInst, AngleCompInst, ScoreCompInst)
  73.         dev_display (FoundComponents)
  74.     endfor
  75. endfor
  76. clear_component_model (ComponentModelID)
复制代码




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复

使用道具 举报

1323

主题

1551

帖子

0

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22647
 楼主| 发表于 2020-2-11 21:06:24 | 显示全部楼层
简化的多组件模板匹配方法:
  1. * 基于组件建模的匹配定位方法
  2. *     1.)  create_component_model
  3. *     2.)  find_component_model
  4. dev_update_off ()
  5. dev_close_window ()
  6. read_image (ModelImage, 'modules/modules_model')
  7. dev_open_window_fit_image (ModelImage, 0, 0, -1, -1, WindowHandle)
  8. * Define the regions for the components
  9. gen_rectangle2 (ComponentRegions, 318, 109, -1.62, 34, 19)
  10. gen_rectangle2 (Rectangle2, 342, 238, -1.63, 32, 17)
  11. gen_rectangle2 (Rectangle3, 355, 505, 1.41, 25, 17)
  12. gen_rectangle2 (Rectangle4, 247, 448, 0, 14, 8)
  13. gen_rectangle2 (Rectangle5, 237, 537, -1.57, 13, 10)
  14. concat_obj (ComponentRegions, Rectangle2, ComponentRegions)
  15. concat_obj (ComponentRegions, Rectangle3, ComponentRegions)
  16. concat_obj (ComponentRegions, Rectangle4, ComponentRegions)
  17. concat_obj (ComponentRegions, Rectangle5, ComponentRegions)
  18. dev_set_colored (12)
  19. dev_set_draw ('margin')
  20. dev_set_line_width (2)
  21. dev_display (ModelImage)
  22. dev_display (ComponentRegions)
  23. stop ()
  24. * 创建多组件模板匹配模型
  25. * Create the component model by explicitly specifying the relations
  26. create_component_model (ModelImage, ComponentRegions, 20, 20, rad(25), 0, rad(360), 15, 40, 15, 10, 0.8, [4,3,3,3,3], 0, 'none', 'use_polarity', 'true', ComponentModelID, RootRanking)
  27. *
  28. * Find the component model in a run-time image
  29. ImageName := 'modules/modules_'
  30. for I := 1 to 12 by 1
  31.     read_image (SearchImage, ImageName + I'.2d')
  32.     find_component_model (SearchImage, ComponentModelID, RootRanking, 0, rad(360), 0.5, 0, 0.5, 'stop_search', 'search_from_best', 'none', 0.8, 'interpolation', 0, 0.8, ModelStart, ModelEnd, Score, RowComp, ColumnComp, AngleComp, ScoreComp, ModelComp)
  33.     dev_display (SearchImage)
  34.     * Display the found component models
  35.     for Match := 0 to |ModelStart| - 1 by 1
  36.         dev_set_line_width (1)
  37.         get_found_component_model (FoundComponents, ComponentModelID, ModelStart, ModelEnd, RowComp, ColumnComp, AngleComp, ScoreComp, ModelComp, Match, 'false', RowCompInst, ColumnCompInst, AngleCompInst, ScoreCompInst)
  38.         dev_display (FoundComponents)
  39.     endfor
  40.     stop ()
  41. endfor
  42. clear_component_model (ComponentModelID)
复制代码





本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-11-22 16:46 , Processed in 0.244395 second(s), 26 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表