halcon光学字符识别(训练后识别),验证码识别

Posted 棉晗榜

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了halcon光学字符识别(训练后识别),验证码识别相关的知识,希望对你有一定的参考价值。





























识别效果




halcon光学字符识别

使用的halcon版本21.05
先训练模型,标记图片对应的字符,训练生成分类器

* 
* Training of the OCR
* The font is used in "bottle.hdev"
* 
* 
* Step 0: Preparations
 

* 
* Step 1: Segmentation
FontName:='yan_zheng_ma9988'

* 训练文件保存
TrainingFileName:='D:/'+FontName+'.trf'

* 分类器保存
FontFileOMC:='D:/'+FontName+'.omc'

dev_update_window ('off')
*训练
      
            read_image (Image, 'D:/Halcon测试图片/验证码图片1/3+11.jpg')
       

get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window (0, 0, 2 * Width, 2 * Height, 'black', WindowID)
set_display_font (WindowID, 27, 'mono', 'true', 'false')

 

*regiongrowing (ImageInvert, Regions, 2, 2, 1, 255)

* 筛选体积大的,数字越大体积越大,红色为选中
threshold (Image, RawSegmentation, 4, 200)


* 开口可以消除小区域(比圆形结构元件小)并平滑区域的边界。
opening_circle (RawSegmentation, circleRegion, 1)

*内部充满
fill_up (circleRegion, RegionFillUp)

*char_threshold (Image, Image, Characters, 3, 50, Threshold)

*根据形状特征填充孔洞
 *fill_up_shape(RawSegmentation, fillRegin, 'area', 5, 5)

*去掉小的模块
opening_rectangle1 (RegionFillUp, Cut,2,2)

*利用矩形结构元素执行开运算。矩形宽设为1,高为7,相当于低于7的连接被截断
*opening_rectangle1 (RegionFillUp, Cut,3, 3)

*分割粘连字符
*partition_rectangle (Cut, ConnectedPatterns, 22, 100)
partition_dynamic(Cut,parRegion,22,20)


*  与threshold后的图,求交集
intersection (parRegion, RawSegmentation, NumberCandidates)

opening_rectangle1 (NumberCandidates, Cut2,1, 2)

* 选择指定尺寸的块
select_shape (Cut2, Numbers, ['width','height'], 'and',[2, 1],[50,55])

sort_region (Numbers, FinalNumbers, 'first_point', 'true', 'column')
dev_display (Image)
dev_set_color ('green')
dev_set_line_width (2)
dev_set_shape ('rectangle1')
dev_set_draw ('margin')
dev_display (FinalNumbers)
* 
stop()

*----------------训练模型,开始-----------------------
TrainingNames := ['3','+','1','1' ,'=']
*TrainingFileName := FontName + '.trf'
sort_region (FinalNumbers, SortedRegions, 'first_point', 'true', 'column')
shape_trans (SortedRegions, RegionTrans, 'rectangle1')
area_center (RegionTrans, Area, Row, Column)
MeanRow := mean(Row)
dev_set_check ('~give_error')
*delete_file (TrainingFileName)
dev_set_check ('give_error')

*图片数字下标
K:=0
for I := 0 to |Column| - 1 by 1
    select_obj (SortedRegions, CharaterRegions, I+1)
     
    gen_empty_region (EmptyObject)     
    if(CharaterRegions!=EmptyObject)          
         append_ocr_trainf (CharaterRegions, Image, TrainingNames[K], TrainingFileName)
         disp_message (WindowID, TrainingNames[K], 'image', MeanRow+10, Column[I]-5, 'red', 'false') 
        K:=K+1
    endif
      
  
endfor


*字符去重复 uniq
CharNames := uniq(sort(TrainingNames))
create_ocr_class_mlp (10, 50, 'constant', 'default', CharNames, 10, 'none', 10, 42, OCRHandle)
trainf_ocr_class_mlp (OCRHandle, TrainingFileName, 200, 1, 0.01, Error, ErrorLog)

