Demo entry 6326889

123

   

Submitted by anonymous on Nov 23, 2016 at 14:54
Language: ANTLR With ObjectiveC Target. Code size: 2.7 kB.

*1创建训练文件
*1.1分割字符
dev_close_window()
read_image(Image,'C:/Users/脆脆/Pictures/S1')
get_image_size(Image, Width, Height)
dev_open_window(0, 0, Width, Height, 'black', WindowHandle)
dev_display(Image)
decompose3 (Image, Red, Green, Blue)
trans_from_rgb (Red, Green, Blue, Hue, Saturation, Intensity, 'hsv')
threshold(Red, Region, 180, 255)
*连通区域
connection(Region, ConnectedRegions)
*排队
sort_region (ConnectedRegions, SortedRegions, 'upper_left', 'true', 'column')
count_obj(SortedRegions, Number)
for Index := 1 to Number by 1
    dev_clear_window()
    select_obj(SortedRegions, SingleWord, Index)
    dev_display(SingleWord)
    stop()
endfor
*1.2文字分类标识 
words:=['1','6','2','4','2','6','7','6','6','5','3','0','0']
*1.3创建训练文件
TrainFile:='words.trf'
dev_set_check('~give_error')
delete_file(TrainFile)
dev_set_check('~give_error')
*1.4将图像字符与字符标识关联,保存到训练图像中
for i:=1 to Number by 1
    select_obj(SortedRegions, SingleWord, i)
    append_ocr_trainf(SingleWord,Image,words[i-1],TrainFile)
endfor
*2训练OCR
*2.1确定字体文件名
FontFile:='words.omc'
*2.2得到字符标识名
read_ocr_trainf_names(TrainFile, CharacterNames, CharacterCount)
*2.3确定神经网络隐藏层节点数
NumHidden:=20
*2.4创建神经网络分类器
create_ocr_class_svm(8, 10, 'constant', 'default', CharacterNames, 'rbf', 0.02, 0.001, 'one-versus-one', 'normalization', 0, OCRHandle)
*2.4训练神经网络(MLP&SVM)
trainf_ocr_class_svm(OCRHandle, TrainFile, 0.001, 'default')
*2.5保存训练结果
write_ocr_class_svm(OCRHandle, FontFile)
*2.6清除句柄
clear_ocr_class_svm(OCRHandle)

*3识别文字
*3.1读入图像
dev_close_window()
read_image(Image2,'C:/Users/脆脆/Pictures/S1')
get_image_size(Image2, Width, Height)
dev_open_window(0, 0, Width, Height, 'black', WindowHandle)
dev_display(Image)
*3.2对齐文字
text_line_orientation(Image2, Image, 25, rad(-45), rad(45), OrientationAngle)
*rotate_image(Image, ImageRotate, -OrientationAngle/rad(180)*180, 'constant')
*分割文字
decompose3 (Image2, Red, Green, Blue)
trans_from_rgb (Red, Green, Blue, Hue, Saturation, Intensity, 'hsv')
threshold(Red, TestWordsRegion, 180, 255)
connection(TestWordsRegion, TestSingleWords)
select_shape (TestSingleWords, SelectedRegions, 'area', 'and', 300, 9999)
sort_region (SelectedRegions, TestWordsSortedRegions, 'upper_left', 'true', 'column')
count_obj(TestWordsSortedRegions, Number)
read_ocr_class_svm('C:/Users/脆脆/Desktop/数字图像处理/words.omc', OCRHandle1)
*识别
do_ocr_multi_class_svm(TestWordsSortedRegions, Image2, OCRHandle1, Class)
for Index := 1 to Number by 1
  *  dev_display(ImageRotate)
    select_obj(TestWordsSortedRegions, ObjectSelected, Index)
    dev_display(ObjectSelected)
    disp_message(WindowHandle, Class[Index-1], 'image', 12, 20*Index, 'green', 'true')
    stop()
endfor
clear_ocr_class_svm (OCRHandle1) 

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).