简体   繁体   English

确定已知字体中的图像中是否存在数字(OCR + OpenCV)

[英]Determine if Digit Exists in Image from Known Font (OCR + OpenCV)

With Python 2.7, I'm trying to recognize digits on playing cards . 使用Python 2.7,我试图识别纸牌上的数字。 I've figured out the font being used (Herculanum) and created a reference image of the digits. 我已经弄清楚正在使用的字体(Herculanum)并创建了数字的参考图像 Multiple cards may be in an image at different angles (upside down for example) and will likely overlap . 一张图像中可能有多张不同角度的卡片(例如上下颠倒),并且可能会重叠 Eventually, color will come into play, but I want to identify numbers first. 最终,颜色将发挥作用,但我想首先确定数字。

Ideally, I would want to know of presence of a number and possibly its location in the image. 理想情况下,我想知道数字的存在及其在图像中的位置。 I've tried a few methods using pytesseract and OpenCV. 我尝试了一些使用pytesseract和OpenCV的方法。 I'm also in the process of training a TensorFlow neural network, but I think it's overkill for what seems to be a simple problem. 我也正在训练TensorFlow神经网络,但是我认为这似乎是一个简单的问题,这太过分了。 I have some code to read in the reference digits, but I'm struggling to tie it back to an example card image. 我有一些代码可以读取参考数字,但我正努力将其绑定到示例卡图像上。

from imutils import contours
import numpy as np
import imutils
import cv2

ref = cv2.imread('ocr_a_reference.png')
ref = cv2.cvtColor(ref, cv2.COLOR_BGR2GRAY)
ref = cv2.threshold(ref, 10, 255, cv2.THRESH_BINARY_INV)[1]

refCnts = cv2.findContours(ref.copy(), cv2.RETR_EXTERNAL, 
cv2.CHAIN_APPROX_SIMPLE)
refCnts = refCnts[0] if imutils.is_cv2() else refCnts[1]
refCnts = contours.sort_contours(refCnts, method="left-to-right")[0]
digits = {}

for (i, c) in enumerate(refCnts):
    # compute the bounding box for the digit, extract it, and resize
    # it to a fixed size
    (x, y, w, h) = cv2.boundingRect(c)
    roi = ref[y:y + h, x:x + w]
    roi = cv2.resize(roi, (57, 88))

    # update the digits dictionary, mapping the digit name to the ROI
    digits[i] = roi

From a known font, is it possible to identify matching digits in an image? 通过已知的字体,可以识别图像中的匹配数字吗? It would almost be like searching for an image within an bigger image for the closest match. 这几乎就像在更大的图像中搜索图像以找到最匹配的图像。 The ideal end output would be a list of digits and the count within the image. 理想的最终输出将是数字列表和图像中的计数。 Is this possible with pytesseract and/or OpenCV? pytesseract和/或OpenCV是否可能? Or is there another library I should be looking at? 还是我应该看看另一个图书馆?

Thanks for the advice! 谢谢你的建议!

You can use tesseract to iterate over words that it has identified and get the bounding box for each word.However, you will have the following challenge : 您可以使用tesseract遍历已识别的单词并获取每个单词的边界框,但是您将面临以下挑战:

  • Orientation : While you can configure tesseract to auto detect orientation , your image has multiple orientations for different cards which contain the digits. 方向:虽然可以将tesseract配置为自动检测方向,但是对于包含数字的不同卡,图像具有多种方向。
  • Duplicates : You will have same digits visible twice on some card and only once on others if they are hidden by other cards. 重复:您将在某张卡上看到相同的数字两次,而在其他卡上将它们隐藏一次。

One approach you might be able to use is that you can generate your reference digits for font used in card at multiple rotations for ex: every 10 degress...As a result of this you will have 36 reference images for each character and 360 in all. 您可能会使用的一种方法是,可以多次旋转生成卡中使用的字体的参考数字,例如:每10个下降点...因此,您将获得每个字符36个参考图像和360个in图像。所有。 You can now train tesseract on this model. 您现在可以在此模型上训练tesseract。 Also, i notice your card has black color used only for digits and border. 另外,我注意到您的卡为黑色,仅用于数字和边框。 You can preprocess your image to remove remaining background colors to improve accuracy..You might have to retry with different angles if you are not getting very good result with 10 degree rotations. 您可以对图像进行预处理,以去除剩余的背景色,以提高准确性。.如果旋转10度后效果不佳,则可能必须重新尝试不同的角度。 For duplicate issue, once you have bounding box for all digits and you know that digits in a given card will have fixed distance from its opposite pair. 对于重复问题,一旦所有数字都具有边界框,并且知道给定卡中的数字与其相对的对具有固定的距离。 you can use this information to remove any duplicates. 您可以使用此信息删除所有重复项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM