python识别文字 opencv,使用Python和OpenCV在OCR中检测字间距

I am new to Python and OpenCV . I am currently working on OCR using Python and OpenCV without using Tesseract.Till now I have been successful in detecting the text (character and digit) but I am encountering a problem to detect space between words.

Eg-

If the image says "Hello John", then it detects hello john but cannot detect space between them, so my output is "HelloJohn" without any space between them.My code for extracting contour goes like this(I have imported all the required modules, this one is the main module extracting contour) :

imgGray = cv2.cvtColor(imgTrainingNumbers, cv2.COLOR_BGR2GRAY)

imgBlurred = cv2.GaussianBlur(imgGray, (5,5), 0)

imgThresh = cv2.adaptiveThreshold(imgBlurred,

255,

cv2.ADAPTIVE_THRESH_GAUSSIAN_C,

cv2.THRESH_BINARY_INV,

11,

2)

cv2.imshow("imgThresh", imgThresh)

imgThreshCopy = imgThresh.copy()

imgContours, npaContours, npaHierarchy = cv2.findContours(imgThreshCopy,

cv2.RETR_EXTERNAL,

cv2.CHAIN_APPROX_SIMPLE)

After this I classify the extracted contours which are digits and character.

Please help me detecting space between them.

Thank You in advance,your reply would be really helpful.

解决方案

Since you did not give any example images, I just generated a simple image to test with:

h, w = 100, 600

img = np.zeros((h, w), dtype=np.uint8)

font = cv2.FONT_HERSHEY_SIMPLEX

cv2.putText(img, 'OCR with OpenCV', (30, h-30), font, 2, 255, 2, cv2.LINE_AA)

67164b8058552692c68c47fd73a43b57.png

As I mentioned in the comments, if you simply dilate the image, then the white areas will expand. If you do this with a large enough kernel so that nearby letters merge, but small enough that separate words do not, then you'll be able to extract contours of each word and use that to mask one word at a time for OCR purposes.

kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (15, 15))

dilated = cv2.dilate(img, kernel)

fed3005038c8cf4d840e8af9edac9890.png

To get the mask of each word individually, just find the contours of these larger blobs. You can sort the contours too; vertically, horizontally, or both so that you get the words in proper order. Here since I just have a single line I'll sort just in the x direction:

contours = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[1]

contours = sorted(contours, key=lambda c: min(min(c[:, :, 0])))

for i in range(len(contours)):

mask = np.zeros((h, w), dtype=np.uint8)

# i is the contour to draw, -1 means fill the contours

mask = cv2.drawContours(mask, contours, i, 255, -1)

masked_img = cv2.bitwise_and(img, img, mask=mask)

cv2.imshow('Masked single word', masked_img)

cv2.waitKey()

# do your OCR here on the masked image

824270398bdbd6263e8079e0a5b2b731.png

a1f91fc000f61910fdde191cfebbaf80.png

ca6711246711ff87f2924b7ca397782b.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值