字符水平方向与垂直放投影的计算
图像水平方向矫正后,根据输入的字符图像,阈值分割,生成二值化图像,计算二值化图像在X、Y方向的投影图像
垂直投影: 统计每列的黑色点个数
水平投影: 统计每行的黑色点个数
Halcon示例代码如下:
根据字符图像,生成二值化图像
reduce_domain (ImageRotate, ROI_0, ImageReduced)
crop_domain (ImageReduced, ImagePart)
threshold (ImagePart, Regions1, 110, 170)
region_features (ROI_0, 'width', W)
region_features (ROI_0, 'height', H)
region_to_bin (Regions1, BinImage, 0, 255, W, H)
在二值化图像中遍历,统计水平方向与垂直方向黑色像素个数
tuple_gen_const (W, 0, CountV)
tuple_gen_const (H, 0, CountH)
dev_update_off ()
for X := 0 to W-1 by 1
for Y := 0 to H-1 by 1
get_grayval (BinImage, Y, X, Grayval)
if (Grayval==0)
CountV[X]:=CountV[X]+1
CountH[Y]:=CountH[Y]+1
endif
endfor
endfor
根据统计的个数,绘制投影图像
gen_image_const (Image1, 'byte', W, H)
gen_image_proto (Image1, ImageCleared, 255)
copy_image (ImageCleared, BinImageV)
copy_image (ImageCleared, BinImageH)
*垂直投影图像
for X := 0 to W-1 by 1
for Y := H-CountV[X] to H-1 by 1
set_grayval (BinImageV, Y, X, 0)
endfor
endfor
*水平投影图像
for Y := 0 to H-1 by 1
for X := 0 to CountH[Y]-1 by 1
set_grayval (BinImageH, Y, X, 0)
endfor
endfor
字符Q的投影图像:
字符0的投影图像:
得到投影图像后,可基于投影图像作Blob分析。
若获取区域的宽度、高度、面积、轮廓长度等属性
region_features (RegionQV, 'area', AreaQV)
region_features (RegionQV, 'width', WidthQV)
region_features (RegionQV, 'height', HeightQV)
region_features (RegionQV, 'contlength', LengthQV)