extract data from the overlapping letters in a image

爱⌒轻易说出口 提交于 2020-02-24 12:21:13

问题


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)

回答1:


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

Sharpen

Otsu's threshold

Slight Gaussian blur

Invert image

Here's the OCR results using Pytesseract

DST INTERNATIONAL D-307@ 3266 01 Dec 2007. HowellJerde Jan!
2007" 125802AM RafaelaBoyer Keon3@gmnil.com 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()


来源:https://stackoverflow.com/questions/59283501/extract-data-from-the-overlapping-letters-in-a-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!