问题
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:
- Apply a sharpening kernel
- Otsu's threshold
- Apply slight Gaussian blur
- Invert image
- 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