*cbm_label_simple 程式說明:
*這個示例程式展示了如何使用基于組件的匹配來定位復合物件,在這種情況下,應該在影像中找到一個標簽,用戶既不知道其中的組件,也不知道它們之間的關系,因此,創建組件模型需要三個運算子:
* 1) gen_initial_components
* 2) train_model_components
* 3) create_trained_component_model
*最后,使用
* 4) find_component_model
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/248904.html
標籤:其他
