*重要理解一下这个几个算子 * dev_update_off () * dev_update_on () * dev_update_pc ('off') * 计算区域的总数,使用count_obj()算子 * 选择访问第i个区域的,使用select_obj()算子 ****************第1步:定位矫正,分割,形成单个汉字区域,排序 *关闭窗口 dev_close_window () *开窗口 dev_open_window (0, 0, 512, 512, 'black', WindowHandle) *读取图像 read_image (Image, 'E:/BaiduNetdiskDownload/Halcon采用分类器实现OCR识别/汉字识别 .jpg') *灰度化 rgb1_to_gray (Image, GrayImage) *线性灰度变换 scale_image (GrayImage, ImageScaled, 1.34921, -89) *二值化 threshold (ImageScaled, Regions, 152, 226) *断开连通域 connection (Regions, ConnectedRegions) *特征选择,把想要的目标显示提取 select_shape (ConnectedRegions, SelectedRegions, ['area','row'], 'and', [0,243.86], [22589.8,599.24]) *联合成一个连通域 union1 (SelectedRegions, RegionUnion) *形状凸性变换 shape_trans (RegionUnion, RegionTrans, 'rectangle2') *求角度 orientation_region (RegionTrans, Phi) *求面积和行列坐标 area_center (RegionTrans, Area, Row, Column) *仿射变换 vector_angle_to_rigid (Row, Column, Phi, Row, Column, rad(0), HomMat2D) *作用到图像上 affine_trans_image (GrayImage, ImageAffinTrans, HomMat2D, 'constant', 'false') *作用到区域上 affine_trans_region (RegionTrans, RegionAffineTrans, HomMat2D, 'nearest_neighbor') *剪切 reduce_domain (ImageAffinTrans, RegionAffineTrans, ImageReduced) *灰度化 rgb1_to_gray (ImageReduced, GrayImage1) *二值化 threshold (GrayImage1, Regions1, 194, 230) *采用矩形进行闭运算 closing_rectangle1 (Regions1, RegionClosing, 7,7) *断开连通域 connection (RegionClosing, ConnectedRegions1) *闭运算 closing_circle (ConnectedRegions1, RegionClosing1,125.5) *联合连通域 union1 (RegionClosing1, RegionUnion1) *断开连通域 connection (RegionUnion1, ConnectedRegions2) *求出分割单个字体 intersection (ConnectedRegions2, RegionClosing, RegionIntersection) *计算有几个独立的区域 count_obj (RegionIntersection, Number) *排序 sort_region (RegionIntersection, SortedRegions, 'character', 'true', 'row') *验证排序是否正确 for Index := 1 to Number by 1 select_obj (SortedRegions, SingleWord, Index) endfor *************************第2步 形成训练文件 *创建trf训练文件,即将字符图像和字符关联起来 *如果这些字符图像在一幅图像中,一个一个添加用append_ocr_trainf, *可以直接用write_ocr_trainf把所有字符图像全部写进去 *形成汉字文件 words:=['数','字','图','像','处','理','与','机','器','视','觉'] *赋值路径 TrainFile:='E:/BaiduNetdiskDownload/Halcon采用分类器实现OCR识别/word.trf' *索引每一个文字,把 for Index := 1 to Number by 1 select_obj (SortedRegions, SingleWord, Index) append_ocr_trainf(SingleWord,ImageAffinTrans,words[Index-1],TrainFile) endfor FontFile:='E:/BaiduNetdiskDownload/Halcon采用分类器实现OCR识别/words.omc' *读取训练文件 read_ocr_trainf_names (TrainFile, CharacterNames, CharacterCount) NumHidden:=20 *创建模型,注意第五个参数为识别结果的所有类别 create_ocr_class_mlp (8, 10, 'constant', 'default',CharacterNames, 80, 'none', 10, 42, OCRHandle) *训练模型 trainf_ocr_class_mlp(OCRHandle,TrainFile,200,1,0.01,Error,ErrorLog) *保存训练好的模型,omc文件 write_ocr_class_mlp(OCRHandle,FontFile) *清除句柄 clear_ocr_class_mlp (OCRHandle) *****************第3步 读取显示 *读取训练好的模型 read_ocr_class_mlp ('E:/BaiduNetdiskDownload/Halcon采用分类器实现OCR识别/words.omc', OCRHandle1) *识别 *这里面可以用其他的文字信息 do_ocr_multi_class_mlp (SortedRegions, ImageReduced, OCRHandle1, Class, Confidence) *显示图像 dev_display (Image) *设置显示字体的大小,是否倾斜,是否有背景,什么颜色 set_display_font (WindowHandle, 26, 'mono', 'true', 'false') xinxi:='www.skcircle.com' disp_message (WindowHandle, xinxi, 'window', 400, 100, 'black', 'true') **********另外一种显示的方式,在C#中可以直接用,不用写disp_message的类 ** set_tposition (WindowHandle, 10, 10) write_string (WindowHandle, xinxi) *显示方法1 result:='' for Index1 := 0 to |Class|-1 by 1 result:=result+Class[Index1] disp_message (WindowHandle, result, 'window',30,50, 'red', 'true') endfor *显示方法2 for i := 1 to Number by 1 select_obj (SortedRegions, SingleWord, i) area_center (SingleWord, Area1, Row1, Column1) disp_message (WindowHandle, Class[i-1], 'Image', Row1+300, Column1, 'green', 'true') endfor
本文出自勇哥的网站《少有人走的路》wwww.skcircle.com,转载请注明出处!讨论可扫码加群:


