python - 从图像中提取多个背景中的文本

标签 python opencv image-processing image-recognition python-tesseract

我有不同背景的多张图片,

我需要忽略背景并从我的图像中提取数字。例如:

Original

Original with different background

Original diff 3

经过测试,我有这个结果:

thresh

由于背景色,提取文字非常困难。

我正在使用此代码:

image = cv2.imread('AA.png')

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 165, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]



# Invert image and perform morphological operations
inverted = 255 - thresh
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (15,3))
close = cv2.morphologyEx(inverted, cv2.MORPH_CLOSE, kernel, iterations=1)

# Find contours and filter using aspect ratio and area
cnts = cv2.findContours(close, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    area = cv2.contourArea(c)
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.01 * peri, True)
    x,y,w,h = cv2.boundingRect(approx)
    aspect_ratio = w / float(h)
    if (aspect_ratio >= 2.5 or area < 75):
        cv2.drawContours(thresh, [c], -1, (255,255,255), -1)

# Blur and perform text extraction
thresh = cv2.GaussianBlur(thresh, (3,3), 0)
data = pytesseract.image_to_string(thresh, lang='eng',config='tessedit_char_whitelist=0123456789 --psm 6')
print(data)


cv2.imshow('close', close)
cv2.imshow('thresh', thresh)
cv2.waitKey()

即使背景颜色发生变化,我如何也能从该图像中准确提取数字?

修改后编辑结果:

comment

最佳答案

阈值化是您的问题。这是在执行OCR之前我将如何在Python / OpenCV中处理图像。

我只是将阈值设为165,以使字母为白色,背景为黑色。然后在区域上过滤轮廓,以去除小的多余白色区域。然后反转结果,以便在白色背景上有黑色字母。

输入:

enter image description here

import cv2
import numpy as np

# load image as HSV and select saturation
img = cv2.imread("numbers.png")
hh, ww, cc = img.shape

# convert to gray
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# threshold the grayscale image
ret, thresh = cv2.threshold(gray,165,255,0)

# create black image to hold results
results = np.zeros((hh,ww))

# find contours
cntrs = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cntrs = cntrs[0] if len(cntrs) == 2 else cntrs[1]

# Contour filtering and copy contour interior to new black image.
for c in cntrs:
    area = cv2.contourArea(c)
    if area > 1000:
        x,y,w,h = cv2.boundingRect(c)
        results[y:y+h,x:x+w] = thresh[y:y+h,x:x+w]

# invert the results image so that have black letters on white background
results = (255 - results)

# write results to disk
cv2.imwrite("numbers_extracted.png", results)

cv2.imshow("THRESH", thresh)
cv2.imshow("RESULTS", results)
cv2.waitKey(0)
cv2.destroyAllWindows()

轮廓过滤之前的阈值图像:

enter image description here

轮廓过滤和反演后的结果:

enter image description here

附言cv2.inRange()可以替代cv2.threshold。

当然,该解决方案可能仅限于该一张图像,因为其他图像可能需要不同的阈值和面积限制值。

关于python - 从图像中提取多个背景中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58573026/

相关文章:

c++ - OpenCV 错误 : "LINK : fatal error LNK1104: cannot open file ' opencv_core231d. 库'”

python - 相同值的饼图标签重叠。

c++ - 从openCV获取cvReleaseImage时出现错误

ionic-framework - 如何在 ionic 2 中从 api 响应返回的网络浏览器上显示图像

python-3.x - Python 脚本在到达 CV2 函数时卡住

python - 关于OpenCV resize's INTER_AREA working domain的问题(func != 0 && cn <= 4 in function 'cv::hal::resize' failure)

python - OpenCV ORB 描述符 : TypeError: Incorrect type of self (must be 'Feature2D' or its derivative)

python - 为什么 scikit-learn 的 RandomForestClassifier 在显式设置中不是确定性的?

python - 设置 NetworkX 边长

python - keras 中的 categorical_crossentropy 是如何实现的?