write_ocr_class_mlp (OCRHandle, FontFileOMC)
*----------------训练模型,结束-----------------------

halcon识别代码

* 
* Training of the OCR
* The font is used in "bottle.hdev"
* 
* 
* Step 0: Preparations
 

* 
* Step 1: Segmentation
FontName:='yan_zheng_ma9988'
TrainingFileName:='d:/'+FontName+'.trf'
FontFileOMC:='d:/'+FontName+'.omc'

dev_update_window ('off')
       
*  字符识别
      
     read_image (Image, 'd:/Halcon测试图片/验证码图片1/220_7-3.jpg')
    

get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window (0, 0, 2 * Width, 2 * Height, 'black', WindowID)

* 显示字体大小
set_display_font (WindowID, 30, 'mono', 'true', 'false')

 
   
*  灰度值处理
*gray_range_rect (Image, ImageResult, 3, 3)
*invert_image (ImageResult, ImageInvert)

*regiongrowing (ImageInvert, Regions, 2, 2, 1, 255)

* 筛选体积大的,数字越大体积越大,红色为选中
threshold (Image, RawSegmentation, 4, 200)
*char_threshold (Image, Image, Characters, 3, 50, Threshold)

* 开口可以消除小区域(比圆形结构元件小)并平滑区域的边界。
opening_circle (RawSegmentation, circleRegion, 1)

*内部充满
fill_up (circleRegion, RegionFillUp)

*char_threshold (Image, Image, Characters, 3, 50, Threshold)

*根据形状特征填充孔洞
 *fill_up_shape(RawSegmentation, fillRegin, 'area', 5, 5)

*去掉小的模块
opening_rectangle1 (RegionFillUp, Cut,2,2)

*利用矩形结构元素执行开运算。矩形宽设为1,高为7,相当于低于7的连接被截断
*opening_rectangle1 (RegionFillUp, Cut,3, 3)

*分割粘连字符
*partition_rectangle (Cut, ConnectedPatterns, 22, 100)
partition_dynamic(Cut,parRegion,22,20)


*  与threshold后的图,求交集
intersection (parRegion, RawSegmentation, NumberCandidates)

opening_rectangle1 (NumberCandidates, Cut2,1, 2)

* 选择指定尺寸的块
select_shape (Cut2, Numbers, ['width','height'], 'and',[2, 1],[50,55])

sort_region (Numbers, FinalNumbers, 'first_point', 'true', 'column')

dev_display (Image)
dev_set_color ('green')
dev_set_line_width (2)
dev_set_shape ('rectangle1')
dev_set_draw ('margin')
dev_display (FinalNumbers)
* 

* 读取字符,识别标记
shape_trans (FinalNumbers, RegionTrans, 'rectangle1')
dev_display (RegionTrans)
area_center (RegionTrans, Area, Row, Column)
MeanRow := mean(Row)

count_obj (FinalNumbers, Number)
 

*read_ocr_class_mlp (FontName, OCRHandle)
read_ocr_class_mlp (FontFileOMC, OCRHandle2)
for I := 1 to Number by 1
    select_obj (FinalNumbers, CharaterRegions,I)   
         
    gen_empty_region (EmptyObject) 
    if(CharaterRegions!=EmptyObject)
        * 识别时,必须与原图对比
       * do_ocr_single_class_mlp(CharaterRegions, Image, OCRHandle2,1, Class, Confidence)
       do_ocr_multi_class_mlp (CharaterRegions, Image, OCRHandle2, Class, Confidence)
         dev_disp_text (Class, 'image', MeanRow+10, Column[I - 1]-5, 'blue', 'box', 'false')
    endif
 
endfor



以上是关于halcon光学字符识别(训练后识别),验证码识别的主要内容,如果未能解决你的问题,请参考以下文章

halcon学习和实践(svm模型训练实战)

验证码识别图形验证码识别01

图片验证码识别

非黑即白--谷歌OCR光学字符识别

验证码识别大赛英文验证码识别

scikit-learn训练验证码识别模型