文本检测:获取边界框

Text Detection: Getting Bounding boxes

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

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

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

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

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

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)