*功能散点字符的OCR识别
dev_clear_window ()
dev_open_window (0, 0, 640, 400, 'black', WindowHandle)
*1.读取图像
read_image (Image, 'E:/03 CV(古)/Halcon/代码/超人启蒙_课题启蒙/OCR_SVM/图像/1.bmp')
get_image_size (Image, Width, Height)
*2.定位及仿射变换
Classes:=['2','0','1','6','0','6','1','6','6','F','1','6','C','1','8','3','8','C']
Row1:=64.3375
Column1:=165.313
Row2:=218.713
Column2:=618.988
Px:=Column1+(Column2-Column1)/2
Py:=Row1+(Row2-Row1)/2
gen_rectangle1 (Rectangle,Row1,Column1,Row2,Column2 )
dev_display (Image)
dev_display (Rectangle)
*仿射变换
text_line_orientation (Rectangle, Image, 25, -0.523599, 0.523599, OrientationAngle)#会根据gen_rectangle1 (Rectangle,,, )中的Rectangle自动获取旋转角度OrientationAngle
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_rotate (HomMat2DIdentity, -OrientationAngle, Px, Py, HomMat2DRotate)
affine_trans_image (Image, ImageAffineTrans, HomMat2DRotate, 'constant', 'false')
dev_display (Image)
dev_display (ImageAffineTrans)
*3.点状物提取(暗点)
dots_image (ImageAffineTrans, DotImage, 3, 'dark', 2)
gen_rectangle1 (ROI_0, 97.6825, 204.763, 205.128, 605.427)
reduce_domain (DotImage, ROI_0, ImageReduced)#裁剪(仿射变换后的图片和手动矩形绘制区域进行裁剪)
threshold (ImageReduced, Regions, 60, 255)#手动二值化(需要将阈值打√,通道改为1)
connection (Regions, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 0, 50)
union1 (SelectedRegions, RegionUnion)
*把每个数字中的数字散点连成一个完整闭合的连通域(膨胀方向上:纵向-->横向-->45°斜向-->145°斜向)
closing_rectangle1 (RegionUnion, RegionClosing1, 1, 5)
closing_rectangle1 (RegionClosing1, RegionClosing2, 5, 1)
*斜向膨胀:高设置为0
gen_rectangle2 (ROI_0, 16, 16, rad(45), 3, 0)
closing (RegionClosing2, ROI_0, RegionClosing3)
gen_rectangle2 (ROI_0, 2, 2, rad(145), 2, 0)
closing (RegionClosing3, ROI_0, RegionClosing4)
dilation_circle (RegionClosing4, RegionDilation, 1)
connection (RegionDilation, ConnectedRegions1)
select_shape (ConnectedRegions1, SelectedRegions1, 'area', 'and', 145.41, 433.49)#去掉冒号:
partition_dynamic (SelectedRegions1, Partitioned, 30, 20)#在垂直范围较小的位置水平地划分区域),防止腐蚀膨胀后有粘连的情况
sort_region (Partitioned, SortedRegions, 'character', 'true', 'row')#读取方式和方向
count_obj (SortedRegions, Number)
dev_display (ImageAffineTrans)
for i := 1 to Number by 1
dev_set_color ('red')
select_obj (SortedRegions, char, i)
dev_display (char)
intersection (char, Regions, RegionIntersection)#提取原图数字:提取散点图连通后的数字与原散点图做交集,获取公共部分
append_ocr_trainf (RegionIntersection, ImageAffineTrans, Classes[i-1], 'D:/svm.trf')
endfor