halcon示例學習之字元檢測
阿新 • • 發佈:2018-11-16
從這個影象中提取字元,首次檢視發現色差很小,不好處理,但是分析了例子處理方式,感覺很巧妙
- 是提取字元的方式,segment_character,這個運算元引數很多,但是有很好的處理結果
- 提取出字元之後去除雜點的方式,通過字元在一條水平線上把不在這一水平線上的給去除了。
* This example demonstrates how to segment the expiration date of * a yoghurt cap. After the segmentation, the characters are classified * by the mlp ocr classifier using the dotprint font. * * * Display initializations dev_update_off () dev_close_window () read_image (Image, 'ocr/yogurt_lid_01') dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle) set_display_font (WindowHandle, 16, 'mono', 'true', 'false') dev_set_draw ('margin') dev_set_colored (12) dev_set_line_width (2) * read_ocr_class_mlp ('DotPrint_NoRej', OCRHandle) * Approximate the character's dot size DotDiameter := 6 * Rough character size CharHeight := 60 CharWidth := 50 * Number of candidates returned by the classifier NCandidates := 3 for I := 1 to 4 by 1 read_image (Image, 'ocr/yogurt_lid_' + I$'02') * * Segmentation of characters on yoghurt cover * 提取三個通道 decompose3 (Image, ImageR, ImageG, ImageB) * diff_of_gauss() emphasizes structures having a characteristic dimension of size * DotDiameter (line thickness, maximum diameter, ...) diff_of_gauss (ImageR, DiffOfGauss, DotDiameter / 2, 2) *規一化 scale_image_max (DiffOfGauss, ImageScaleMax) * Merge dots gray_dilation_shape (ImageScaleMax, ImageMax, 3, 3, 'octagon') * Segment_characters expects black on white characters *反變一下,把字元變成黑色的 invert_image (ImageMax, ImageInvert) *從一個給定的區域裡面分割字元 *使用這個運算元有一個假設,就是字元要比背景黑,否則需要使用invert_image進行一下反色 segment_characters (ImageInvert, ImageInvert, ImageForeground, RegionForeground, 'local_contrast_best', 'false', 'true', 'medium', CharWidth, CharHeight, 20, 40, UsedThreshold) *連線上小斷點 closing_circle (RegionForeground, RegionClosing, 3) connection (RegionClosing, ConnectedRegions) select_shape (ConnectedRegions, SelectedRegions, 'height', 'and', 39, 65) * * We assume that characters are aligned along a line and * the regions over or below this line are distractors *假定字元水平對齊的,比這線上高的或者低的都是錯誤的選擇 * 這個方式可以學習一下 area_center (SelectedRegions, Area, Row, Column) get_image_size (Image, Width, Height) *計算陣列分佈情況 tuple_histo_range (Row, 0, Height, 2 * Height / CharHeight, Histo, BinSize) *找到數量最多的 tuple_find (Histo, max(Histo), IndMax) CharRow := BinSize * (IndMax[0] + 0.5) *選擇row在cahrRow 附近的,這樣就把左下角的去掉了 select_shape (SelectedRegions, Characters, 'row', 'and', CharRow - CharHeight / 2, CharRow + CharHeight / 2) * *上面識別出來位置了,然後下面就是排序一下,然後就是使用mlp識別了 * Read out the characters on the yoghurt cover sort_region (Characters, SortedRegions, 'character', 'true', 'row') do_ocr_word_mlp (SortedRegions, ImageForeground, OCRHandle, '\\d{4}[A-Z]', NCandidates, 2, Class, Confidence, Word, Score) * DateString := Word{0} + Word{1} + '.' + Word{2} + Word{3} + '.' BatchID := Word{4} * dev_display (Image) dev_display (SortedRegions) disp_message (WindowHandle, 'Best before: ' + DateString + '\nBatch ID : ' + BatchID, 'window', 12, 12, 'black', 'true') if (I < 4) disp_continue_message (WindowHandle, 'black', 'true') stop () endif endfor clear_ocr_class_mlp (OCRHandle)