转载自:http://blog.csdn.net/zhang2012liang/article/details/50993173
1 Halcon 例子 里面其实自带字符训练和识别
2 Halcon OCR训练分SVM和MLP两种
3 Halcon提供了一些识别模型,但是毕竟自己的项目跟他的不一样,所以需要自己训练自己的模型
4 下面先以SVM训练和识别开始(创建训练文件,训练,识别)
SVM训练和识别(训练自己的0-9和A-Z)
第一步:准备图片
每个字符对应一个文件夹,为了后期遍历文件夹方便,文件夹名字以字符直接命名,见下图。
第二步:创建训练文件
* 声明一个字符数组,并且将0-9和A-Z赋值此数组
- CharH := []
- for i := 0 to 9 by 1
- CharH[i] := chr(round(i + ord('0')))
- endfor
- for i := 10 to 36-1 by 1
- CharH[i] := chr(round(i-10 + ord('A')))
- endfor
- NumChar := |CharH|
CharH := []
for i := 0 to 9 by 1
CharH[i] := chr(round(i + ord('0')))
endfor
for i := 10 to 36-1 by 1
CharH[i] := chr(round(i-10 + ord('A')))
endfor
NumChar := |CharH|
*声明一个训练文件.trf
- TrainFile := 'ZHANG-Num0-9A-Z.trf'
- dev_set_check ('~give_error')
- delete_file (TrainFile)
- dev_set_check ('give_error')
TrainFile := 'ZHANG-Num0-9A-Z.trf'
dev_set_check ('~give_error')
delete_file (TrainFile)
dev_set_check ('give_error')
*遍历每个文件夹以及每个文件夹里面的字符图片,将每个文件夹与一个字符关联起来(这里每个文件夹里面的图片对应文件夹名“字符”)
- for Indexfile:=0 to |CharH| - 1 by 1
- list_files ('Z:\\00Trainlate\\TRIAN20150909\\02pictureTrain_2015-10-26_V1.0\\blackwitewordfirstsub\\checkimage\\test\\char\\'+CharH[Indexfile], ['files','follow_links'], ImageFiles)
- tuple_regexp_select (ImageFiles, ['\\.(bmp|jpg)$','ignore_case'], ImageFiles)
- for Index := 0 to |ImageFiles| - 1 by 1
- read_image (ImageSige, ImageFiles[Index])
- append_ocr_trainf(ImageSige,ImageSige,CharH[Indexfile],TrainFile)
- endfor
- endfor
for Indexfile:=0 to |CharH| - 1 by 1
list_files ('Z:\\00Trainlate\\TRIAN20150909\\02pictureTrain_2015-10-26_V1.0\\blackwitewordfirstsub\\checkimage\\test\\char\\'+CharH[Indexfile], ['files','follow_links'], ImageFiles)
tuple_regexp_select (ImageFiles, ['\\.(bmp|jpg)$','ignore_case'], ImageFiles)
for Index := 0 to |ImageFiles| - 1 by 1
read_image (ImageSige, ImageFiles[Index])
append_ocr_trainf(ImageSige,ImageSige,CharH[Indexfile],TrainFile)
endfor
endfor
第三步:训练文件(可以选择SVM训练或者MLP训练,根据自己选择的训练函数决定),获得最终模型文件.omc
- * ****
- * step: read training data
- * ****
- read_ocr_trainf_names (TrainFile, CharacterNames, CharacterCount)
- stop ()
- * ****
- * step: create and train classifier
- * ****
- create_ocr_class_svm (8, 10, 'constant', 'default', CharacterNames, 'rbf', 0.02, 0.001, 'one-versus-one', 'normalization', 0, OCRHandle)
- * Train the classifier
- trainf_ocr_class_svm (OCRHandle, TrainFile, 0.001, 'default')
- stop ()
- * ****
- * step: save classifier
- * ****
- FontFile := 'ZHANG-Num0-9A-Z_SVM.omc'
- write_ocr_class_svm(OCRHandle,FontFile)
- * free memory
- clear_ocr_class_svm (OCRHandle)
* ****
* step: read training data
* ****
read_ocr_trainf_names (TrainFile, CharacterNames, CharacterCount)
stop ()
* ****
* step: create and train classifier
* ****
create_ocr_class_svm (8, 10, 'constant', 'default', CharacterNames, 'rbf', 0.02, 0.001, 'one-versus-one', 'normalization', 0, OCRHandle)
* Train the classifier
trainf_ocr_class_svm (OCRHandle, TrainFile, 0.001, 'default')
stop ()
* ****
* step: save classifier
* ****
FontFile := 'ZHANG-Num0-9A-Z_SVM.omc'
write_ocr_class_svm(OCRHandle,FontFile)
* free memory
clear_ocr_class_svm (OCRHandle)
第四步 用自己训练的.omc 文件进行识别要识别的图片
- <pre name="code" class="cpp">* Read the SVM font file from file 读取刚刚自己创建的识别模型文件
- read_ocr_class_svm ('C:/Users/Public/Documents/MVTec/HALCON-11.0/examples/solution_guide/zhang/ZHANG-Num0-9A-Z_SVM.omc', OCRHandle)
- *读取待识别的图片
- read_image(ImageSige,'C:/Users/CQU/Desktop/QQ截图20160327192542.jpg')
- *有两个识别函数,他们之间的区别看帮助文档</span>
- do_ocr_single_class_svm(ImageSige, ImageSige, OCRHandle, 1, Class)
- * Clear the classifier from memory
- clear_ocr_class_svm (OCRHandle)
<pre name="code" class="cpp">* Read the SVM font file from file 读取刚刚自己创建的识别模型文件
read_ocr_class_svm ('C:/Users/Public/Documents/MVTec/HALCON-11.0/examples/solution_guide/zhang/ZHANG-Num0-9A-Z_SVM.omc', OCRHandle)
*读取待识别的图片
read_image(ImageSige,'C:/Users/CQU/Desktop/QQ截图20160327192542.jpg')
*有两个识别函数,他们之间的区别看帮助文档</span>
do_ocr_single_class_svm(ImageSige, ImageSige, OCRHandle, 1, Class)
* Clear the classifier from memory
clear_ocr_class_svm (OCRHandle)
第五步:检验无误就可以随意使用.omc 文件了
- <span style="font-family: Arial, Helvetica, sans-serif;">*MLP跟SVM一样,把对应的函数替换即可,具体教程看其提供的案例</span>