当前位置:   article > 正文

VS2017+OpenCV+Halcon实现包装袋日期识别(二)——OCR字符识别_mfc+halcon+opencv字符识别

mfc+halcon+opencv字符识别

本项目使用到Halcon的OCR识别,也可使用Opencv训练数据集来识别日期。使用Halcon主要是因为用OpenCV训练数据集较为麻烦(对小白不太友好),而Halcon本身便自带许多已训练好的数据集,并且使用Halcon的ocr助手也是相当容易的。

下面介绍如何使用Halcon的OCR助手

第一步:打开新的OCR

第二步 加载一个示例图像,再标记出需要识别的文本位置,右键确定,点击分割

第三步:调整参数,若是暗背景亮文本,记得勾选第一个方框

第四步: 选择“训练文件”,点击学习下方的输入框,根据右侧图片输入学习内容,接着点击“加入训练数据”。接下来,为保证训练数据集的准确性,可重复第一步操作,多次加入训练数据。最后,点击保存即可。

训练好的ocr文件可点击 可视化->工具->ocr训练文件浏览器 预览。当然,也可以直接使用第四步中 预训练分类器 的文件。

下面介绍本项目使用的ocr识别日期代码

 本项目参考Hanlcon的实例程序

下面是根据上面的实例程序所更改的代码

  1. dev_update_window ('off')
  2. //加载OpenCVc处理后的图像
  3. read_image (Image01, 'C:/Users/86133/source/repos/main/main/01.jpg')
  4. get_image_size (Image01, Width, Height)
  5. //预处理(也可在OpenCV中提前处理)
  6. invert_image (Image01, ImggrayInvert)
  7. threshold (Image01, Region, 130, 600)
  8. connection (Region, ConnectedRegions)
  9. select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 50, 500)
  10. sort_region (SelectedRegions, SortedRegions, 'first_point', 'true', 'column')
  11. shape_trans (SortedRegions, RegionTrans, 'rectangle1')
  12. area_center (RegionTrans, Area, Row, Column)
  13. MeanRow := mean(Row)
  14. count_obj (SortedRegions, Number)
  15. //OCR识别
  16. read_ocr_class_mlp ('C:/Users/86133/Desktop/学习资料/halcon/1.omc', OCRHandle)
  17. for I := 1 to Number by 1
  18. select_obj (SortedRegions, ObjectSelected, I)
  19. do_ocr_single_class_mlp (ObjectSelected, ImggrayInvert, OCRHandle, 1, Class, Confidence)
  20. dev_clear_window()
  21. dev_display (ObjectSelected)
  22. ocrNumbers[I-1] := Class
  23. //识别结果进行对比,判断日期是否正确,若equal==1,则正确
  24. Array1:=['2','0','2','1','/','0','5','/','2','3','/','A','-','Y']
  25. tuple_equal(Array1,ocrNumbers,Equal)
  26. endfor

