如何通过 Opencv 在图像中找到类似 table 的结构

How to find table like structure in image by Open CV

我没有像图片上那样table镶边。

我尝试使用 中的示例 我得到了这个结果

但我需要这样的东西

如何调整 Open CV 以获得所需的结果?

使用pytesseract的image_to_data方法即可轻松实现。

你还需要知道:


步骤:

    1. 以 BGR 模式加载图像并将其转换为灰度
    1. 获取感兴趣区域
    1. 从每个区域,获取坐标并绘制矩形。

结果:

代码:


# Load the library
import cv2
import pytesseract

# Load the image
img = cv2.imread("1Tksb.jpg")

# Convert to gry-scale
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# OCR detection
d = pytesseract.image_to_data(gry, config="--psm 6", output_type=pytesseract.Output.DICT)

# Get ROI part from the detection
n_boxes = len(d['level'])

# For each detected part
for i in range(n_boxes):

    # Get the localized region
    (x, y, w, h) = (d['left'][i], d['top'][i], d['width'][i], d['height'][i])

    # Draw rectangle to the detected region
    cv2.rectangle(img, (x, y), (x+w, y+h), (0, 0, 255), 1)

# Display
cv2.imshow("img", img)
cv2.waitKey(0)

如果您想阅读,可以使用image_to_string方法。