halcon 车牌识别

文章目录

  • 素材图片
  • 预处理操作
    • 读取图片
    • 阈值处理
    • 图片仿射
  • 识别预处理
  • 数字字母识别
  • 识别显示
  • 识别汉字
    • 预处理
  • 字符组装
    • 汉字组装
    • 连接所有字符
    • 训练文件
    • 图像识别
    • 全部代码

素材图片

halcon 车牌识别_第1张图片

预处理操作

读取图片

* 读取图片
dev_close_window()
read_image(Image,'C:/Users/Augustine/Desktop/picture/车牌.png')
dev_open_window_fit_image(Image, 0, 0, -1, -1, WindowHandle)
dev_display(Image)

阈值处理

* 灰度处理
rgb1_to_gray(Image, GrayImage)
* 阈值处理
threshold (GrayImage, Regions, 87, 244)
* 连通图打断
connection(Regions, ConnectedRegions)
* 特征处理
select_shape (ConnectedRegions, SelectedRegions, ['area','row','column'], 'and', [0,176.21,151.16], [3756.71,335.42,607.33])
* 开运算,去毛刺
opening_circle(SelectedRegions, RegionOpening, 1)

halcon 车牌识别_第2张图片

图片仿射

* 联合成一个区域
union1(RegionOpening, RegionUnion)
*获取向量矩形区域
shape_trans(RegionUnion, RegionTrans, 'rectangle2')
*1.获取中心点
area_center(RegionTrans, Area, Row, Column)
*2.获取当前矩阵的原始弧度
orientation_region(RegionTrans, Phi)
*3.生成仿射变换矩阵
vector_angle_to_rigid(Row, Column, Phi, Row, Column, rad(0), HomMat2D)
*4.转换斜切矩阵
hom_mat2d_slant(HomMat2D, rad(-5), 'x', Column, Row, HomMat2DSlant)
*5.仿射变换区域
affine_trans_region(RegionTrans, RegionAffineTrans, HomMat2DSlant, 'nearest_neighbor')
*6.仿射变换图片
affine_trans_image(Image,ImageAffineTrans, HomMat2DSlant, 'constant', 'false')
*抠图
reduce_domain(ImageAffineTrans, RegionAffineTrans, ImageReduced)

halcon 车牌识别_第3张图片

识别预处理

*识别预处理
rgb1_to_gray(ImageReduced, GrayImage1)
*阈值处理
threshold (GrayImage1, Regions1, 184, 243)
*腐蚀
erosion_rectangle1(Regions1, RegionErosion, 4, 3)
* 开运算去毛刺
opening_rectangle1(RegionErosion, RegionOpening1, 1,1)
* 连通图打断
connection(RegionOpening1, ConnectedRegions1)
*特征选择
select_shape (ConnectedRegions1, SelectedRegions1, 'area', 'and', 552.77, 2000.01)
*排序 根据列排序
sort_region(SelectedRegions1, SortedRegions, 'character', 'true', 'column')
select_obj(SortedRegions, ObjectSelected, 1)

halcon 车牌识别_第4张图片

数字字母识别

*识别
*颜色反转
invert_image(GrayImage1, ImageInvert)
*定义识别器
read_ocr_class_mlp('Industrial_0-9A-Z_Rej.omc', OCRHandle)
*识别
do_ocr_multi_class_mlp(SortedRegions, ImageInvert, OCRHandle, Class, Confidence)

halcon 车牌识别_第5张图片

识别显示

* 设置字体、设置颜色
set_display_font (WindowHandle, 50, 'mono', 'true', 'false')
dev_clear_window ()
dev_display (Image)
dev_set_color ('red')
***** 循环打印
for Index := 0 to |Class|-1 by 1
    set_tposition (WindowHandle,30, 120+40*Index)
    write_string (WindowHandle, Class[Index])
endfor

halcon 车牌识别_第6张图片

识别汉字

预处理

