我有多张背景不同的图像
我需要忽略背景,并从我的图像中提取Number。例如:
经过测试,我得到了这个结果:
由于背景色,提取文本非常困难。
我正在使用以下代码:
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()
即使背景颜色发生变化,我如何也能从该图像中准确提取数字?
修改后编辑结果:
答案 0 :(得分:2)
您的门槛是您的问题。在执行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()
轮廓过滤之前的阈值图像:
轮廓过滤和反演后的结果:
P.S。 cv2.inRange()可以替代cv2.threshold。
当然,此解决方案可能仅限于一张图片,因为其他图片可能需要不同的阈值和面积限制值。