赞
踩
本项目使用到Halcon的OCR识别,也可使用Opencv训练数据集来识别日期。使用Halcon主要是因为用OpenCV训练数据集较为麻烦(对小白不太友好),而Halcon本身便自带许多已训练好的数据集,并且使用Halcon的ocr助手也是相当容易的。
下面介绍如何使用Halcon的OCR助手
第一步:打开新的OCR
第二步 加载一个示例图像,再标记出需要识别的文本位置,右键确定,点击分割
第三步:调整参数,若是暗背景亮文本,记得勾选第一个方框
第四步: 选择“训练文件”,点击学习下方的输入框,根据右侧图片输入学习内容,接着点击“加入训练数据”。接下来,为保证训练数据集的准确性,可重复第一步操作,多次加入训练数据。最后,点击保存即可。
训练好的ocr文件可点击 可视化->工具->ocr训练文件浏览器 预览。当然,也可以直接使用第四步中 预训练分类器 的文件。
下面介绍本项目使用的ocr识别日期代码
本项目参考Hanlcon的实例程序
下面是根据上面的实例程序所更改的代码
- dev_update_window ('off')
- //加载OpenCVc处理后的图像
- read_image (Image01, 'C:/Users/86133/source/repos/main/main/01.jpg')
- get_image_size (Image01, Width, Height)
-
- //预处理(也可在OpenCV中提前处理)
- invert_image (Image01, ImggrayInvert)
- threshold (Image01, Region, 130, 600)
- connection (Region, ConnectedRegions)
- select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 50, 500)
- sort_region (SelectedRegions, SortedRegions, 'first_point', 'true', 'column')
- shape_trans (SortedRegions, RegionTrans, 'rectangle1')
- area_center (RegionTrans, Area, Row, Column)
- MeanRow := mean(Row)
- count_obj (SortedRegions, Number)
-
- //OCR识别
- read_ocr_class_mlp ('C:/Users/86133/Desktop/学习资料/halcon/1.omc', OCRHandle)
- for I := 1 to Number by 1
- select_obj (SortedRegions, ObjectSelected, I)
- do_ocr_single_class_mlp (ObjectSelected, ImggrayInvert, OCRHandle, 1, Class, Confidence)
- dev_clear_window()
- dev_display (ObjectSelected)
-
- ocrNumbers[I-1] := Class
-
- //识别结果进行对比,判断日期是否正确,若equal==1,则正确
- Array1:=['2','0','2','1','/','0','5','/','2','3','/','A','-','Y']
- tuple_equal(Array1,ocrNumbers,Equal)
-
- endfor
最后,选择 文件->导出 ->c++->导出
下面是我导出后删减的代码
- # include "HalconCpp.h"
- # include "HDevThread.h"
-
- using namespace HalconCpp;
-
-
- #ifndef NO_EXPORT_MAIN
- // Main procedure
- void action()
- {
-
- // Local iconic variables
- HObject ho_Image01, ho_ImggrayInvert, ho_Region;
- HObject ho_ConnectedRegions, ho_SelectedRegions, ho_SortedRegions;
- HObject ho_RegionTrans, ho_ObjectSelected;
-
- // Local control variables
- HTuple hv_Width, hv_Height, hv_Area, hv_Row;
- HTuple hv_Column, hv_MeanRow, hv_Number, hv_OCRHandle;
- HTuple hv_I, hv_Class, hv_Confidence, hv_ocrNumbers, hv_Array1;
- HTuple hv_Equal;
-
- // dev_update_window(...); only in hdevelop
- ReadImage(&ho_Image01, "C:/Users/86133/source/repos/main/main/01.jpg");
- GetImageSize(ho_Image01, &hv_Width, &hv_Height);
-
- InvertImage(ho_Image01, &ho_ImggrayInvert);
- Threshold(ho_Image01, &ho_Region, 130, 600);
- Connection(ho_Region, &ho_ConnectedRegions);
- SelectShape(ho_ConnectedRegions, &ho_SelectedRegions, "area", "and", 50, 500);
- SortRegion(ho_SelectedRegions, &ho_SortedRegions, "first_point", "true", "column");
-
- ShapeTrans(ho_SortedRegions, &ho_RegionTrans, "rectangle1");
- AreaCenter(ho_RegionTrans, &hv_Area, &hv_Row, &hv_Column);
- hv_MeanRow = hv_Row.TupleMean();
- CountObj(ho_SortedRegions, &hv_Number);
- ReadOcrClassMlp(//'C:/Users/86133/Desktop/学习资料/halcon/1.omc'
- "C:/Users/86133/Desktop/\321\247\317\260\327\312\301\317/halcon/1.omc",
- &hv_OCRHandle);
- {
- HTuple end_val15 = hv_Number;
- HTuple step_val15 = 1;
- for (hv_I=1; hv_I.Continue(end_val15, step_val15); hv_I += step_val15)
- {
- SelectObj(ho_SortedRegions, &ho_ObjectSelected, hv_I);
- DoOcrSingleClassMlp(ho_ObjectSelected, ho_ImggrayInvert, hv_OCRHandle, 1, &hv_Class,
- &hv_Confidence);
- if (HDevWindowStack::IsOpen())
- ClearWindow(HDevWindowStack::GetActive());
- if (HDevWindowStack::IsOpen())
- DispObj(ho_ObjectSelected, HDevWindowStack::GetActive());
- cout << (HString)hv_Class;//这里能打印出ocr识别内容,需要数据类型转换
- hv_ocrNumbers[hv_I-1] = hv_Class;
-
- hv_Array1.Clear();
- hv_Array1[0] = "2";
- hv_Array1[1] = "0";
- hv_Array1[2] = "2";
- hv_Array1[3] = "1";
- hv_Array1[4] = "/";
- hv_Array1[5] = "0";
- hv_Array1[6] = "5";
- hv_Array1[7] = "/";
- hv_Array1[8] = "2";
- hv_Array1[9] = "3";
- hv_Array1[10] = "/";
- hv_Array1[11] = "A";
- hv_Array1[12] = "-";
- hv_Array1[13] = "Y";
- TupleEqual(hv_Array1, hv_ocrNumbers, &hv_Equal);
-
- }
- if (hv_Equal == 1) {
- cout << "1" << endl;
- putText(img, "OK", Point(50, 50), FONT_HERSHEY_COMPLEX_SMALL, 2, Scalar(255, 69, 255), 2);
- }
- else if (hv_Equal == 0) {
- cout << "0" << endl;
- putText(img, "NO", Point(50, 50), FONT_HERSHEY_COMPLEX_SMALL, 2, Scalar(255, 69, 255), 2);
- }
- }
- }
-
-
- #ifndef NO_EXPORT_APP_MAIN
-
-
- int main(int argc, char *argv[])
- {
- int ret = 0;
-
- try
- {
- #if defined(_WIN32)
- SetSystem("use_window_thread", "true");
- #elif defined(__linux__) && (defined(__i386__) || defined(__x86_64__))
- XInitThreads();
- #endif
-
- // file was stored with local-8-bit encoding
- // -> set the interface encoding accordingly
- SetHcppInterfaceStringEncodingIsUtf8(false);
-
- // Default settings used in HDevelop (can be omitted)
- SetSystem("width", 512);
- SetSystem("height", 512);
-
- #ifndef __APPLE__
- action();
- #else
- ret = apple_main(argc,argv);
- #endif
- }
- catch (HException &exception)
- {
- fprintf(stderr," Error #%u in %s: %s\n", exception.ErrorCode(),
- (const char *)exception.ProcName(),
- (const char *)exception.ErrorMessage());
- ret = 1;
- }
- return ret;
- }
-
- #endif
-
-
-
-
这一节内容其实完全可以用OpenCV来完成,但为何使用Halcon,前文已经说了。当然,这个项目只用Halcon做,或许会更简单,但毕竟Halcon并不像OpenCV一样开源,而且本人在机器视觉学习的主要是使用OpenCV。
下一节,将介绍如何将Halcon导出的代码嵌入到OpenCV程序中。
下一篇链接:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。