* 读取图片
dev_close_window()
read_image(Image,'C:/Users/Augustine/Desktop/picture/车牌.png')
dev_open_window_fit_image(Image, 0, 0, -1, -1, WindowHandle)
dev_display(Image)
* 灰度处理
rgb1_to_gray(Image, GrayImage)
* 阈值处理
threshold (GrayImage, Regions, 87, 244)
* 连通图打断
connection(Regions, ConnectedRegions)
* 特征处理
select_shape (ConnectedRegions, SelectedRegions, ['area','row','column'], 'and', [0,176.21,151.16], [3756.71,335.42,607.33])
* 开运算,去毛刺
opening_circle(SelectedRegions, RegionOpening, 1)

* 联合成一个区域
union1(RegionOpening, RegionUnion)
*获取向量矩形区域
shape_trans(RegionUnion, RegionTrans, 'rectangle2')
*1.获取中心点
area_center(RegionTrans, Area, Row, Column)
*2.获取当前矩阵的原始弧度
orientation_region(RegionTrans, Phi)
*3.生成仿射变换矩阵
vector_angle_to_rigid(Row, Column, Phi, Row, Column, rad(0), HomMat2D)
*4.转换斜切矩阵
hom_mat2d_slant(HomMat2D, rad(-5), 'x', Column, Row, HomMat2DSlant)
*5.仿射变换区域
affine_trans_region(RegionTrans, RegionAffineTrans, HomMat2DSlant, 'nearest_neighbor')
*6.仿射变换图片
affine_trans_image(Image,ImageAffineTrans, HomMat2DSlant, 'constant', 'false')
*抠图
reduce_domain(ImageAffineTrans, RegionAffineTrans, ImageReduced)

*识别预处理
rgb1_to_gray(ImageReduced, GrayImage1)
*阈值处理
threshold (GrayImage1, Regions1, 179, 245)
*空洞处理闭运算
closing_circle(Regions1, RegionClosing, 3.5)
*去除毛刺开运算
opening_circle(Regions1, RegionOpening1, 2)
*特征选择
connection(RegionOpening1, ConnectedRegions1)
select_shape (ConnectedRegions1, SelectedRegions1, ['area','row'], 'and', [0,180.23], [2884.62,275.94])
* 排序
sort_region(SelectedRegions1, SortedRegions, 'character', 'true', 'column')
select_obj(SortedRegions, ObjectSelected, 1)

halcon 车牌识别_第7张图片

字符组装

汉字组装

select_obj(SortedRegions, ObjectSelected1, 1)
select_obj(SortedRegions, ObjectSelected2, 2)
select_obj(SortedRegions, ObjectSelected3, 3)
select_obj(SortedRegions, ObjectSelected4, 4)
select_obj(SortedRegions, ObjectSelected5, 5)
select_obj(SortedRegions, ObjectSelected6, 6)
union2(ObjectSelected1, ObjectSelected2, RegionUnion1)
union2(RegionUnion1, ObjectSelected3, RegionUnion2)
union2(RegionUnion2, ObjectSelected4, RegionUnion3)
union2(RegionUnion3, ObjectSelected5, RegionUnion4)
union2(RegionUnion4, ObjectSelected6, RegionUnion5)

halcon 车牌识别_第8张图片

连接所有字符

* 把所有区域连接成一个对象
gen_empty_obj(NumberObject)
concat_obj(NumberObject,RegionUnion5,NumberObject)
concat_obj(NumberObject, ObjectSelected7, NumberObject)
concat_obj(NumberObject, ObjectSelected8, NumberObject)
concat_obj(NumberObject, ObjectSelected8, NumberObject)
concat_obj(NumberObject, ObjectSelected9, NumberObject)
concat_obj(NumberObject, ObjectSelected10, NumberObject)
concat_obj(NumberObject, ObjectSelected11, NumberObject)
concat_obj(NumberObject, ObjectSelected12, NumberObject)

halcon 车牌识别_第9张图片

训练文件

