本文主要是介绍機器視覺Halcon檢測動態想要的特征區域識別字符,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
光學字符識別是机器视觉领域非常重要的一个应用。机器视觉是集光学、机电和计算机三个领域的一门不算新的技术。此例子用的CNN神經網絡算子識別,調用的訓練庫是系統HDevelop自帶的
識別的圖片為電錶的屏幕截圖,用於提取電錶中間的一大串數字
要識別的圖片:
識別過程
1、閾值處理
mean_image(Image,Mean,15,15)
dyn_threshold(Image,Mean,Region,5,‘dark’)
dev_display (Region)
圖片效果:
2、執行代碼:
*連接字符
closing_rectangle1 (Region, RegionClosing1, 1, 5)
3、裁剪不要的,將不要的其他區域剪小
opening_circle (RegionClosing1, GuoRegion,2.2)
4、 *篩選區域
在調用select_shape 之前必須要調用連通區域方法connection 才有效果,切記
*计算区域的连通组件
connection (GuoRegion, ConnPatterns)
5、篩選區域
select_shape (ConnPatterns, shapeRegion, ‘area’, ‘and’, 260, 99999)
6、去掉頭頂的紅杠
*去掉圖片頭頂的紅色橫杠
select_shape (shapeRegion, shapeRegion2, ‘width’, ‘and’, 10, 100)
7、求交集,與第一步處理后的區域求交集
*求交集 以區分的顏色模塊在前,原字符再后,才能單獨著色交集后的單體字符
intersection (shapeRegion2, Region, RegionIntersection)
dev_display(RegionIntersection)
8、最後用cnn卷積神經網絡識別
*排序
sort_region (RegionIntersection, SortedRegions, ‘character’, ‘true’, ‘row’)
dev_set_draw (‘margin’)
dev_set_shape (‘rectangle1’)
dev_display (SortedRegions)
shape_trans (SortedRegions, RegionTrans, ‘rectangle1’)
area_center (RegionTrans, Area, Row, Column)
MeanRow := mean(Row)
FontName := ‘Universal_Rej’
count_obj (SortedRegions, Number)
read_ocr_class_cnn (FontName, OCRHandle)
for I := 1 to Number by 1
select_obj (SortedRegions, ObjectSelected, I)
do_ocr_single_class_cnn (ObjectSelected, Image, OCRHandle, 1, Class, Confidence)
dev_disp_text (Class, ‘image’, MeanRow+30, Column[I - 1]-5, ‘green’, ‘box’, ‘false’)
endfor
識別結果:
Halcon HDevelop完整代碼:
dev_update_off()
read_image(Image,'d:/Desktop/vision/DBIAO01.png')
get_image_size(Image,Width,Height)dev_close_window ()
dev_open_window (0, 0, Width*2, Height*2, 'black', WindowHandle)
set_display_font (WindowHandle, 30, 'mono', 'true', 'false')
dev_display (Image)dev_set_line_width (2)mean_image(Image,Mean,15,15)
dyn_threshold(Image,Mean,Region,5,'dark')
dev_display (Region)*clip_region (Region, RawSegmentation, 74.8873, 58.6812, 132.943, 312.194)
*select_shape (Region, SelectedRegions, 'height', 'and',170,99999)
*gen_rectangle1 (ROI_0, 73.2924, 55.8125, 135.814, 312.938)*連接字符,以9度閉合畫圓圈
*closing_circle (CharCandidates, RegionClosing1, 1.5)
closing_rectangle1 (Region, RegionClosing1, 1, 5)
*去除不要的
opening_circle (RegionClosing1, GuoRegion,2.2)* fill_up (RegionClosing1, Solid)
* opening_rectangle1 (Solid, Cut, 5, 2)*计算区域的连通组件connection (GuoRegion, ConnPatterns)*篩選區域select_shape (ConnPatterns, shapeRegion, 'area', 'and', 260, 99999)*去掉圖片頭頂的紅色橫杠select_shape (shapeRegion, shapeRegion2, 'width', 'and', 10, 100)*求交集 以區分的顏色模塊在前,原字符再后,才能單獨著色交集后的單體字符
intersection (shapeRegion2, Region, RegionIntersection)
dev_display(RegionIntersection)*排序
sort_region (RegionIntersection, SortedRegions, 'character', 'true', 'row') dev_set_draw ('margin')
dev_set_shape ('rectangle1')
dev_display (SortedRegions)shape_trans (SortedRegions, RegionTrans, 'rectangle1')
area_center (RegionTrans, Area, Row, Column)
MeanRow := mean(Row)
FontName := 'Universal_Rej'
count_obj (SortedRegions, Number)
read_ocr_class_cnn (FontName, OCRHandle)
for I := 1 to Number by 1select_obj (SortedRegions, ObjectSelected, I)do_ocr_single_class_cnn (ObjectSelected, Image, OCRHandle, 1, Class, Confidence)dev_disp_text (Class, 'image', MeanRow+30, Column[I - 1]-5, 'green', 'box', 'false')
endfor
这篇关于機器視覺Halcon檢測動態想要的特征區域識別字符的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!