【问题标题】:Text Detection: Getting Bounding boxes文本检测:获取边界框
【发布时间】:2018-04-24 11:15:19
【问题描述】:

我有一个文本文档的黑白图像。我希望能够获得每个字符的边界框列表。我自己尝试过一种算法,但它花费的时间太长,而且只是有点成功。我可以使用任何 python 库来查找边界框吗?我一直在研究 opencv,但文档很难理解。而在这个tutorial 中,我什至无法破译是否找到了边界框,因为我无法轻易找到函数的实际作用。

【问题讨论】:

  • 你提供的LINK涉及分水岭算法。当您想要分隔明文时,这不是必需的。你最好试试THIS

标签: python-3.x opencv image-segmentation


【解决方案1】:

您可以使用boundingRect()。确保您的图像背景为黑色,图像中的文本为白色。使用此代码,您可以在图像中的文本周围绘制矩形。要获取每个矩形的列表,请根据您的要求添加相应的代码段。

import cv2
img = cv2.imread('input.png', 0) 
cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU,img)

image, contours, hier = cv2.findContours(img, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
for c in contours:
    # get the bounding rect
    x, y, w, h = cv2.boundingRect(c)
    # draw a white rectangle to visualize the bounding rect
    cv2.rectangle(img, (x, y), (x + w, y + h), 255, 1)

cv2.drawContours(img, contours, -1, (255, 255, 0), 1)

cv2.imwrite("output.png",img)

【讨论】:

    【解决方案2】:

    我建议您查看 openCV 库中的 boundingRect() 函数:

    https://docs.opencv.org/2.4/doc/tutorials/imgproc/shapedescriptors/bounding_rects_circles/bounding_rects_circles.html

    文档基于cpp,但也可以在python中实现。

    【讨论】:

      猜你喜欢
      • 2020-04-03
      • 1970-01-01
      • 1970-01-01
      • 2013-04-06
      • 1970-01-01
      • 1970-01-01
      • 2015-02-01
      • 2014-02-04
      • 2013-12-09
      相关资源
      最近更新 更多