【问题标题】:Processing an image with block letters for OCR为 OCR 处理带有大写字母的图像
【发布时间】:2019-01-22 12:34:49
【问题描述】:

我想对游戏中的战斗日志进行 OCR:

战斗记录图片

原始图像具有块文本字体,因此在阈值处理后我反转了颜色。现在我想删除“黑色”背景(但不是黑色文本),但我不确定如何在 OpenCV 中实现。之后我想我想将文本锐化为更粗以获得更好的 OCR。

请问我该怎么办?

【问题讨论】:

    标签: opencv tesseract


    【解决方案1】:

    试试这个。

    import cv2
    import numpy as np
    
    img = cv2.imread("1.png")
    
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    
    invert0 = cv2.bitwise_not(gray)
    
    _,thresh = cv2.threshold(invert0,128,255,cv2.THRESH_BINARY)
    
    invert1 = cv2.bitwise_not(thresh)
    
    im2, contours, hierarchy = cv2.findContours(invert1,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    
    mask = np.zeros(gray.shape, dtype="uint8")
    for i in range(len(contours)):
        if(hierarchy[0][i][3]==-1): #contour has no parent (most outer contour)
            cv2.fillPoly(mask, pts =[contours[i]], color=255)
    
    invert2 = cv2.bitwise_not(mask)
    res = invert2 + invert1
    
    cv2.imshow("img", img)    
    cv2.imshow("gray", gray)   
    cv2.imshow("invert0", invert0) 
    cv2.imshow("thresh", thresh) 
    cv2.imshow("invert1", invert1) 
    cv2.imshow("invert2", invert2)
    cv2.imshow("mask", mask)
    cv2.imshow("res", res)
    
    cv2.waitKey()
    cv2.destroyAllWindows() 
    

    【讨论】:

    • 我一直关注你的个人资料。您对简单图像处理任务的回答非常有用。
    • 谢谢!它显然适用于屏幕截图,因此我将其标记为已接受的答案。为了理解完整的逻辑,我有很多要研究的地方。
    猜你喜欢
    • 2013-02-27
    • 2019-10-16
    • 2015-09-07
    • 2018-09-22
    • 2016-03-10
    • 2010-11-10
    • 1970-01-01
    • 2014-03-30
    • 1970-01-01
    相关资源
    最近更新 更多