使用蒙版将单词合并成行

问题描述 投票:0回答:1

我的文本检测器有一个掩码,如下所示: 其中分段区域是单词。 我想找到尽可能近似单词的线(4 个点 [x1,y1,x2,y2,...,x4,y4] 或 5 个值 [x1,y1,h,w,angle])。 预期输出:

重现代码:

# pip install "python-doctr[torch]"
import numpy as np
import cv2
from doctr.models import detection_predictor
from doctr.io import DocumentFile
from matplotlib import pyplot as plt


def plot(image,si=[12,12]):
    fig, ax = plt.subplots(figsize=si);ax.imshow(image,cmap='gray')
    ax.get_xaxis().set_visible(False);ax.get_yaxis().set_visible(False)
    plt.show()


det_predictor = detection_predictor(
        arch='db_resnet50', 
        pretrained=True,
        pretrained_backbone=True,
        batch_size=2,
        assume_straight_pages=False,
        preserve_aspect_ratio=True,
        symmetric_pad=True
    )

path = "path/to/your/file/"
input =  DocumentFile.from_images(path)

img = cv2.imread(path)
img = cv2.resize(img, (1024, 1024))

out = det_predictor(input, return_maps=True)

segm_map = (out[1][0]*255).astype(np.uint8)

# Find contours for each text line
contours, _ = cv2.findContours(segm_map, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

contours = [cnt for cnt in contours if (cv2.boundingRect(cnt)[2] / cv2.boundingRect(cnt)[3])>=3.0]
for contour in contours:
    epsilon = 0.001 * cv2.arcLength(contour, True)
    approx = cv2.approxPolyDP(contour, epsilon, True)
    cv2.polylines(img, [approx], True,(0, 255, 0) )

plot(segm_map)
plot(img)
python opencv computer-vision ocr
1个回答
0
投票

您的“预期输出”看起来像是通过以下过程创建的。 因此,尝试这样的方法怎么样?


  1. 决定哪 2 个区域应合并为一个区域(或者如果没有一对区域应合并,则过程结束)。 例如,对于所有可能的区域对,评估marge结果的优度,然后选择最佳优度对。
  2. 合并步骤 1 中选择的对。
  3. 转到步骤 1。

其中,“优点”可能取决于{合并结果区域的形状(直线度)或角度、2个区域之间的距离等}?

© www.soinside.com 2019 - 2024. All rights reserved.