* 创建训练文件
TrainFile:='C:/Users/Augustine/Desktop/picture/车牌.trf'
Words:=['粤','B','7','0','l','4','6']
* 完成图像与字符训练对应关系
append_ocr_trainf(RegionUnion5,GrayImage1,'粤',TrainFile)
append_ocr_trainf(ObjectSelected7,GrayImage1,'B',TrainFile)
append_ocr_trainf(ObjectSelected8,GrayImage1,'7',TrainFile)
append_ocr_trainf(ObjectSelected9,GrayImage1,'0',TrainFile)
append_ocr_trainf(ObjectSelected10,GrayImage1,'l',TrainFile)
append_ocr_trainf(ObjectSelected11,GrayImage1,'4',TrainFile)
append_ocr_trainf(ObjectSelected12,GrayImage1,'6',TrainFile)

* 读取训练文件
read_ocr_trainf_names(TrainFile, CharacterNames, CharacterCount)

* 创建一个分类识别器

*create_ocr_class_mlp(WidthCharacter, HeightCharacter, // 识别字符宽度、高度
*                     Interpolation, // 插值算法
*                     Features, // 特征值 (区域特征、灰度值特征   包含曲度、紧密度、凸性等等)
*                     Characters, // 训练样本的名称
*                     NumHidden, // 隐藏层中的单元数
*                     Preprocessing, // 预处理
*                     NumComponents, // 特征变换的数量(特征未规划前的向量数一般为默认值)
*                     RandSeed, // 随机种子点数一般都写42 ,这个是神经网络迭代的过程
*                     OCRHandle) // 句柄

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)

* 保存分类识别文件
write_ocr_class_mlp(OCRHandle, 'C:/Users/Augustine/Desktop/picture/chepai.omc')

halcon 车牌识别_第10张图片

图像识别

*基于训练omc文件开始识别带中文车牌
*读取分类识别文件
dev_clear_window ()
dev_display (Image)
dev_set_color ('red')
set_display_font (WindowHandle, 30, 'mono', 'true', 'false')

read_ocr_class_mlp ('C:/Users/Augustine/Desktop/picture/chepai.omc', OCRHandle)


do_ocr_multi_class_mlp (RegionUnion5, GrayImage1, OCRHandle, Class, Confidence)
set_tposition (WindowHandle,30, 120+40)
write_string (WindowHandle, Class)
do_ocr_multi_class_mlp(ObjectSelected7,GrayImage1,OCRHandle,Class,Confidence)

set_tposition (WindowHandle,30, 120+40*2)
write_string (WindowHandle, Class)

do_ocr_multi_class_mlp(ObjectSelected8,GrayImage1,OCRHandle,Class,Confidence)
set_tposition (WindowHandle,30, 120+40*3)
write_string (WindowHandle, Class)

do_ocr_multi_class_mlp(ObjectSelected9,GrayImage1,OCRHandle,Class,Confidence)
set_tposition (WindowHandle,30, 120+40*4)
write_string (WindowHandle, Class)

do_ocr_multi_class_mlp(ObjectSelected10,GrayImage1,OCRHandle,Class,Confidence)
set_tposition (WindowHandle,30, 120+40*5)
write_string (WindowHandle, Class)

do_ocr_multi_class_mlp(ObjectSelected11,GrayImage1,OCRHandle,Class,Confidence)
set_tposition (WindowHandle,30, 120+40*6)
write_string (WindowHandle, Class)

do_ocr_multi_class_mlp(ObjectSelected12,GrayImage1,OCRHandle,Class,Confidence)
set_tposition (WindowHandle,30, 120+40*7)
write_string (WindowHandle, Class)

全部代码

* 读取图片
dev_close_window()
read_image(Image,'C:/Users/Augustine/Desktop/picture/车牌.png')
dev_open_window_fit_image(Image, 0, 0, -1, -1, WindowHandle)
dev_display(Image)
* 灰度处理
rgb1_to_gray(Image, GrayImage)
* 阈值处理
threshold (GrayImage, Regions, 87, 244)
* 连通图打断
connection(Regions, ConnectedRegions)
* 特征处理
select_shape (ConnectedRegions, SelectedRegions, ['area','row','column'], 'and', [0,176.21,151.16], [3756.71,335.42,607.33])
* 开运算,去毛刺
opening_circle(SelectedRegions, RegionOpening, 1)

