如何检测泡泡字母编号

时间:2019-11-13 02:24:16

标签: python machine-learning ocr

我应该如何处理,然后从这张图片中找到编号:

enter image description here 这是我用来处理图像的代码:

def process_img(screen):
    global filename
    ap = argparse.ArgumentParser()
    ap.add_argument("-p", "--preprocess", type=str, default="thresh",
                    help="type of preprocessing to be done")
    args = vars(ap.parse_args())
    b = screen.copy()

    resizedimg = cv2.resize(b, (750,500))

    gamma = 0.3  # change the value here to get different result
    adjusted = adjust_gamma(resizedimg, gamma=gamma)
    ret, thresh = cv2.threshold(adjusted, 127, 255, cv2.THRESH_BINARY)
    kernel = np.ones((3, 3), np.uint8)
    img = cv2.erode(thresh, kernel, iterations=1)

    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    if args["preprocess"] == "thresh":
        img_gray = cv2.threshold(img_gray, 0, 255,
                             cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
    elif args["preprocess"] == "blur":
        img_gray = cv2.medianBlur(img_gray, 3)
    blurred = cv2.GaussianBlur(img_gray, (5, 5), 0)
    edged = cv2.Canny(blurred, 50, 200, 255)
    filename = "{}.png".format(os.getpid())
    cv2.imwrite(filename, blurred)
    return blurred

将其处理为: enter image description here 然后,我使用以下代码将图像实际转换为文本:

text = pytesseract.image_to_string(Image.open(filename), lang='eng', config='--psm 13 --oem 3 -c '
                                                                                'tessedit_char_whitelist=0123456789')

但是,即使在我尝试调整设置并使之如此设置时,也只有大多数情况下这是行不通的,从而仅使文本边缘稍微模糊/平滑。不过,我无法对此进行很多改进,如果有人提出建议,将不胜感激。

0 个答案:

没有答案