打开halcon,按下ctrl+e打开halcon自带例程。应用范围->光学字符识别->ocrcolor.hdev
*
* OCR (numbers) with color segmentation
*
read_image (Image, 'ocr/color_form_01')
get_image_pointer3 (Image, PointerRed, PointerGreen, PointerBlue, Type, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width, Height, 'black', WindowID)
dev_display (Image)
dev_set_line_width (3)
dev_set_draw ('margin')
dev_update_window ('off')
* Read the classifier to use for reading the text.
* It is easiest to use the pre-trained font Industrial_0-9_NoRej. If you
* have run the program ocrcolort.hdev in this directory, you can activate
* the second line to use the font trained with this program.
*读取halcon自带的模型文件
*read_ocr_class_mlp ('Industrial_0-9_NoRej', OCRHandle)
*读取前面训练得到的文件
read_ocr_class_mlp ('ocrcolor', OCRHandle)
*
* LOOP: Process all Images
*
NumImages := 8
for img := 1 to NumImages by 1
read_image (Image, 'ocr/color_form_0' + img)
*
* Detect foreground
*
*分割出文字区域
mean_image (Image, Mean, 3, 3)
decompose3 (Mean, Red, Green, Blue)
threshold (Green, ForegroundRaw, 0, 220)
clip_region (ForegroundRaw, Foreground, 3, 3, Height - 4, Width - 4)
*
* Divide colors
*
reduce_domain (Red, Foreground, RedReduced)
reduce_domain (Green, Foreground, GreenReduced)
sub_image (RedReduced, GreenReduced, ImageSub, 2, 128)
mean_image (ImageSub, ImageMean, 3, 3)
binary_threshold (ImageMean, Cluster1, 'smooth_histo', 'dark', UsedThreshold)
difference (Foreground, Cluster1, Cluster2)
concat_obj (Cluster1, Cluster2, Cluster)
opening_circle (Cluster, Opening, 2.5)
smallest_rectangle1 (Opening, Row1, Column1, Row2, Column2)
WidthCluster := Column2 - Column1 + 1
if (WidthCluster[0] > WidthCluster[1])
select_obj (Opening, NumberRegion, 2)
else
select_obj (Opening, NumberRegion, 1)
endif
*
* Expand Numbers
*
closing_rectangle1 (NumberRegion, NumberCand, 1, 20)
difference (Image, NumberCand, NoNumbers)
connection (NumberRegion, NumberParts)
intensity (NumberParts, Green, MeanIntensity, Deviation)
expand_gray_ref (NumberParts, Green, NoNumbers, Numbers, 20, 'image', MeanIntensity, 48)
union1 (Numbers, NumberRegion)
connection (NumberRegion, Numbers)
*
* Fine tuning
*
fill_up_shape (Numbers, RegionFillUp, 'area', 1, 100)
opening_circle (RegionFillUp, FinalNumbersUnsorted, 3.5)
sort_region (FinalNumbersUnsorted, FinalNumbers, 'character', 'true', 'row')
dev_set_color ('blue')
dev_display (Image)
dev_display (FinalNumbers)
count_obj (FinalNumbers, NumNumbers)
union1 (FinalNumbers, NumberRegion)
difference (Image, NumberRegion, NoNumbers)
paint_region (NoNumbers, Green, ImageOCRRaw, 255, 'fill')
paint_region (NumberRegion, ImageOCRRaw, ImageOCR, 0, 'fill')
*
* OCR
*
*识别文字信息
*第一个参数是数字的像素区域
*第二个参数输入是原图像
*第三个模型句柄,即前面训练出来的那个模型
*第五个输出的识别出来的数字
*第六个参数是识别信任度
do_ocr_multi_class_mlp (FinalNumbers, ImageOCR, OCRHandle, RecChar, Confidence)
set_display_font (WindowID, 27, 'mono', 'true', 'false')
disp_message (WindowID, sum(RecChar), 'window', 32, 24, 'blue', 'false')
if (img < NumImages)
set_display_font (WindowID, 16, 'mono', 'true', 'false')
disp_continue_message (WindowID, 'black', 'true')
stop ()
endif
endfor
dev_update_window ('on')
用于识别文字的原图像与识别信息
以上官方例程带有一些干扰信息,其实对于文字训练与识别是很简单的。简单分为以下几步:
训练
1,分割出文字对应图像区域,这里需要主注意文字模型是要黑色背景白色字体才能训练成功,如果是白色背景黑色像素区域训练会失败,具体原因暂不清楚。
2,用append_ocr_trainf ()函数把像素区域与对应的文字信息加到训练文件中,此时还没有开始训练文件,trf文件只是图像与文字的映射
3,create_ocr_class_mlp()函数创建训练模型
4,trainf_ocr_class_mlp()函数开始训练文件,得到omc文件,此文件即为我们需要的神经网络模型,可以存储起来
识别
1,分割出文字对应图像区域
2,读取训练好的omc文件
3,do_ocr_multi_class_mlp()或do_ocr_single_class_mlp()识别文字
下面是我自己写的一个简单训练识别中文的例子,文字是手写的
WindowHandle:=3600
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
gen_empty_obj (EmptyObject)
read_image (Image, 'C:/Users/Administrator/Desktop/2.PNG')
rgb1_to_gray (Image, GrayImage)
charcount:=6
for Index := 1 to charcount by 1
disp_message (WindowHandle, '请框选单个汉字区域,右键确认:','window', 12, 12, 'yellow', 'false')
**画个矩形
draw_rectangle1 (WindowHandle, Row1, Column1, Row2, Column2)
**根据画的矩形生成对应的矩形
gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2)
*裁出来
reduce_domain (GrayImage, Rectangle, ImageReduced1)
*阈值
threshold (ImageReduced1, Region1, 128, 255)
opening_circle (Region1, RegionOpening, 1.5)
*把区域加入到空变量中
concat_obj (EmptyObject, RegionOpening, EmptyObject)
endfor
*训练的图像对应的文字,注意顺序,这里从上到下,从左到右分别是苏林其,苏林其
words:=['苏','林','其','苏','林','其']
*区域排序,从左到右,从上到下
sort_region (EmptyObject, SortedRegions1, 'character', 'true', 'row')
for Index1:=1 to charcount by 1
select_obj (SortedRegions1, ObjectSelected1, Index1)
append_ocr_trainf (ObjectSelected1, Image, words[Index1-1], '2.trf')
endfor
read_ocr_trainf_names ('2.trf', CharacterNames, CharacterCount)
create_ocr_class_mlp (8, 10, 'constant', 'default', CharacterNames, 80, 'none', 10, 42, OCRHandle)
trainf_ocr_class_mlp (OCRHandle, '2.trf', 200, 1, 0.01, Error, ErrorLog)
write_ocr_class_mlp (OCRHandle, '2.omc')
*读取一张测试图
read_image (Image1, 'C:/Users/Administrator/Desktop/3.PNG')
*二值化
threshold (Image1, testwordregion, 200, 255)
*开始识别
read_ocr_class_mlp ('2.omc', OCRHandle1)
*识别单个
do_ocr_single_class_mlp (testwordregion, Image1, OCRHandle1,1, Class, Confidence)
*显示结果
disp_message(WindowHandle, '中文识别结果:', 'image', 50, 50, 'white', 'false')
disp_message(WindowHandle, Class, 'image', 50, 200, 'red', 'false')
训练原图像
识别结果