* 联合成一个区域
union1(RegionOpening, RegionUnion)
*获取向量矩形区域
shape_trans(RegionUnion, RegionTrans, 'rectangle2')
*1.获取中心点
area_center(RegionTrans, Area, Row, Column)
*2.获取当前矩阵的原始弧度
orientation_region(RegionTrans, Phi)
*3.生成仿射变换矩阵
vector_angle_to_rigid(Row, Column, Phi, Row, Column, rad(0), HomMat2D)
*4.转换斜切矩阵
hom_mat2d_slant(HomMat2D, rad(-5), 'x', Column, Row, HomMat2DSlant)
*5.仿射变换区域
affine_trans_region(RegionTrans, RegionAffineTrans, HomMat2DSlant, 'nearest_neighbor')
*6.仿射变换图片
affine_trans_image(Image,ImageAffineTrans, HomMat2DSlant, 'constant', 'false')
*抠图
reduce_domain(ImageAffineTrans, RegionAffineTrans, ImageReduced)

*识别预处理
rgb1_to_gray(ImageReduced, GrayImage1)
*阈值处理
threshold (GrayImage1, Regions1, 179, 245)
*空洞处理闭运算
closing_circle(Regions1, RegionClosing, 3.5)
*去除毛刺开运算
opening_circle(Regions1, RegionOpening1, 2)
*特征选择
connection(RegionOpening1, ConnectedRegions1)
select_shape (ConnectedRegions1, SelectedRegions1, ['area','row'], 'and', [0,180.23], [2884.62,275.94])
* 排序
sort_region(SelectedRegions1, SortedRegions, 'character', 'true', 'column')

* 组装字体区域
* 汉字组装
select_obj(SortedRegions, ObjectSelected1, 1)
select_obj(SortedRegions, ObjectSelected2, 2)
select_obj(SortedRegions, ObjectSelected3, 3)
select_obj(SortedRegions, ObjectSelected4, 4)
select_obj(SortedRegions, ObjectSelected5, 5)
select_obj(SortedRegions, ObjectSelected6, 6)
union2(ObjectSelected1, ObjectSelected2, RegionUnion1)
union2(RegionUnion1, ObjectSelected3, RegionUnion2)
union2(RegionUnion2, ObjectSelected4, RegionUnion3)
union2(RegionUnion3, ObjectSelected5, RegionUnion4)
union2(RegionUnion4, ObjectSelected6, RegionUnion5)


select_obj(SortedRegions, ObjectSelected7, 7)
select_obj(SortedRegions, ObjectSelected8, 8)
select_obj(SortedRegions, ObjectSelected9, 9)
select_obj(SortedRegions, ObjectSelected10, 10)
select_obj(SortedRegions, ObjectSelected11, 11)
select_obj(SortedRegions, ObjectSelected12, 12)

* 把所有区域连接成一个对象
gen_empty_obj(NumberObject)
concat_obj(NumberObject,RegionUnion5,NumberObject)
concat_obj(NumberObject, ObjectSelected7,NumberObject)
concat_obj(NumberObject, ObjectSelected8,NumberObject)
concat_obj(NumberObject, ObjectSelected8,NumberObject)
concat_obj(NumberObject, ObjectSelected9,NumberObject)
concat_obj(NumberObject, ObjectSelected10,NumberObject)
concat_obj(NumberObject, ObjectSelected11,NumberObject)
concat_obj(NumberObject, ObjectSelected12,NumberObject1)

