Index:.../Applications/OCR/ocr_yogurt.hdev
* 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_0-9A-Z.omc', OCRHandle)
* Approximate the character's dot size
DotDiameter := 6
里面字的dot差不多就是直径为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)
这里面把R通道图像做了变换,我感觉好像不做其实也没影响
* segment_characters expects black on white characters
invert_image (ImageScaleMax, ImageInvert)
segment_characters (ImageInvert, ImageInvert, ImageForeground, RegionForeground, 'local_contrast_best', 'false', 'true', 'light', CharWidth, CharHeight, 20, 50, UsedThreshold)
segment_characters 这个基本找到了字符, All segmentation methods assume that the text ist darker than the background. If this is not the case, please invert the image with invert_image. 说明这个算子使用前要把字变黑来找的。
* Merge dots
closing_circle (RegionForeground, RegionClosing, 4.0)
connection (RegionClosing, ConnectedRegions)
* We assume that characters are aligned along a line and
* the regions over or below this line are distractors
select_shape (ConnectedRegions, SelectedRegions, ['height','width','area'], 'and', [CharHeight - 20,CharWidth - 20,210], [CharHeight + 20,CharWidth + 20,CharHeight * CharHeight])
按照字符大小做第一次选择
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分成12bins然后histo就有12个,在这里面看分布情况,则5个字符是5,上面一个字符是1
*则5那个位置的row就是保质期所在的位置了
select_shape (SelectedRegions, Characters, 'row', 'and', CharRow - CharHeight / 2, CharRow + CharHeight / 2)
然后按照这个选择就可以每次选出来字符的位置
*
* Read out the characters on the yoghurt cover
sort_region (Characters, SortedRegions, 'character', 'true', 'row')
select_obj (SortedRegions, Chars, [1,2,3,4,5])
do_ocr_word_mlp (Chars, 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 (Chars)
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)