HALCON示例程序color_pieces.hdev通過MLP訓練器對彩色棋子進行分類識別;分別在彩色圖像下與灰度圖像下進行,從而產生對比。
示例程序源碼(加注釋)
- 關于顯示類函數解釋
dev_update_off ()
dev_close_window ()
dev_open_window (0, 0, 557, 416, ‘black’, WindowHandle)
set_display_font (WindowHandle, 14, ‘mono’, ‘true’, ‘false’)
dev_set_draw (‘margin’) - 初始化
ImageRootName := ‘color/color_pieces_0’
Regions := [‘yellow’,‘pink’,‘blue’,‘background’]
Highlight := [‘goldenrod’,‘magenta’,‘cyan’]
gen_empty_obj (Classes)
for Mode := 0 to 1 by 1
dev_set_color (‘black’)
read_image (Image, ImageRootName + ‘0’)- 生成灰度三通道圖像
if (Mode == 1)- rgb1_to_gray - 將RGB圖像轉換為灰度圖像。
- rgb1_to_gray(RGB圖:灰度圖 ::);轉換公式:灰色= 0.299 *R+ 0.587 *G+ 0.114 *B。
rgb1_to_gray (Image, GrayImage) - compose3 - 將3個圖像轉換為三通道圖像。
- compose3(圖1,圖2,圖3:多通道圖像 ::)
compose3 (GrayImage, GrayImage, GrayImage, Image)
dev_display (Image)
disp_message (WindowHandle, ‘Train and apply the classes again on gray images’, ‘window’, 12, 12, ‘black’, ‘false’)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop ()
endif
if (Mode == 0) - 指定顏色類
for I := 1 to 4 by 1
dev_display (Image)
dev_display (Classes)
disp_message (WindowHandle, [‘Drag rectangle inside ’ + Regions[I - 1] + ’ color’,‘Click right mouse button to confirm’], ‘window’, 24, 12, ‘black’, ‘false’)
draw_rectangle1 (WindowHandle, Row1, Column1, Row2, Column2)
gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2) - 帶有concat_obj解釋的貼子注意和union1的區別
concat_obj (Classes, Rectangle, Classes)
endfor
endif
- 創建MLP分類器并添加訓練樣本;關于MLP分類器解釋的例子
create_class_mlp (3, 7, 4, ‘softmax’, ‘normalization’, 3, 42, MLPHandle)
add_samples_image_class_mlp (Image, Classes, MLPHandle)
disp_message (WindowHandle, ‘Training…’, ‘window’, 100, 12, ‘black’, ‘false’)
train_class_mlp (MLPHandle, 400, 0.5, 0.01, Error, ErrorLog)
for J := 0 to 3 by 1
read_image (Image, ImageRootName + J)
if (Mode == 1)
rgb1_to_gray (Image, GrayImage)
compose3 (GrayImage, GrayImage, GrayImage, Image)
endif
classify_image_class_mlp (Image, ClassRegions, MLPHandle, 0.5)
dev_display (Image)
disp_message (WindowHandle, ‘Looking for 4 game pieces of each color …’, ‘window’, 24, 12, ‘black’, ‘false’)
dev_set_line_width (2)
for Figure := 1 to 3 by 1
* copy_obj - 復制HALCON數據庫中的圖標對象。
* copy_obj(要復制對象:復制出的對象,開始索引號,對象數量:)
copy_obj (ClassRegions, ObjectsSelected, Figure, 1)
* 分割定義域
connection (ObjectsSelected, ConnectedRegions)
* 通過面積篩選區域
select_shape (ConnectedRegions, SelectedRegions, ‘area’, ‘and’, 400, 99999)
* 對區域進行計數
count_obj (SelectedRegions, Number)
dev_set_color (Highlight[Figure - 1])
dev_display (SelectedRegions)
OutString := Regions[Figure - 1] + ': ’ + Number + ’ ’
dev_set_color (‘green’)
disp_message (WindowHandle, OutString, ‘window’, 24 + 30 * Figure, 12, ‘black’, ‘false’)
if (Number != 4)
disp_message (WindowHandle, ‘Not OK’, ‘window’, 24 + 30 * Figure, 120, ‘red’, ‘false’)
else
disp_message (WindowHandle, ‘OK’, ‘window’, 24 + 30 * Figure, 120, ‘green’, ‘false’)
endif
endfor
if (J < 3)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop ()
endif
endfor
endfor
dev_clear_window ()
dev_display (Image)
Message := ‘The game pieces cannot be classified reliable on’
Message[1] := ‘gray images because the gray values of the’
Message[2] := ‘game pieces cannot always be distinguished from’
Message[3] := ‘the gray values of the background.’
disp_message (WindowHandle, Message, ‘window’, 12, 12, ‘black’, ‘true’)
- 生成灰度三通道圖像
處理思路
這個例子是將三通道的RGB圖像使用MLP分類器進行分類,分別對灰度圖像與彩色圖像進行了訓練與識別,對比發現還是彩色圖像分類較為準確,因為灰度圖像不能很好地分割出棋子與背景。
后記
大家有什么問題可以向我提問哈,我看到了第一時間回復,希望在學習的路上多多結交良師益友。