【Halcon - Example】letters_knn.hdev

* This example program shows how to use a simple k-NN OCR classifier
* 
dev_update_off ()
read_image (Image, 'letters')
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
dev_set_colored (12)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
get_tmp_dir (TmpDir)
TrainFile := TmpDir + '/letters.trf'
dev_display (Image)
gen_rectangle1 (Rectangle, 0, 0, Height - 1, 400)
reduce_domain (Image, Rectangle, Image)
* Segment the image
binary_threshold (Image, Region, 'max_separability', 'dark', UsedThreshold)
* Connect the i's and j's with their dots
dilation_circle (Region, RegionDilation, 3.5)
* Compute the correct connected components
connection (RegionDilation, ConnectedRegions)
* Reduce each connected component (character) to its original shape
intersection (ConnectedRegions, Region, RegionIntersection)
* Sort the characters line-by-line
sort_region (RegionIntersection, Characters, 'character', 'true', 'row')
dev_display (Characters)
disp_message (WindowHandle, 'Training characters', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* Compute the true class of each character
count_obj (Characters, Number)
Length := Number / 27
Classes := []
for J := 0 to 25 by 1
    Classes := [Classes,gen_tuple_const(Length,chr(ord('a') + J))]
endfor
Classes := [Classes,gen_tuple_const(Length,'.')]
* Construct the necessary training file from the segmented characters
write_ocr_trainf (Characters, Image, Classes, TrainFile)
* Create the classifier.  We read out the classes from the train file.
* Therefore, the training part of the program is generic and can be used
* to train any OCR classifier
read_ocr_trainf_names (TrainFile, CharacterNames, CharacterCount)
create_ocr_class_knn (8, 10, 'constant', 'default', CharacterNames, [], [], OCRHandle)
* Train the classifier
trainf_ocr_class_knn (OCRHandle, TrainFile, [], [])
* 
* Now test the classifier on the whole training image
full_domain (Image, Image)
* Segment characters the same way as before
binary_threshold (Image, Region, 'max_separability', 'dark', UsedThreshold1)
dilation_circle (Region, RegionDilation, 3.5)
connection (RegionDilation, ConnectedRegions)
intersection (ConnectedRegions, Region, RegionIntersection)
sort_region (RegionIntersection, Characters, 'character', 'true', 'row')
* Classification
do_ocr_multi_class_knn (Characters, Image, OCRHandle, Class, Confidence)
* 
* Display results
area_center (Characters, Area, Row, Column)
dev_display (Image)
set_display_font (WindowHandle, 16, 'sans', 'true', 'false')
disp_message (WindowHandle, Class, 'image', Row - 16, Column + 8, 'blue', 'false')
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
disp_message (WindowHandle, 'Classification result', 'window', 12, 12, 'black', 'true')
clear_ocr_class_knn (OCRHandle)
dev_set_check ('~give_error')
delete_file (TrainFile)
dev_set_check ('give_error')

 

你可能感兴趣的:(《Halcon》)