* 创建训练文件
TrainFile:='C:/Users/Augustine/Desktop/picture/车牌.trf'
Words:=['粤','B','7','0','l','4','6']
* 完成图像与字符训练对应关系
append_ocr_trainf(RegionUnion5,GrayImage1,'粤',TrainFile)
append_ocr_trainf(ObjectSelected7,GrayImage1,'B',TrainFile)
append_ocr_trainf(ObjectSelected8,GrayImage1,'7',TrainFile)
append_ocr_trainf(ObjectSelected9,GrayImage1,'0',TrainFile)
append_ocr_trainf(ObjectSelected10,GrayImage1,'l',TrainFile)
append_ocr_trainf(ObjectSelected11,GrayImage1,'4',TrainFile)
append_ocr_trainf(ObjectSelected12,GrayImage1,'6',TrainFile)


* 读取训练文件
read_ocr_trainf_names(TrainFile, CharacterNames, CharacterCount)

* 创建一个分类识别器

*create_ocr_class_mlp(WidthCharacter, HeightCharacter, // 识别字符宽度、高度
*                     Interpolation, // 插值算法
*                     Features, // 特征值 (区域特征、灰度值特征   包含曲度、紧密度、凸性等等)
*                     Characters, // 训练样本的名称
*                     NumHidden, // 隐藏层中的单元数
*                     Preprocessing, // 预处理
*                     NumComponents, // 特征变换的数量(特征未规划前的向量数一般为默认值)
*                     RandSeed, // 随机种子点数一般都写42 ,这个是神经网络迭代的过程
*                     OCRHandle) // 句柄

create_ocr_class_mlp(10, 10, 'constant', 'default', CharacterNames, 80, 'none', 10, 42, OCRHandle)

* 创建一个分类识别器
trainf_ocr_class_mlp (OCRHandle, TrainFile, 200, 1, 0.01, Error, ErrorLog)

* 保存分类识别文件
write_ocr_class_mlp(OCRHandle, 'C:/Users/Augustine/Desktop/picture/chepai.omc')

stop()
*基于训练omc文件开始识别带中文车牌
*读取分类识别文件
dev_clear_window ()
dev_display (Image)
dev_set_color ('red')
set_display_font (WindowHandle, 30, 'mono', 'true', 'false')

read_ocr_class_mlp ('C:/Users/Augustine/Desktop/picture/chepai.omc', OCRHandle)


do_ocr_multi_class_mlp (RegionUnion5, GrayImage1, OCRHandle, Class, Confidence)
set_tposition (WindowHandle,30, 120+40)
write_string (WindowHandle, Class)
do_ocr_multi_class_mlp(ObjectSelected7,GrayImage1,OCRHandle,Class,Confidence)

set_tposition (WindowHandle,30, 120+40*2)
write_string (WindowHandle, Class)

do_ocr_multi_class_mlp(ObjectSelected8,GrayImage1,OCRHandle,Class,Confidence)
set_tposition (WindowHandle,30, 120+40*3)
write_string (WindowHandle, Class)

do_ocr_multi_class_mlp(ObjectSelected9,GrayImage1,OCRHandle,Class,Confidence)
set_tposition (WindowHandle,30, 120+40*4)
write_string (WindowHandle, Class)

do_ocr_multi_class_mlp(ObjectSelected10,GrayImage1,OCRHandle,Class,Confidence)
set_tposition (WindowHandle,30, 120+40*5)
write_string (WindowHandle, Class)

do_ocr_multi_class_mlp(ObjectSelected11,GrayImage1,OCRHandle,Class,Confidence)
set_tposition (WindowHandle,30, 120+40*6)
write_string (WindowHandle, Class)

do_ocr_multi_class_mlp(ObjectSelected12,GrayImage1,OCRHandle,Class,Confidence)
set_tposition (WindowHandle,30, 120+40*7)
write_string (WindowHandle, Class)


*dev_clear_window ()
*dev_display (Image)
*dev_set_color ('red')
*set_display_font (WindowHandle, 30, 'mono', 'true', 'false')
*for Index := 0 to |Class|-1 by 1
*    set_tposition (WindowHandle,30, 120+36*Index)
*    write_string (WindowHandle, Class[Index])
*endfor

halcon 车牌识别_第11张图片

你可能感兴趣的:(计算机视觉)