首页 > 编程语言 >实战 | Halcon实例转OpenCV实现-复杂背景下OCR数字识别(附源码)

实战 | Halcon实例转OpenCV实现-复杂背景下OCR数字识别(附源码)

时间:2022-11-09 14:37:00浏览次数:41  
标签:img cv2 Halcon OpenCV 源码 result thres pt1 255



导读

本文主要介绍一个复杂背景下OCR数字提取识别实例,并将Halcon实现转为OpenCV。

实例来源

Halcon例程ocrcolor.hdev,例程代码精简修改如下:

* Author: Color Space
* 来源-公众号:OpenCV与AI深度学习
*--------------------------------
dev_get_window (WindowHandle)
read_image (Image, '5.png')


decompose3 (Image, R, G, B)
trans_from_rgb (R, G, B, H, S, V, 'hsv')
threshold (B, Regions, 0, 240)
dilation_circle (Regions, RegionDilation, 4.5)
connection (RegionDilation, ConnectedRegions)


dev_display (Image)
select_shape (ConnectedRegions, SelectedRegions, ['width','height'], 'and', [25.86,80.71], [67.84,138.51])
sort_region(SelectedRegions, SortedRegions, 'first_point', 'true', 'column')


read_ocr_class_mlp ('Industrial_0-9_NoRej', OCRHandle)
do_ocr_multi_class_mlp (SortedRegions, B, OCRHandle, RecChar, Confidence)
area_center (SortedRegions, Area, Row, Column)
set_display_font (WindowHandle, 27, 'mono', 'true', 'false')
disp_message (WindowHandle, sum(RecChar), 'window', 32, 24, 'red', 'false')

实现效果:

实战 | Halcon实例转OpenCV实现-复杂背景下OCR数字识别(附源码)_公众号

实战 | Halcon实例转OpenCV实现-复杂背景下OCR数字识别(附源码)_c#_02

OpenCV实现步骤与代码

实现步骤:

【1】分离RGB颜色通道,选择B(蓝色)通道处理;

【2】阈值处理,凸显出数字;

【3】形态学:膨胀 + 腐蚀 ->或直接闭运算,将数字轮廓断开部分连接

【4】轮廓筛选,剔除杂讯

【5】使用PaddleOCR或EasyOCR直接用以上步骤二值图识别。



逐步效果演示与代码:

完整源码:

# Author:Color Space
# 来源-公众号:OpenCV与AI深度学习
# --------------------------------
import numpy as np
import cv2
import easyocr


reader = easyocr.Reader(['en'])
img = cv2.imread('2.png')
cv2.imshow('src', img)


B,G,R=cv2.split(img)


ret,thres= cv2.threshold(B, 230, 255, cv2.THRESH_BINARY_INV)
cv2.imshow('thres', thres)




k1=np.ones((11,11), np.uint8)
k2=np.ones((9,9), np.uint8)
thres = cv2.morphologyEx(thres, cv2.MORPH_DILATE, k1)
thres = cv2.morphologyEx(thres, cv2.MORPH_ERODE, k2)
cv2.imshow('morph', thres)
#cv2.imwrite('morph.jpg', thres)


contours,hierarchy = cv2.findContours(thres, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
rects = []
for cnt in contours:
(x, y, w, h) = cv2.boundingRect(cnt)
if w>200 or h > 200:
#cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),1)
cv2.drawContours(thres,cnt,-1,(0,0,0),-1)
if w > 10 and w < 70 or h > 50 and h < 90:
cv2.drawContours(img,cnt,-1,(255,0,0),2)
#cv2.imwrite('morph.jpg', thres)


result = reader.readtext(thres)
if len(result) > 0:
for i in range(0,len(result)):
pt1 = (tuple(result[i][0][0]))
pt2 = (tuple(result[i][0][1]))
pt3 = (tuple(result[i][0][2]))
pt4 = (tuple(result[i][0][3]))
cv2.line(img,pt1,pt2,(0,0,255),2,cv2.LINE_AA)
cv2.line(img,pt2,pt3,(0,0,255),2,cv2.LINE_AA)
cv2.line(img,pt3,pt4,(0,0,255),2,cv2.LINE_AA)
cv2.line(img,pt4,pt1,(0,0,255),2,cv2.LINE_AA)
print(result[i][1])
strText = result[i][1].replace(' ','')
cv2.putText(img,strText,(pt1[0]+2,pt1[1]-5),0,1.0,(255,0,255),2)


else:
print('have not found any text!')


cv2.imshow('result', img)
cv2.imwrite('result.jpg', img)


cv2.waitKey(0)
cv2.destroyAllWindows()
print ('finish')

原图:

实战 | Halcon实例转OpenCV实现-复杂背景下OCR数字识别(附源码)_深度学习_03

RGB通道分离:

实战 | Halcon实例转OpenCV实现-复杂背景下OCR数字识别(附源码)_c#_04

B通道阈值提取:

实战 | Halcon实例转OpenCV实现-复杂背景下OCR数字识别(附源码)_深度学习_05

形态学膨胀腐蚀:

实战 | Halcon实例转OpenCV实现-复杂背景下OCR数字识别(附源码)_c#_06

轮廓筛选识别:

实战 | Halcon实例转OpenCV实现-复杂背景下OCR数字识别(附源码)_深度学习_07

更多图像测试验证:

实战 | Halcon实例转OpenCV实现-复杂背景下OCR数字识别(附源码)_公众号_08

实战 | Halcon实例转OpenCV实现-复杂背景下OCR数字识别(附源码)_公众号_09


实战 | Halcon实例转OpenCV实现-复杂背景下OCR数字识别(附源码)_c#_10

实战 | Halcon实例转OpenCV实现-复杂背景下OCR数字识别(附源码)_公众号_11

实战 | Halcon实例转OpenCV实现-复杂背景下OCR数字识别(附源码)_公众号_12



标签:img,cv2,Halcon,OpenCV,源码,result,thres,pt1,255
From: https://blog.51cto.com/stq054188/5836299

相关文章