ajay ai
ajay ai

Reputation: 27

extract data from the overlapping letters in a image

Input image:

input image

i want to extract the data from the image ( ocr ) code which i tried:

    import cv2
    import textract
    import numpy as np
    img = cv2.imread('/home/ajay/Desktop/name.jpg',0)
    # img = cv2.imread('path_to_your_image', 0)
    _, blackAndWhite = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)
    nlabels, labels, stats, centroids = cv2.connectedComponentsWithStats(blackAndWhite, None, None, None, 8, cv2.CV_32S)
    sizes = stats[1:, -1] #get CC_STAT_AREA component
    img2 = np.zeros((labels.shape), np.uint8)
    for i in range(0, nlabels - 1):
        if sizes[i] >= 50:   #filter small dotted regions
            img2[labels == i + 1] = 255
    res = cv2.bitwise_not(img2)
    cv2.imwrite('ress.png', res)
    a =  textract.process('ress.png',method = 'tesseract')
    a = a.decode()
    print(a)

Upvotes: 2

Views: 1402

Answers (1)

nathancy
nathancy

Reputation: 46630

A simple method is:

  1. Apply a sharpening kernel
  2. Otsu's threshold
  3. Apply slight Gaussian blur
  4. Invert image
  5. OCR

Here's a visualization of the steps:

Input image

enter image description here

Sharpen

enter image description here

Otsu's threshold

enter image description here

Slight Gaussian blur

enter image description here

Invert image

enter image description here

Here's the OCR results using Pytesseract

DST INTERNATIONAL D-307@ 3266 01 Dec 2007. HowellJerde Jan!
2007" 125802AM RafaelaBoyer [email protected] Fhvio Abernathy Sr.

Code

import cv2
import numpy as np
import pytesseract

pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"

image = cv2.imread('1.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
sharpen = cv2.filter2D(gray, -1, kernel)

thresh = cv2.threshold(sharpen, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

blur = cv2.GaussianBlur(thresh, (3,3), 0)
invert = 255 - blur
data = pytesseract.image_to_string(invert, lang='eng',config='--psm 6')
print(data)

cv2.imshow('sharpen', sharpen)
cv2.imshow('thresh', thresh)
cv2.imshow('blur', blur)
cv2.imshow('invert', invert)
cv2.waitKey()

Upvotes: 1

Related Questions