【问题标题】:How to print text on image without overlapping using OpenCV or other image Lib?如何使用 OpenCV 或其他图像库在图像上打印文本而不重叠?
【发布时间】:2021-03-12 09:38:57
【问题描述】:

我正在使用 Google OCR API 从手写文本图像中提取文本。但我想将这些文本打印在另一个空白图像中,它们的坐标分别在原始图像上。

def draw_location(ver, image, word_text):
    font = cv2.FONT_HERSHEY_SIMPLEX
    x = (int)((ver[0].x+ver[1].x) / 2 )
    y = (int)((ver[0].y+ver[3].y) / 2 )
    cv2.putText(image, word_text, (x,y), font, 2, (250, 10, 50), 1, cv2.LINE_AA)
 

【问题讨论】:

    标签: opencv image-processing computer-vision ocr


    【解决方案1】:

    你可以使用这个逻辑。

    import cv2
    import numpy as np
    
    your_text = ['I', 'am', 'extracting', 'text', 'from', 'handwritten', 'text', 'images', 'using', 'Google', 'OCR', 'API.',
                 'but', 'I', 'want', 'to', 'print', 'those', 'text', 'in', 'another', 'blank', 'image', 'with',
                 'respective', 'their', 'coordinates', 'on', 'the', 'original', 'image.']
    
    blank_image = np.zeros((400, 400))
    height, width = blank_image.shape
    
    FONT_SCALE = 0.6
    FONT = cv2.FONT_HERSHEY_SIMPLEX
    FONT_THICKNESS = 1
    word_x, word_y = 20, 20  # Origin cordinate of text
    height_spacing = 5  # space between 2 string
    
    
    for word in your_text:
        (label_width, label_height), baseline = cv2.getTextSize(word + " ", FONT, FONT_SCALE, FONT_THICKNESS)
    
        if word_x + label_width > width:
            word_x = 0
            word_y += label_height + height_spacing
    
        cv2.putText(blank_image, word + " ", (word_x, word_y), FONT, FONT_SCALE, 255, FONT_THICKNESS)
        word_x += label_width
    
    cv2.imshow("text image", blank_image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    
    

    【讨论】:

    • 实际上,我想在原始图像上打印具有各自坐标位置的文本。
    • 您能否提供一个检测到单词坐标的示例,以便有人可以提供基于此的解决方案?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-27
    • 1970-01-01
    • 1970-01-01
    • 2021-01-28
    • 2012-09-24
    • 2016-10-13
    • 1970-01-01
    相关资源
    最近更新 更多