Halcon學習筆記之OCR系列-環形字符,斜體字

這篇文章主要是介紹下我經歷的一些比較難已提取OCR部分的圖片,從而介紹下一些特別的處理方式。
第一種:差分高斯diff_of_gauss(近似拉普拉斯高斯)
原圖如下:一般的方法基本提取不出來相應的字符。
在這裏插入圖片描述
那我們可以通過差分高斯這個算子直接得出很好的效果圖,代碼以及效果圖如下:



read_image (Image, 'C:/Users/Administrator/Desktop/3.bmp')
rgb1_to_gray (Image, GrayImage)
*差分高斯
diff_of_gauss (GrayImage, DiffOfGauss, 3, 1.6)
threshold (DiffOfGauss, Regions, 2, 12)

在這裏插入圖片描述
這個效果可以直接進行處理,參照我之前的處理OCR的程序,直接就可以得出結果了,這裏我就不再寫了。

第二種:環形的字符,主要的思路就是通過極座標轉換,將環形部分拉直,剩下的就是正常的字符讀取了。
原圖:
在這裏插入圖片描述

read_image (Image, 'C:/Users/Administrator/Desktop/環形字符.png')
rgb1_to_gray (Image, GrayImage)
get_image_size (GrayImage, Width, Height)
emphasize (GrayImage, ImageEmphasize, Width, Height, 1)
threshold (ImageEmphasize, Regions, 0, 21)
connection (Regions, ConnectedRegions)
select_shape_std (ConnectedRegions, SelectedRegions, 'max_area', 70)
fill_up (SelectedRegions, RegionFillUp)
*分別兩次膨脹
dilation_circle (RegionFillUp, RegionDilation, 20)
dilation_circle (RegionFillUp, RegionDilation1, 70)
*這裏是求出兩個膨脹區域的最小外接圓的圓心和半徑,爲之後的極座標轉換做準備
smallest_circle (RegionDilation, InnerRow, InnerCol, InnerRadius)
smallest_circle (RegionDilation1, OuterRow, OuterCol, OuterRadius)
*求出兩個膨脹區域的差異部分,就是求出一個字體圓環部分
difference (RegionDilation1, RegionDilation, RegionDifference)
reduce_domain (ImageEmphasize, RegionDifference, ImageReduced)
*這裏就是極座標轉換的算子了,就是將環形部分拉直,方便讀取OCR
polar_trans_image_ext (ImageReduced, PolarTransImage, OuterRow, OuterCol, rad(-30), rad(-120), InnerRadius+16, OuterRadius, Width, Height/8, 'nearest_neighbor')
mirror_image (PolarTransImage, ImageMirror, 'row')
gray_range_rect (ImageMirror, ImageResult, 7, 7)
binary_threshold (ImageResult, Region, 'max_separability', 'light', UsedThreshold)
connection (Region, ConnectedRegions1)
select_shape (ConnectedRegions1, SelectedRegions1, ['height','area'], 'and', [54.14,912.03], [100,10000])
partition_rectangle (SelectedRegions1, Partitioned, 40, 70)
sort_region (Partitioned, SortedRegions, 'character', 'true', 'row')
invert_image (ImageResult, ImageInvert)
read_ocr_class_mlp ('Industrial_0-9A-Z_NoRej.omc', OCRHandle)
do_ocr_multi_class_mlp (SortedRegions, ImageInvert, OCRHandle, Class, Confidence)
dev_display (Image)
set_tposition (3600, 61, 63)
write_string (3600, Class)

字體圓環部分效果圖:
在這裏插入圖片描述
環形字體拉直效果圖:
在這裏插入圖片描述
最終結果圖:
在這裏插入圖片描述
一般字體所處的圓環部分,兩次形態學處理再加一次difference,可以直接得出來,這種套路方法一般也應在找邊緣的地方。正常的處理這種圓環中讀取OCR,主要就是polar_trans_image_ext這個算子,其他的都是一些預處理手段。





第三種斜體字處理
原圖:
在這裏插入圖片描述
代碼如下:


read_image (Image, 'C:/Users/Administrator/Desktop/斜體字練習.png')
dev_close_window()
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_display (Image)
fast_threshold (Image, Region, 0, 128, 20)
*獲取字體區域偏轉的角度
text_line_slant (Region, Image, 45, -0.523599, 0.523599, SlantAngle)
*生成矩陣
hom_mat2d_identity (HomMat2DIdentity)
*這裏是獲取字體轉正的矩陣,SlantAngle這個只是之前獲取的字體斜了多少度,那麼要轉正的話就是糾正
*這個偏轉角度,所以就要向相反方向偏正
hom_mat2d_slant (HomMat2DIdentity, -SlantAngle, 'x', 0, 0, HomMat2DSlant)
affine_trans_image (Image, ImageAffinTrans, HomMat2DSlant, 'nearest_neighbor', 'false')
fast_threshold (ImageAffinTrans, Region1, 0, 90, 20)
connection (Region1, ConnectedRegions1)

read_ocr_class_mlp ('DotPrint_0-9A-Z.omc', OCRHandle)

dilation_rectangle1 (Region1, RegionDilation, 2, 5)
connection (RegionDilation, ConnectedRegions)
partition_rectangle (ConnectedRegions, Partitioned, 32, 45)
intersection (Partitioned, ConnectedRegions1, RegionIntersection)
sort_region (RegionIntersection, SortedRegions, 'character', 'true', 'row')
do_ocr_multi_class_mlp (SortedRegions, ImageAffinTrans, OCRHandle, Class, Confidence)

轉正後的圖片:
在這裏插入圖片描述
處理得到的結果:
在這裏插入圖片描述


以上就是一些需要經過特殊處理之後才能夠正常讀取OCR的三種常見方法。當然還有一些更爲難得項目,就比如那種刻字和本體顏色無差異的,而且字體是下沉或者凸起的,遇到這種的話首先先考慮通過低距離低角度打光突出邊緣,如果光源搭設被限制住,那麼就可以考慮光度立體這種方法來解決了(diff_of_gauss效果不行的情況下)。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章