halcon学习笔记之(一)label_word_process_mlp.hdev
2013-05-27 08:29
246 查看
* This example shows how OCR results can be improved by restricting the * allowed results using a regular expression or a lexicon. Note, that the * classification results are artificially distorted for demonstration purposes. dev_update_pc ('off') dev_update_window ('off') dev_update_var ('off') dev_close_window () *图片的完整路径由环境变量决定,软件安装时,自动会添加该环境变量。 read_image (Image, 'label/label_01.png') get_image_size (Image, Width, Height) *新建一个背景为黑色的窗口 dev_open_window (0, 0, Width, Height, 'black', WindowHandle) set_display_font (WindowHandle, 20, 'mono', 'true', 'false') * Load pretrained font for character classification *可能是破解软件,Industrial.omc文件的路径虽没有环境变量,但仍然可以到%安装路径%OCR中找到 read_ocr_class_mlp ('Industrial', OCRHandle) * Create a lexicon of 3 allowed words *创建OCR用的词典lexicon或者正则表达式 create_lexicon ('label', ['BEST','BEFORE','END'], LexiconHandle) for i := 1 to 9 by 1 * Read image * +运算符连接字符串 read_image (Image, 'label/label_0' + i + '.png') * Find the label threshold (Image, Region, 128, 230) connection (Region, ConnectedRegions) select_shape (ConnectedRegions, LabelRaw, 'width', 'and', 350, 450) *半径为5的圆形结构元素进行开运算 opening_circle (LabelRaw, LabelOpen, 5) *根据区域的形状特性(区域的凸壳,外接椭圆,...等等)进行变换 shape_trans (LabelOpen, Label, 'rectangle2') * Perform rough deskew (of course, the text lines are not even parallel) text_line_orientation (Label, Image, 25, -0.523599, 0.523599, OrientationAngle) *创建单位阵 hom_mat2d_identity (HomMat2DIdentity) *创建旋转变换矩阵 hom_mat2d_rotate (HomMat2DIdentity, -OrientationAngle, 0, 0, Deskew) *根据旋转变换矩阵,对图像进行旋转, affine_trans_image (Image, ImageDeskew, Deskew, 'constant', 'false') *根据旋转变换矩阵,对区域进行旋转, affine_trans_region (Label, LabelDeskew, Deskew, 'false') smallest_rectangle1 (LabelDeskew, LabelTop, LabelLeft, LabelBottom, LabelRight) *提取ROI对应的图像区域,提取结果仍然与原图像尺寸相等 reduce_domain (ImageDeskew, LabelDeskew, ImageOCR) * Extract character regions var_threshold (ImageOCR, Foreground, 40, 40, 0.8, 10, 'dark') *对区域进行连通域分析 connection (Foreground, Blobs) *partition_dynamic起什么作用? partition_dynamic (Blobs, Split, 20, 40) select_shape (Split, Characters, ['width','height'], 'and', [10,20], [30,50]) select_shape (Characters, CharactersWords, ['row'], 'and', 0, LabelTop + 80) select_shape (Characters, CharactersDate, ['row'], 'and', LabelTop + 80, 600) * Artificially distort segmentation results for correction demonstration move_region (CharactersWords, CharactersWords, 0, 3) move_region (CharactersDate, CharactersDate, -2, 0) * Process Word Text *对连通区域按照某个特性进行升序或者降序排列 sort_region (CharactersWords, SortedWords, 'character', 'true', 'row') *求各个连通区域的面积和中心 area_center (SortedWords, Area, Row, Column) *|Column|求数组长度, :=赋值运算符, Column[|Column|]对Column数组中第|Column| *个元素(下标从0开始)赋值 Column[|Column|] := 9999 gen_empty_obj (Word) Text := '' OriginalText := '' *Column[|Column|] := 9999,导致Column数组的长度增加1,所以下面|Column| - 1 for j := 1 to |Column| - 1 by 1 *通过下标(下标从1开始)从object tuple中选取某个object select_obj (SortedWords, Character, j) concat_obj (Word, Character, Word) * Check character spacing to determine end of word if (j = |Column| or (Column[j] - Column[j-1]) > 30) * Classify the word without restriction (for comparison) *正则表达式'.*'表示重复0到任意次(*)的任意字符(.) do_ocr_word_mlp (Word, ImageOCR, OCRHandle, '.*', 1, 5, Class, Confidence, WordText, WordScore) OriginalText := OriginalText + ' ' + WordText * Classify the word using a lexicon *使用预先定义的lexicon词典 do_ocr_word_mlp (Word, ImageOCR, OCRHandle, '<label>', 1, 5, Class, Confidence, WordText, WordScore) Text := Text + ' ' + WordText gen_empty_obj (Word) endif endfor * Process Date Text sort_region (CharactersDate, SortedDate, 'character', 'true', 'row') * Classify the data string without restriction (for comparison) do_ocr_word_mlp (SortedDate, ImageOCR, OCRHandle, '.*', 5, 5, Class, Confidence, OriginalDateText, DateScore) * Classify the data string using a regular expression *使用正则表达式进行匹配 do_ocr_word_mlp (SortedDate, ImageOCR, OCRHandle, '^([0-2][0-9]|30|31)/(0[0-9]|11|12)/0[0-5]$', 5, 5, Class, Confidence, DateText, DateScore) * Visualization dev_clear_window () dev_display (ImageOCR) dev_set_colored (6) * dev_display (Characters) dev_display (CharactersWords) dev_display (CharactersDate) dev_set_color ('red') disp_message (WindowHandle, 'Distorted: ' + OriginalText + ' ' + OriginalDateText, 'image', 10, 10, 'red', 'false') disp_message (WindowHandle, 'Corrected:' + Text + ' ' + DateText, 'image', 40, 10, 'green', 'false') if (i < 9) set_display_font (WindowHandle, 14, 'mono', 'true', 'false') disp_continue_message (WindowHandle, 'black', 'true') set_display_font (WindowHandle, 20, 'mono', 'true', 'false') endif stop () endfor clear_lexicon (LexiconHandle) clear_ocr_class_mlp (OCRHandle)
相关文章推荐
- halcon例程学习笔记(7)---检测漏焊board.hdev
- halcon例程学习笔记(10)---图像灰度共生矩阵cooc_feature_image.hdev
- halcon例程学习笔记(2)----check_smd_tilt.hdev
- halcon例程学习笔记(10)---图像灰度共生矩阵cooc_feature_image.hdev
- halcon例程学习笔记(8)---瓶子编号识别bottle2.hdev
- nginx 源码学习笔记(十七)—— ngx_worker_process_cycle子进程执行
- cocos2d学习笔记(四)CCLabelTTF和CCLabelBMFont
- 深度学习(DL)与卷积神经网络(CNN)学习笔记随笔-04-基于Python的LeNet之MLP
- Hadoop 之 Wordcount 单词计数 (学习笔记)
- HADOOP的学习笔记 (第四期) eclipse 执行 wordcount
- nginx 源码学习笔记(十五)—— ngx_master_process_cycle 多进程(一)
- 深度学习(四十二)word2vec词向量学习笔记
- 模式识别(Pattern Recognition)学习笔记(十九)--多层感知器模型(MLP)
- [知了堂学习笔记]打印生成word文档(jq)
- C#学习笔记-调用HALCON代码
- python 下的 word2vec 学习笔记
- Oxford Deep NLP学习笔记1:Word Level Semantics
- Halcon 学习笔记3 仿射变换
- Hadoop学习笔记---1.wordcount程序的剖析
- Cocos2d-x学习笔记(十三)CCLabel详解