识别图中的数字和英文
套路思路:
1.读取图片
2.字符分割
3.字符识别
具体操作如下:
dev_close_window() read_image (Image2, 'D:/image1.jpg') get_image_size (Image2, Width, Height) dev_open_window(0, 0, Width/2, Height/2, 'black', WindowHandle) * 画区域 gen_rectangle1 (ROI_0, 175.442, 300.138, 670.685, 1149.25) * 获取图片中的点点点 dots_image (Image2, DotImage, 15, 'dark', 0) * 根据所画的区域获取方向 text_line_orientation (ROI_0, DotImage, 25, -0.523599, 0.523599, OrientationAngle) *平移 vector_angle_to_rigid (Width, Height, OrientationAngle, Width, Height, 0, HomMat2D) * 仿色变换 affine_trans_image (DotImage, ImageAffineTrans, HomMat2D, 'constant', 'false') * 预处理 invert_image (ImageAffineTrans, ImageInvert) reduce_domain (ImageInvert, ROI_0, ImageReduced) * 快速二值化,前提是只有两种颜色 binary_threshold (ImageInvert, Region, 'max_separability', 'dark', UsedThreshold) connection (Region, ConnectedRegions) select_shape (ConnectedRegions, SelectedRegions, ['row','column'], 'and', [281.15,150], [500,2000]) union1 (SelectedRegions, RegionUnion) * 膨胀 dilation_rectangle1 (RegionUnion, RegionDilation, 10, 10) connection (RegionDilation, ConnectedRegions1) shape_trans (ConnectedRegions1, RegionTrans, 'rectangle1') * 清楚冒号 select_shape (RegionTrans, SelectedRegions1, 'area', 'and', 2563.49, 20000) * 区域划分,通过大小进行切割 partition_rectangle (SelectedRegions1, Partitioned, 43, 80) * 区域一和区域二取交集 intersection (Partitioned, Region, RegionIntersection) read_ocr_class_mlp ('D:/halcon/ocr/DotPrint_0-9A-Z_NoRej.omc', OCRHandle) sort_region (RegionIntersection, SortedRegions, 'character', 'true', 'row') *4识别显示 do_ocr_multi_class_mlp(SortedRegions, ImageInvert, OCRHandle, Class, Confidence) count_obj(Partitioned,num) dev_display(ImageInvert) smallest_rectangle1 (SortedRegions, Row1, Column1, Row2, Column2) count_obj (SortedRegions, NumberFinal) for i := 1 to NumberFinal by 1 disp_message(WindowHandle, Class[i - 1], 'image', Row2[i - 1]+120, Column1[i - 1], 'red', 'false') endfor