1 Halcon数据结构
2 一致性增强扩散
3 锯片检测
4 序列号读取
5 印刷检测
本次例程是读取CD盘上印刷的序列号。序列号印刷在圆环上,圆环与CD同圆心。
a. 使用极坐标变换将字符变换为标准位置
b. 将每个字符分割出来
c. 利用神经网络分类器识别出每个字符
*
*该程序演示了使用极坐标变换来校正CD上的序列号,之后使用OCR读取校正后的字符。
*
* 初始化
dev_update_off()
read_image(Image, 'cd_print')
dev_close_window()
dev_open_window_fit_image(Image, 0, 0, 640, 480, WindowHandle)
get_image_size(Image, Width, Height)
dev_set_draw('margin')
dev_set_line_width(2)
set_display_font(WindowHandle, 16, 'mono', 'true', 'false')
Pi := rad(180)
*
* 初始化一些变量
AnnulusInner := 0.90
AnnulusOuter := 0.99
CharWidthFraction := 0.01
*
* 提取外圆轮廓并拟合获取圆心坐标与半径
edges_sub_pix(Image, Edges, 'canny', 1, 20, 40)
select_shape_xld(Edges, ContourBorder, 'circularity', 'and', 0.9, 1)
fit_circle_contour_xld(ContourBorder, 'algebraic', -1, 0, 0, 3, 2, CenterRow, CenterColumn, Radius, StartPhi, EndPhi, PointOrder)
dev_display(Image)
dev_set_color('green')
dev_display(ContourBorder)
dev_disp_text('提取外圆轮廓并拟合获取圆心坐标与半径', 'window', 12, 12, 'black', [], [])
disp_continue_message (WindowHandle, 'black', 'true')
stop()
*
* 将带字符的环形空间转换为极坐标。
WidthPolar := 2 * Pi * Radius * AnnulusOuter
HeightPolar := Radius *(AnnulusOuter - AnnulusInner)
RadiusStart := Radius * AnnulusOuter
RadiusEnd := Radius * AnnulusInner
AngleStart := 2 * Pi
AngleEnd := 0
polar_trans_image_ext(Image, PolarTransImage, CenterRow, CenterColumn, AngleStart, AngleEnd, RadiusStart, RadiusEnd, WidthPolar, HeightPolar, 'bilinear')
dev_resize_window_fit_image(PolarTransImage, 0, 0, 1000, 1000)
dev_display(PolarTransImage)
stop()
*
* 分割字符
CharWidth := WidthPolar * CharWidthFraction
CharHeight := CharWidth
mean_image(PolarTransImage, ImageMean, 2 * CharWidth, 2 * CharHeight)
dyn_threshold(PolarTransImage, ImageMean, RegionThreshold, 10, 'dark')
connection(RegionThreshold, ConnectedRegions)
select_shape(ConnectedRegions, RegionChar, ['height','width','row'], 'and', [CharHeight * 0.1,CharWidth * 0.3,HeightPolar * 0.25], [CharHeight * 1.1,CharWidth * 1.1,HeightPolar * 0.75])
sort_region(RegionChar, RegionCharSort, 'character', 'true', 'row')
dev_set_colored(12)
dev_display(PolarTransImage)
dev_display(RegionCharSort)
stop()
*
* 读取字符
read_ocr_class_mlp('Industrial_Rej.omc', OCRHandle)
do_ocr_multi_class_mlp(RegionCharSort, PolarTransImage, OCRHandle, Class, Confidence)
* 将单个字符连接成一个字符串
SNString := sum(Class)
*
* 结果可视化
dev_resize_window_fit_image(Image, 0, 0, 640, 480)
set_display_font(WindowHandle, 16, 'mono', 'true', 'false')
dev_display(Image)
dev_disp_text('S/N: ' + SNString, 'window', 12, 12, 'black', [], [])
polar_trans_region_inv(RegionCharSort, XYTransRegion, CenterRow, CenterColumn, AngleStart, AngleEnd, RadiusStart, RadiusEnd, WidthPolar, HeightPolar, Width, Height, 'nearest_neighbor')
dev_set_colored(12)
dev_display(XYTransRegion)
*
* 清理分类器
clear_ocr_class_mlp(OCRHandle)
dev_disp_text(' 程序结束 ', 'window', 'bottom', 'right', 'black', [], [])
a. 使用极坐标变换将字符变换为标准位置
*
* 初始化一些变量
AnnulusInner := 0.90
AnnulusOuter := 0.99
CharWidthFraction := 0.01
*
* 提取外圆轮廓并拟合获取圆心坐标与半径
edges_sub_pix(Image, Edges, 'canny', 1, 20, 40)
select_shape_xld(Edges, ContourBorder, 'circularity', 'and', 0.9, 1)
fit_circle_contour_xld(ContourBorder, 'algebraic', -1, 0, 0, 3, 2, CenterRow, CenterColumn, Radius, StartPhi, EndPhi, PointOrder)
dev_display(Image)
dev_set_color('green')
dev_display(ContourBorder)
dev_disp_text('提取外圆轮廓并拟合获取圆心坐标与半径', 'window', 12, 12, 'black', [], [])
disp_continue_message (WindowHandle, 'black', 'true')
stop()
*
* 将带字符的环形空间转换为极坐标。
WidthPolar := 2 * Pi * Radius * AnnulusOuter
HeightPolar := Radius *(AnnulusOuter - AnnulusInner)
RadiusStart := Radius * AnnulusOuter
RadiusEnd := Radius * AnnulusInner
AngleStart := 2 * Pi
AngleEnd := 0
polar_trans_image_ext(Image, PolarTransImage, CenterRow, CenterColumn, AngleStart, AngleEnd, RadiusStart, RadiusEnd, WidthPolar, HeightPolar, 'bilinear')
dev_resize_window_fit_image(PolarTransImage, 0, 0, 1000, 1000)
dev_display(PolarTransImage)
stop()
代码如上,想对图像进行极坐标变换,首先需要知道圆环圆心和半径,也就是要提取出圆环,再拟合圆获取圆心和半径;想要获取圆环 可以通过图像分割或者直接提取轮廓;观察CD图像可知,图像亮度不均匀,垂直方向中部较亮,而水平中间部分较暗,想要获取圆环,图像分割的话可以做局部阈值分割;我修改了原程序,选择直接提取轮廓;之后通过极坐标变换得到常规图像。
b. 将每个字符分割出来
*
* 分割字符
CharWidth := WidthPolar * CharWidthFraction
CharHeight := CharWidth
mean_image(PolarTransImage, ImageMean, 2 * CharWidth, 2 * CharHeight)
dyn_threshold(PolarTransImage, ImageMean, RegionThreshold, 10, 'dark')
connection(RegionThreshold, ConnectedRegions)
select_shape(ConnectedRegions, RegionChar, ['height','width','row'], 'and', [CharHeight * 0.1,CharWidth * 0.3,HeightPolar * 0.25], [CharHeight * 1.1,CharWidth * 1.1,HeightPolar * 0.75])
sort_region(RegionChar, RegionCharSort, 'character', 'true', 'row')
dev_set_colored(12)
dev_display(PolarTransImage)
dev_display(RegionCharSort)
stop()
代码如上,观察CD图像可知,图像亮度不均匀,垂直方向中部较亮,而水平中间部分较暗,想要分割字符,可以做局部阈值分割。
c. 利用神经网络分类器识别出每个字符
*
* 读取字符
read_ocr_class_mlp('Industrial_Rej.omc', OCRHandle)
do_ocr_multi_class_mlp(RegionCharSort, PolarTransImage, OCRHandle, Class, Confidence)
* 将单个字符连接成一个字符串
SNString := sum(Class)
*
* 结果可视化
dev_resize_window_fit_image(Image, 0, 0, 640, 480)
set_display_font(WindowHandle, 16, 'mono', 'true', 'false')
dev_display(Image)
dev_disp_text('S/N: ' + SNString, 'window', 12, 12, 'black', [], [])
polar_trans_region_inv(RegionCharSort, XYTransRegion, CenterRow, CenterColumn, AngleStart, AngleEnd, RadiusStart, RadiusEnd, WidthPolar, HeightPolar, Width, Height, 'nearest_neighbor')
dev_set_colored(12)
dev_display(XYTransRegion)
本次序列号读取程序,演示了如何对非线性打印出的符号执行OCR。要点是提取轮廓和图像分割,能够使用极坐标变换算子和OCR算子,最好是能够理解算子原理。
《机器视觉算法与应用》