0

I have some images of some ceramic plates. The one shown below is an example of the worst possible from the batch. I am having trouble preprocessing it before using tesseract on it to the get the text (if it's possbile at all). If someone could give me a kick in the right direction preprocessing wise that would be wonderful.

Example unprocessed image:

Plate unprocessed

enter image description here

I have tried a few things myself for the last 4 days and the best I could come up is this:

resulting preprocessed image

enter image description here

As for the code I used:

    import os
import numpy as np
import matplotlib.pyplot as plt
try:
    from cv2 import cv2
except ImportError:
    pass

cwd = os.path.dirname(os.path.abspath(__file__))
input_dir_path = cwd + '/data/input'
output_dir_path = cwd + '/data/output'

input_image_names = os.listdir(input_dir_path)


def nothing(x):
    pass

def makeValueOdd(value):
    if (value % 2) is not 0:
        return value
    return value + 1

wnd = 'Test'
cv2.namedWindow(wnd, cv2.WINDOW_NORMAL)
cv2.createTrackbar("Threshold", wnd, 127, 255, nothing)
cv2.createTrackbar("Blur", wnd, 3, 255, nothing)
cv2.createTrackbar("Lower_gray", wnd, 100, 255, nothing)
cv2.createTrackbar("Higher_gray", wnd, 255, 255, nothing)

img = cv2.imread(input_dir_path + '/' + input_image_names[0])
clahe = cv2.createCLAHE(clipLimit=4.3, tileGridSize=(8,8))


while (1):
    thresh_bin_val = cv2.getTrackbarPos("Threshold", wnd)
    blur_kernel_value = (makeValueOdd(cv2.getTrackbarPos("Blur", wnd)), 
    makeValueOdd(cv2.getTrackbarPos("Blur", wnd)))
    lower_val = cv2.getTrackbarPos("Lower_gray", wnd)
    higher_val = cv2.getTrackbarPos("Higher_gray", wnd)

    hsv_image = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

    lower_gray = np.array([0, 0, lower_val])
    higher_gray = np.array([0, 0, higher_val])

    mask = cv2.inRange(hsv_image, lower_gray, higher_gray)
    result = cv2.bitwise_and(img, img, mask=mask)
    gray_img = cv2.cvtColor(result, cv2.COLOR_BGR2GRAY)

    cl1 = clahe.apply(gray_img)

    blurr = cv2.GaussianBlur(cl1, blur_kernel_value, 0)
    ret, thresh1 = cv2.threshold(blurr, thresh_bin_val, 255, 
    cv2.THRESH_BINARY_INV)

    subtract = cv2.bitwise_not(thresh1)
    cv2.imshow(wnd, subtract)

    k = cv2.waitKey(1) & 0xFF
    if k == ord('m'):
        mode = not mode
    elif k == 27:
        break

    cv2.destroyAllWindows()
Rick Smith
  • 3,962
  • 6
  • 13
  • 24
  • [This](https://stackoverflow.com/questions/56303292/identify-clear-text-from-image-python/56303477#56303477) might help in preprocessing. Btw I won't recommend tesseract for complex image OCR's as tesseract is not good at dealing with those efficiently, I would recommend using OpenCV, or use some other OCR Api – Vasu Deo.S May 31 '19 at 14:35
  • Thanks for the recommendation. Ended up making calls to Google Vision API and got back partials of text. Still having trouble with image pre-processing. – Olariu Lucian Jun 03 '19 at 15:54

0 Answers0