读取图片
*读取图片
read_image(Image,'C:/Users/Augustine/Desktop/halcon/ring.png')
dev_close_window()
dev_open_window(0, 0, 512, 512, 'black', WindowHandle)
dev_display(Image)
rgb1_to_gray(Image, GrayImage)
将圆环拉直
- 先获取整个大(外)圆
- 在获取小(内)圆
- 测量内外圆的半径
- 算出 OuterRadius-40,InnerRadius+20(外圆半径距离字的距离,内圆半径距离字的距离),转换成极坐标
*提取内外圆的半径
*缩放图像的灰度值,以便更好的阈值处理
scale_image (GrayImage, ImageScaled, 19.6154, -2864)
*阈值处理
threshold (ImageScaled, Regions, 0, 11)
*首先获取外圆
shape_trans(Regions, outerCircle, 'outer_circle')
*在获取内接圆:用外接圆的补集(阈值处理的补集)+形状选择
*补集
complement(Regions, RegionComplement)
*连接成独立的个体
connection(RegionComplement, ConnectedRegions)
*特征提取:选择形状获取内接圆
select_shape (ConnectedRegions, SelectedRegions, 'area', 'or', 94086, 157168)
*最小包围圆:外圆
smallest_circle(Regions, OuterRow, OuterColumn, OuterRadius)
*内圆
smallest_circle(SelectedRegions, InnerRow, InnerColumn, InnerRadius)
*极坐标转换
polar_trans_image_ext(Image,PolarTransImage,OuterRow,OuterColumn,rad(360),0,OuterRadius-40,InnerRadius+20,512, 512, 'bilinear')
获取字符
将阈值处理的字符与膨胀之后的字符取交集获取字符
*进行识别预处理以及字符分割形成一个连通域
invert_image(PolarTransImage, ImageInvert)
*缩放图像的灰度值,以便更好的阈值处理
scale_image (ImageInvert, ImageScaled1, 6.71053, -1456)
*快速二值化,提取亮的地方
binary_threshold(ImageScaled1, Region, 'max_separability', 'light', UsedThreshold)
*将每个图像变成单独的连接区域
connection(Region, ConnectedRegions1)
*特征提取
select_shape (ConnectedRegions1, SelectedRegions1, 'area', 'or', 0, 1915.77)
*开运算:先腐蚀再膨胀去掉噪点
opening_rectangle1(SelectedRegions1, RegionOpening, 1,5)
*膨胀运算将字符连接起来,和二值化的字符取交集
dilation_rectangle1(RegionOpening, RegionDilation, 5, 5)
*联合拼接字符
union1(RegionDilation, RegionUnion)
*连接成一个单独的个体
connection(RegionUnion, ConnectedRegions2)
dev_clear_window()
dev_display(ConnectedRegions2)
*求交集提取字符
intersection(ConnectedRegions2, Region, RegionIntersection)
识别字符
*识别字符
*排序
sort_region(RegionIntersection, SortedRegions, 'character', 'true', 'column')
*读取识别的模板
read_ocr_class_mlp('Industrial_0-9A-Z_Rej.omc', OCRHandle)
*识别
do_ocr_multi_class_mlp(SortedRegions, PolarTransImage, OCRHandle, Class, Confidence)
*显示
*求连通域个数
count_obj(SortedRegions,num)
*求最小外接矩形
smallest_rectangle1(SortedRegions, Row1, Column1, Row2, Column2)
set_display_font (WindowHandle, 50, 'mono', 'true', 'false')
dev_clear_window ()
dev_display (Image)
dev_set_color ('red')
for i:=0 to 14 by 1
set_tposition (WindowHandle,30, 10+40*i)
write_string (WindowHandle, Class[i])
endfor
识别不出来的原因
处理的数值没有选好
全部代码
*读取图片
read_image(Image,'C:/Users/Augustine/Desktop/halcon/ring.png')
dev_close_window()
dev_open_window(0, 0, 512, 512, 'black', WindowHandle)
dev_display(Image)
rgb1_to_gray(Image, GrayImage)
*提取内外圆的半径
*缩放图像的灰度值,以便更好的阈值处理
scale_image (GrayImage, ImageScaled, 19.6154, -2864)
*阈值处理
threshold (ImageScaled, Regions, 0, 11)
*首先获取外圆
shape_trans(Regions, outerCircle, 'outer_circle')
*在获取内接圆:用外接圆的补集(阈值处理的补集)+形状选择
*补集
complement(Regions, RegionComplement)
*连接成独立的个体
connection(RegionComplement, ConnectedRegions)
*特征提取:选择形状获取内接圆
select_shape (ConnectedRegions, SelectedRegions, 'area', 'or', 94086, 157168)
*最小包围圆:外圆
smallest_circle(Regions, OuterRow, OuterColumn, OuterRadius)
*内圆
smallest_circle(SelectedRegions, InnerRow, InnerColumn, InnerRadius)
*极坐标转换
polar_trans_image_ext(Image,PolarTransImage,OuterRow,OuterColumn,rad(360),0,OuterRadius-40,InnerRadius+20,1024, 512, 'bilinear')
*进行识别预处理以及字符分割形成一个连通域
invert_image(PolarTransImage, ImageInvert)
*缩放图像的灰度值,以便更好的阈值处理
scale_image (ImageInvert, ImageScaled1, 5.42553, -1129)
*快速二值化,提取亮的地方
binary_threshold(ImageScaled1, Region, 'max_separability', 'light', UsedThreshold)
*将每个图像变成单独的连接区域
connection(Region, ConnectedRegions1)
*特征提取
select_shape (ConnectedRegions1, SelectedRegions1, 'area', 'and', 120.97, 2585.13)
*开运算:先腐蚀再膨胀去掉噪点
opening_rectangle1(SelectedRegions1, RegionOpening, 1,5)
*膨胀运算将字符连接起来,和二值化的字符取交集
dilation_rectangle1(RegionOpening, RegionDilation, 5, 5)
*联合拼接字符
union1(RegionDilation, RegionUnion)
*连接成一个单独的个体
connection(RegionUnion, ConnectedRegions2)
*求交集提取字符
intersection(ConnectedRegions2, Region, RegionIntersection)
*识别字符
*排序
sort_region(RegionIntersection, SortedRegions, 'character', 'true', 'column')
*读取识别的模板
read_ocr_class_mlp('Industrial_0-9A-Z_Rej.omc', OCRHandle)
*识别
do_ocr_multi_class_mlp(SortedRegions, PolarTransImage, OCRHandle, Class, Confidence)
*显示
*求连通域个数
count_obj(SortedRegions,num)
*求最小外接矩形
smallest_rectangle1(SortedRegions, Row1, Column1, Row2, Column2)
set_display_font (WindowHandle, 50, 'mono', 'true', 'false')
dev_clear_window ()
dev_display (Image)
dev_set_color ('red')
for i:=0 to 16 by 1
set_tposition (WindowHandle,30, 10+40*i)
write_string (WindowHandle, Class[i])
endfor