《zw版·Halcon-delphi系列原创教程》 只有2行代码的超市收款单ocr脚本

《zw版·Halcon-delphi系列原创教程》 只有2行代码的超市收款单ocr脚本
只有2行代码的超市收款单ocr脚本

      发了这么多教程,有网友问,为什么没有ocr的。
      的确,在opencv教程当中,秀ocr的、与车牌识别、人脸识别,差不多是三大主流demo。
      不过,Halcon内置的ocr模块非常彪悍,基本上,常规的工业、商业领域ocr,对于Halcon而言,已经成了标配,就象word的排版功能,实在太简单。

     下面示例的脚本,不到100行,主要是图像分割太繁琐

真正用于ocr的,就两行

 92 read_ocr_class_mlp ('DotPrint', OCRHandle)
 93 do_ocr_multi_class_mlp (FinalCharacters, Rotated, OCRHandle, Class, Confidence)

图1,是原图
图2,字符切割
图3,最终识别结果,方框下面的字符是识别结果


ps,有兴趣的网友,可以自己改为delphi、vc、vb版

  1 * dotprt.hdev: Segmentation of a dot printing
  2 * 
  3 dev_update_window ('off')
  4 read_image (Needle, 'needle1')
  5 dev_close_window ()
  6 get_image_size (Needle, Width, Height)
  7 dev_open_window (0, 0, 2 * Width, 2 * Height, 'black', WindowID)
  8 set_display_font (WindowID, 16, 'mono', 'true', 'false')
  9 dev_display (Needle)
 10 disp_continue_message (WindowID, 'black', 'true')
 11 stop ()
 12 * *
 13 Row1 := 50
 14 Column1 := 90
 15 Row2 := 250
 16 Column2 := 370
 17 Px := Column1 + (Column2 - Column1) / 2
 18 Py := Row1 + (Row2 - Row1) / 2
 19 gen_rectangle1 (Rectangle1, Row1, Column1, Row2, Column2)
 20 * orientation correction
 21 text_line_orientation (Needle, Needle, 35, -0.523599, 0.523599, OrientationAngle)
 22 hom_mat2d_identity (HomMat2DIdentity)
 23 hom_mat2d_rotate (HomMat2DIdentity, -OrientationAngle, Px, Py, HomMat2DRotate)
 24 affine_trans_image (Needle, Rotated, HomMat2DRotate, 'constant', 'false')
 25 dev_display (Rotated)
 26 disp_continue_message (WindowID, 'black', 'true')
 27 stop ()
 28 threshold (Rotated, RawSegmentation, 0, 105)
 29 connection (RawSegmentation, ConnectedRegions)
 30 select_shape (ConnectedRegions, MinSizeRegions, 'area', 'and', 6, 99999)
 31 union1 (MinSizeRegions, RemovedNoise)
 32 dev_display (Rotated)
 33 dev_set_color ('green')
 34 dev_set_draw ('fill')
 35 dev_display (RemovedNoise)
 36 disp_continue_message (WindowID, 'black', 'true')
 37 stop ()
 38 clip_region (RemovedNoise, RawSegmentation, 53, 75, 260, 356)
 39 dev_display (Rotated)
 40 dev_display (RawSegmentation)
 41 disp_continue_message (WindowID, 'black', 'true')
 42 stop ()
 43 closing_circle (RawSegmentation, ClosedPatterns, 6)
 44 opening_rectangle1 (ClosedPatterns, SplitPatterns, 1, 5)
 45 connection (SplitPatterns, ConnPatterns)
 46 select_shape (ConnPatterns, CharCandidates, 'area', 'and', 150, 5999)
 47 shape_trans (CharCandidates, CharBlocks, 'rectangle1')
 48 dev_set_draw ('margin')
 49 dev_set_line_width (2)
 50 dev_display (Rotated)
 51 dev_display (CharBlocks)
 52 disp_continue_message (WindowID, 'black', 'true')
 53 stop ()
 54 partition_rectangle (CharBlocks, CharCandidates, 25, 100)
 55 intersection (CharCandidates, RawSegmentation, Characters)
 56 closing_circle (Characters, IntermedCharacters, 2.5)
 57 dev_set_colored (12)
 58 dev_display (Rotated)
 59 dev_set_draw ('fill')
 60 dev_display (IntermedCharacters)
 61 disp_continue_message (WindowID, 'black', 'true')
 62 stop ()
 63 gen_empty_obj (Characters)
 64 count_obj (IntermedCharacters, NumIntermediate)
 65 dev_display (Rotated)
 66 for i := 1 to NumIntermediate by 1
 67 dev_set_color ('red')
 68 select_obj (IntermedCharacters, Char, i)
 69 dev_display (Char)
 70 connection (Char, CharParts)
 71 select_shape (CharParts, CharCandidates, 'area', 'and', 40, 99999)
 72 union1 (CharCandidates, Char)
 73 dev_set_color ('green')
 74 dev_display (Char)
 75 * disp_continue_message (WindowID, 'black', 'true')
 76 * stop ()
 77 concat_obj (Characters, Char, Characters)
 78 endfor
 79 select_shape (Characters, Heigh, 'height', 'and', 24, 50)
 80 sort_region (Heigh, FinalCharacters, 'character', 'true', 'row')
 81 dev_set_color ('red')
 82 dev_set_draw ('margin')
 83 dev_display (Rotated)
 84 dev_display (FinalCharacters)
 85 dev_set_color ('green')
 86 dev_set_line_width (3)
 87 dev_set_shape ('rectangle1')
 88 dev_display (FinalCharacters)
 89 dev_set_shape ('original')
 90 dev_set_line_width (1)
 91 dev_set_draw ('fill')
 92 read_ocr_class_mlp ('DotPrint', OCRHandle)
 93 do_ocr_multi_class_mlp (FinalCharacters, Rotated, OCRHandle, Class, Confidence)
 94 smallest_rectangle1 (FinalCharacters, Row11, Column1, Row2, Column21)
 95 count_obj (FinalCharacters, NumberFinal)
 96 for i := 1 to NumberFinal by 1
 97 disp_message (WindowID, Class[i - 1], 'image', Row2[i - 1], Column1[i - 1], 'green', 'false')
 98 endfor
 99 clear_ocr_class_mlp (OCRHandle)
100 dev_update_window ('on')


【《zw版·Halcon-delphi系列原创教程》,网址,cnblogs.com/ziwang/】

原文地址:https://www.cnblogs.com/ziwang/p/4856854.html