最后,选择 文件->导出 ->c++->导出

 下面是我导出后删减的代码

  1. # include "HalconCpp.h"
  2. # include "HDevThread.h"
  3. using namespace HalconCpp;
  4. #ifndef NO_EXPORT_MAIN
  5. // Main procedure
  6. void action()
  7. {
  8. // Local iconic variables
  9. HObject ho_Image01, ho_ImggrayInvert, ho_Region;
  10. HObject ho_ConnectedRegions, ho_SelectedRegions, ho_SortedRegions;
  11. HObject ho_RegionTrans, ho_ObjectSelected;
  12. // Local control variables
  13. HTuple hv_Width, hv_Height, hv_Area, hv_Row;
  14. HTuple hv_Column, hv_MeanRow, hv_Number, hv_OCRHandle;
  15. HTuple hv_I, hv_Class, hv_Confidence, hv_ocrNumbers, hv_Array1;
  16. HTuple hv_Equal;
  17. // dev_update_window(...); only in hdevelop
  18. ReadImage(&ho_Image01, "C:/Users/86133/source/repos/main/main/01.jpg");
  19. GetImageSize(ho_Image01, &hv_Width, &hv_Height);
  20. InvertImage(ho_Image01, &ho_ImggrayInvert);
  21. Threshold(ho_Image01, &ho_Region, 130, 600);
  22. Connection(ho_Region, &ho_ConnectedRegions);
  23. SelectShape(ho_ConnectedRegions, &ho_SelectedRegions, "area", "and", 50, 500);
  24. SortRegion(ho_SelectedRegions, &ho_SortedRegions, "first_point", "true", "column");
  25. ShapeTrans(ho_SortedRegions, &ho_RegionTrans, "rectangle1");
  26. AreaCenter(ho_RegionTrans, &hv_Area, &hv_Row, &hv_Column);
  27. hv_MeanRow = hv_Row.TupleMean();
  28. CountObj(ho_SortedRegions, &hv_Number);
  29. ReadOcrClassMlp(//'C:/Users/86133/Desktop/学习资料/halcon/1.omc'
  30. "C:/Users/86133/Desktop/\321\247\317\260\327\312\301\317/halcon/1.omc",
  31. &hv_OCRHandle);
  32. {
  33. HTuple end_val15 = hv_Number;
  34. HTuple step_val15 = 1;
  35. for (hv_I=1; hv_I.Continue(end_val15, step_val15); hv_I += step_val15)
  36. {
  37. SelectObj(ho_SortedRegions, &ho_ObjectSelected, hv_I);
  38. DoOcrSingleClassMlp(ho_ObjectSelected, ho_ImggrayInvert, hv_OCRHandle, 1, &hv_Class,
  39. &hv_Confidence);
  40. if (HDevWindowStack::IsOpen())
  41. ClearWindow(HDevWindowStack::GetActive());
  42. if (HDevWindowStack::IsOpen())
  43. DispObj(ho_ObjectSelected, HDevWindowStack::GetActive());
  44. cout << (HString)hv_Class;//这里能打印出ocr识别内容,需要数据类型转换
  45. hv_ocrNumbers[hv_I-1] = hv_Class;
  46. hv_Array1.Clear();
  47. hv_Array1[0] = "2";
  48. hv_Array1[1] = "0";
  49. hv_Array1[2] = "2";
  50. hv_Array1[3] = "1";
  51. hv_Array1[4] = "/";
  52. hv_Array1[5] = "0";
  53. hv_Array1[6] = "5";
  54. hv_Array1[7] = "/";
  55. hv_Array1[8] = "2";
  56. hv_Array1[9] = "3";
  57. hv_Array1[10] = "/";
  58. hv_Array1[11] = "A";
  59. hv_Array1[12] = "-";
  60. hv_Array1[13] = "Y";
  61. TupleEqual(hv_Array1, hv_ocrNumbers, &hv_Equal);
  62. }
  63. if (hv_Equal == 1) {
  64. cout << "1" << endl;
  65. putText(img, "OK", Point(50, 50), FONT_HERSHEY_COMPLEX_SMALL, 2, Scalar(255, 69, 255), 2);
  66. }
  67. else if (hv_Equal == 0) {
  68. cout << "0" << endl;
  69. putText(img, "NO", Point(50, 50), FONT_HERSHEY_COMPLEX_SMALL, 2, Scalar(255, 69, 255), 2);
  70. }
  71. }
  72. }
  73. #ifndef NO_EXPORT_APP_MAIN
  74. int main(int argc, char *argv[])
  75. {
  76. int ret = 0;
  77. try
  78. {
  79. #if defined(_WIN32)
  80. SetSystem("use_window_thread", "true");
  81. #elif defined(__linux__) && (defined(__i386__) || defined(__x86_64__))
  82. XInitThreads();
  83. #endif
  84. // file was stored with local-8-bit encoding
  85. // -> set the interface encoding accordingly
  86. SetHcppInterfaceStringEncodingIsUtf8(false);
  87. // Default settings used in HDevelop (can be omitted)
  88. SetSystem("width", 512);
  89. SetSystem("height", 512);
  90. #ifndef __APPLE__
  91. action();
  92. #else
  93. ret = apple_main(argc,argv);
  94. #endif
  95. }
  96. catch (HException &exception)
  97. {
  98. fprintf(stderr," Error #%u in %s: %s\n", exception.ErrorCode(),
  99. (const char *)exception.ProcName(),
  100. (const char *)exception.ErrorMessage());
  101. ret = 1;
  102. }
  103. return ret;
  104. }
  105. #endif

        这一节内容其实完全可以用OpenCV来完成,但为何使用Halcon,前文已经说了。当然,这个项目只用Halcon做,或许会更简单,但毕竟Halcon并不像OpenCV一样开源,而且本人在机器视觉学习的主要是使用OpenCV。

        下一节,将介绍如何将Halcon导出的代码嵌入到OpenCV程序中。

下一篇链接:

VS2017+OpenCV+Halcon实现包装袋日期识别(三)——多平台联合_栖玖的博客-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/995591
推荐阅读
相关标签
  

闽ICP备14008679号