根据木纹不一样判断不同的木材
*本示例的目的是根据木材的表面纹理对不同种类的木材进行分类
file_exists ('classify_wood.gmc', FileExists)//存在FileExists返回
if (FileExists)
USE_STORED_CLASSIFIER := 1
else
USE_STORED_CLASSIFIER := 0
endif
* First, the path to the images is set, the initial image
* is read and the settings are specified.
get_system ('image_dir', HalconImages)
get_system ('operating_system', OS)
if (OS{0:2} == 'Win')
tuple_split (HalconImages, ';', HalconImages)
else
tuple_split (HalconImages, ':', HalconImages)
endif
ImagePath := '/wood/'
ReadOK := false
dev_get_preferences ('suppress_handled_exceptions_dlg', SaveMode)
dev_set_preferences ('suppress_handled_exceptions_dlg', 'true')
for k := 0 to |HalconImages| - 1 by 1
try
read_image (Image, HalconImages[k] + ImagePath + 'apple/apple_01')
ReadPath := HalconImages[k] + ImagePath
ReadOK := true
break
catch (Exception)
endtry
endfor
if (not ReadOK)
disp_message (WindowID, 'Could not find the images in $HALCONIMAGES', 'window', 12, 12, 'black', 'true')
stop ()
endif
dev_set_preferences ('suppress_handled_exceptions_dlg', SaveMode)
read_image (Image, ImagePath + 'apple/apple_01')
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window_fit_image (Image, 0, 0, Width, Height, WindowID)
set_display_font (WindowID, 16, 'mono', 'true', 'false')
dev_display (Image)
dev_update_off ()
* Now the different wood classes are specified.
Classes := ['apple','beech','cherry','maple','oak','walnut']
* The program uses by default a stored classifier. If you, however,
* want to perform the training, set USE_STORED_CLASSIFIER to 0.
* If the classifier can not be found, USE_STORED_CLASSIFIER
* is set to 0 automatically.
if (USE_STORED_CLASSIFIER == 1)
read_class_mlp ('classify_wood.gmc', MLPHandle)
NumClasses := |Classes|
else
gen_features (Image, FeatureVector)
NumFeatures := |FeatureVector|
NumClasses := |Classes|
NumHidden := 15
create_class_mlp (NumFeatures, NumHidden, NumClasses, 'softmax', 'normalization', 10, 42, MLPHandle)
for CorrectClassID := 0 to NumClasses - 1 by 1
list_files (ReadPath + Classes[CorrectClassID], 'files', Files)
for k := 0 to |Files| - 1 by 2
read_image (Image, Files[k])
dev_display (Image)
gen_features (Image, FeatureVector)
add_sample_class_mlp (MLPHandle, FeatureVector, CorrectClassID)
endfor
endfor
train_class_mlp (MLPHandle, 200, 1, 0.0001, Error, ErrorLog)
write_class_mlp (MLPHandle, 'classify_wood.gmc')
disp_message (WindowID, 'Training of wood textures completed\nPress \'Run\' to continue', 'window', 12, 12, 'black', 'true')
stop ()
endif
Errors := 0
Count := 0
for CorrectClassID := 0 to NumClasses - 1 by 1
list_files (ReadPath + Classes[CorrectClassID], 'files', Files)
for k := 0 to |Files| - 1 by 1
Count := Count + 1
read_image (Image, Files[k])
gen_features (Image, FeatureVector)
classify_class_mlp (MLPHandle, FeatureVector, 2, FoundClassIDs, Confidence)
dev_display (Image)
dev_set_color ('blue')
disp_message (WindowID, 'correct class: ' + Classes[CorrectClassID], 'window', 12, 12, 'black', 'true')
if (CorrectClassID == FoundClassIDs[0])
disp_message (WindowID, 'found class: ' + Classes[FoundClassIDs[0]], 'window', 42, 12, 'forest green', 'true')
else
Errors := Errors + 1
disp_message (WindowID, 'found class: ' + Classes[FoundClassIDs[0]], 'window', 42, 12, 'red', 'true')
disp_continue_message (WindowID, 'black', 'true')
stop ()
endif
wait_seconds (0.1)
endfor
endfor
ErrorRate := real(Errors) / Count * 100.0
disp_message (WindowID, 'ErrorRate = ' + ErrorRate + '%', 'window', 72, 12, 'black', 'true')
disp_end_of_program_message (WindowID, 'black', 'true')
clear_class_mlp (MLPHandle)