How to extract decimal in image with Pytesseract

前端 未结 3 1560
半阙折子戏
半阙折子戏 2021-01-06 11:21

Above is the image ,I have tried everything I could get from SO or google ,nothing seems to work. I can not get the exact value in image , I should get 2.10 , Inste

3条回答
  •  旧巷少年郎
    2021-01-06 12:23

    Before throwing the image into Pytesseract, some preprocessing to clean/smooth the image helps. Here's a simple approach

    • Convert image to grayscale and enlarge image
    • Threshold
    • Perform morphological operations to clean image
    • Invert image

    First we convert the image to grayscale, resize using the imutils library then threshold to obtain a binary image

    Now we perform morphological transformations to smooth the image

    Now we invert the image for Pytesseract and add a Gaussian blur

    We use the --psm 10 config flag since we want to treat the image as a single character. Here's some additional configuration flags that could be useful

    Results

    $2.10

    After filtering

    2.10

    import cv2
    import pytesseract
    import imutils
    
    pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
    
    image = cv2.imread('1.png',0)
    image = imutils.resize(image, width=300)
    thresh = cv2.threshold(image, 150, 255, cv2.THRESH_BINARY_INV)[1]
    
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))
    close = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
    
    result = 255 - close 
    result = cv2.GaussianBlur(result, (5,5), 0)
    
    data = pytesseract.image_to_string(result, lang='eng',config='--psm 10 ')
    processed_data = ''.join(char for char in data if char.isnumeric() or char == '.')
    print(data)
    print(processed_data)
    
    cv2.imshow('thresh', thresh)
    cv2.imshow('close', close)
    cv2.imshow('result', result)
    cv2.waitKey()
    

提交回复
热议问题