【问题标题】:Extract text in multiple background from image从图像中提取多个背景中的文本
【发布时间】:2019-10-26 17:19:48
【问题描述】:

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

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

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

因为背景色,提取文字非常困难..

我正在使用此代码:

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()

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

修改后的编辑结果:

【问题讨论】:

  • 你的阈值不是很好。删除gray[gray &lt; 100] = 255。在 165 处尝试 cv2.threshold。然后获取轮廓并移除太小的轮廓。然后将 thresh 图像反转为 (255 - thresh),使字母在白色背景上显示为黑色。
  • @fmw42,你说得对,灰色,我已经用你在最后一张图片上的参数更新了我的第一篇文章,我丢失了图片顶部的文本数字“1”,我该如何解决?跨度>

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


【解决方案1】:

您的阈值是您的问题。这是我在进行 OCR 之前在 Python/OpenCV 中处理图像的方法。

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

输入:

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()


轮廓过滤前的阈值图像:

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

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

当然,此解决方案可能仅限于这一图像,因为其他图像可能需要不同的阈值和区域限制值。

【讨论】:

  • 好的,谢谢我现在明白我的问题了!我将测试数字的清洁轮廓以获得更好的 OCR 效果,因为在另一张图像上检测到“i”。
  • @frmw42,实际上阈值为 160,OCR 工作正常!我不知道是否可以自动测试多个值以获得更好的阈值精度?
  • 抱歉,OCR 超出了我的范围。我从来没有这样做过。我想您可以在脚本中编写一个循环并改变阈值,执行 OCR 并查看哪个结果最好。也许,尝试使用 cv2.inRange 代替简单的阈值,并在您的许多图像上对其进行测试,以查看您需要的范围。此外,一个或所有通道上的 HSV 颜色空间中的阈值或 inRange 可能值得测试。这可能有助于消除彩色区域。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-29
  • 1970-01-01
  • 2015-07-31
  • 2020-04-21
  • 2015-08-09
  • 1970-01-01
相关资源
最近更新 更多