您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 造纸印刷 > Halcon-OCR训练及识别
HalconOCR总结By17003步骤:1.创建训练文件,将文字图像与文字分类标识关联,保存到训练文件中,训练文件后缀名为trf,主要用到函数为append_ocr_trainf。2.训练OCR分类器,Halcon支持BOX分类器、神经网络分类器(MLP)和支持向量机分类器(SVM),由于后两者比前者更加强大,推荐使用后两者。训练分类器非常简单,首先调用create_ocr_class_mlp或create_ocr_class_svm创建分类器,然后调用trainf_ocr_class_mlp或trainf_ocr_class_svm训练分类器,用write_ocr_class_mlp或write_ocr_class_svm可以保存训练结果。3.测试分类器,读入图像,如要文字不是水平,应将其旋转成水平,可以使用以下函数:text_line_orientation计算文字倾角,rotate_image旋转图像。注意前者使用的是弧度,后角使用的是度。分割文字。读取分类器read_ocr_class_mlp,即读取训练分类器的结果。使用do_ocr_multi_class_mlp对文字进行识别。参考以下实例:为方便起见,这里将三个步骤放在一个实例里,也可以分别将三个步骤保存成三个独立的程序。*1创建训练文件*1.1分割字符dev_close_window()read_image(Image,'F:/学习资料/Halcon工程/ocr-train.bmp')get_image_size(Image,Width,Height)dev_open_window(0,0,Width,Height,'black',WindowHandle)dev_display(Image)threshold(Image,Region,0,100)connection(Region,ConnectedRegions)sort_region(ConnectedRegions,SortedRegions,'upper_left','true','column')count_obj(SortedRegions,Number)forIndex:=1toNumberby1dev_clear_window()select_obj(SortedRegions,SingleWord,Index)dev_display(SingleWord)stop()endfor*1.2文字分类标识words:=['a','b','c','d','e','f','g']*1.3创建训练文件TrainFile:='words.trf'dev_set_check('~give_error')delete_file(TrainFile)dev_set_check('~give_error')*1.4将图像字符与字符标识关联,保存到训练图像中fori:=1toNumberby1select_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_mlp(8,10,'constant','default',CharacterNames,80,'none',10,42,OCRHandle)*2.4训练神经网络trainf_ocr_class_mlp(OCRHandle,TrainFile,200,1,0.01,Error,ErrorLog)*2.5保存训练结果write_ocr_class_mlp(OCRHandle,FontFile)*2.6清除句柄clear_ocr_class_mlp(OCRHandle)*3识别文字*3.1读入图像dev_close_window()read_image(Image,'F:/学习资料/Halcon工程/ocr-read.bmp')get_image_size(Image,Width,Height)dev_open_window(0,0,Width,Height,'black',WindowHandle)dev_display(Image)*3.2对齐文字text_line_orientation(Image,Image,25,rad(-45),rad(45),OrientationAngle)rotate_image(Image,ImageRotate,-OrientationAngle/rad(180)*180,'constant')*分割文字threshold(ImageRotate,TestWordsRegion,0,100)connection(TestWordsRegion,TestSingleWords)select_shape(TestSingleWords,SelectedRegions,'area','and',80,500)sort_region(SelectedRegions,TestWordsSortedRegions,'upper_left','true','column')count_obj(TestWordsSortedRegions,Number)read_ocr_class_mlp(FontFile,OCRHandle1)do_ocr_multi_class_mlp(TestWordsSortedRegions,ImageRotate,OCRHandle1,Class,Confidence)forIndex:=1toNumberby1*dev_display(ImageRotate)select_obj(TestWordsSortedRegions,ObjectSelected,Index)dev_display(ObjectSelected)disp_message(WindowHandle,Class[Index-1],'image',12,20*Index,'green','true')stop()endforclear_ocr_class_mlp(OCRHandle1)附:训练图像测试图像
本文标题:Halcon-OCR训练及识别
链接地址:https://www.777doc.com/doc-5740935 .html