halcon案例學習之cbm_label_simple

*cbm_label_simple 程序說明:
*這個示例程序展現瞭如何使用基於組件的匹配來定位複合對象。在這種狀況下,應該在圖像中找到一個標籤,用戶既不知道其中的組件,也不知道它們之間的關係。所以,建立組件模型須要三個操做符:
* 1) gen_initial_components
* 2) train_model_components
* 3) create_trained_component_model
*最後,使用
* 4) find_component_modeldom

dev_update_off ()
dev_close_window ()
*獲取全部搜索圖像
list_image_files ('label', 'default', [], SearchImageFiles)
SearchImageFiles := regexp_select(SearchImageFiles,'(label_[0-9]+.png)|(label_training_)')
*讀取並顯示模型圖像
read_image (Image, 'label/label_model')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_display (Image)
*設置顯示設置
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_set_line_width (2)
dev_set_draw ('margin')
dev_set_color ('blue')
*定義模型區域
gen_rectangle1 (ModelRegion, 119, 106, 330, 537)
*reduce_domain —縮小圖像的域。
reduce_domain (Image, ModelRegion, ModelImage)
dev_display (ModelRegion)
disp_message (WindowHandle, 'Model image and model region', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
*獲取訓練圖像
*gen_empty_obj —建立一個空的對象元組
gen_empty_obj (TrainingImages)
for Index := 1 to 5 by 1
read_image (TrainingImage, 'label/label_training_' + Index)
*concat_obj —鏈接兩個圖標對象元組
concat_obj (TrainingImages, TrainingImage, TrainingImages)
endfor
*自動提取初始組件
* ---------------------------------------------
*gen_initial_components —提取組件模型的初始組件
gen_initial_components (ModelImage, InitialComponents, 40, 40, 20, 'connection', [], [])
dev_set_line_width (1)
dev_set_colored (12)
dev_display (Image)
dev_display (InitialComponents)
disp_message (WindowHandle, 'Extracted initial components', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 提取模型組件並訓練關係
* -----------------------------------------------------
* (這可能須要幾分鐘!)
*get_system —獲取HALCON系統參數的當前值
get_system ('pregenerate_shape_models', SavePregenerateShapeModels)
*set_system —設置HALCON系統參數
*pregenerate_shape_models 形狀模型 此參數肯定使用create_shape_model或create_scaled_shape_model
*建立的形狀模型是否已徹底預先生成('true')。該參數主要用於在最少的代碼更改下實現兩種模式之間的切換。一般,僅須要插入或更改一行。
*值:「 true」或「 false」 默認值:'false'
set_system ('pregenerate_shape_models', 'true')
*train_model_components —爲基於組件的匹配訓練組件和關係
train_model_components (ModelImage, InitialComponents, TrainingImages, ModelComponents, 40, 40, 20, 0.85, -1, -1, rad(15), 'reliability', 'rigidity', 0.2, 0.5, ComponentTrainingID)
set_system ('pregenerate_shape_models', SavePregenerateShapeModels)
*訓練結果能夠寫入文件,以便
*結果能夠在之後的程序中從文件中讀取
*執行和時間要求高的培訓不須要再次執行。
* write_training_components (ComponentTrainingID, 'train_label.ct')
* read_training_components ('train_label.ct', ComponentTrainingID)
dev_display (Image)
dev_display (ModelComponents)
disp_message (WindowHandle, 'Extracted model components', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
*爲計算出的關係添加小公差值,以覆蓋訓練圖像中未出現的組件變化。
*Modify_component_relations —修改訓練結果中的關係
modify_component_relations (ComponentTrainingID, 'all', 'all', 15, rad(5))
* 根據訓練結果建立組件模型
* -------------------------------------------------------
create_trained_component_model (ComponentTrainingID, -rad(30), rad(60), 10, 0.8, 'auto', 'auto', 'none', 'use_polarity', 'false', ComponentModelID, RootRanking)
clear_training_components (ComponentTrainingID)
*在運行時映像中查找組件模型
for Index := 0 to |SearchImageFiles| - 1 by 1
read_image (SearchImage, SearchImageFiles[Index])
*find_component_model —查找圖像中組件模型的最佳匹配
find_component_model (SearchImage, ComponentModelID, RootRanking, -rad(30), rad(60), 0.5, 0, 0.5, 'stop_search', 'prune_branch', 'none', 0.6, 'least_squares', 4, 0.9, ModelStart, ModelEnd, Score, RowComp, ColumnComp, AngleComp, ScoreComp, ModelComp)
dev_display (SearchImage)
for Match := 0 to |ModelStart| - 1 by 1
dev_display (SearchImage)
*get_found_component_model —返回找到的組件模型實例的組件
get_found_component_model (FoundComponents, ComponentModelID, ModelStart, ModelEnd, RowComp, ColumnComp, AngleComp, ScoreComp, ModelComp, Match, 'false', RowCompInst, ColumnCompInst, AngleCompInst, ScoreCompInst)
dev_display (FoundComponents)
endfor
disp_message (WindowHandle, 'Found components in image ' + (Index + 1) + ' of 15', 'window', 12, 12, 'black', 'true')
* 要不中止地運行程序,請取消激活如下行
if (Index < |SearchImageFiles| - 1)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endif
endfor
*clear_component_model —釋放組件模型的內存
clear_component_model (ComponentModelID)regexp

相關文章
相關標籤/搜索