我正在尝试识别用卡通字体编写的数字,因此传统工具(例如pytesser)并没有真正起作用。但是,我能够使用cv2.matchTemplate和10位数的库获得非常准确的单位数识别。我基本上将每个数字视为一个符号,只是试图找到最佳匹配。
import cv2
import numpy as np
def match_char(im1, im2):
result = cv2.matchTemplate(im1,im2,cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
return max_va
def process_image(im, i):
try:
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
a, t = cv2.threshold(gray, 220, 255, cv2.THRESH_BINARY_INV)
return t
except:
print("Error while processing the image")
pass
def process_lib(im, i):
try:
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
a, t = cv2.threshold(gray, 220, 255, cv2.THRESH_BINARY)
return t
except:
print("Error while processing the image")
pass
def process_num():
test = cv2.imread('test.png')
test = process_image(test, 11)
test = cv2.resize(test, (0,0), fx=2.2, fy=2.2) #best
scores = []
for i in range(10):
img = cv2.imread('char-{}.png'.format(i))
img = process_lib(img, i)
scores.append(match_char(img, test))
win = scores.index(max(scores))
print("The best match is {} with {}".format(win, scores[win]))
问题是我无法将其扩展为多位数字。即使生成两位数字并尝试匹配它们,程序也始终将单个数字标识为最佳匹配。如果我将搜索限制为两位数字,它有时会得到一个数字正确,但从不两者都有。当然,这不是最优雅的解决方案,但它似乎最简单,甚至不起作用。
答案 0 :(得分:1)
您应该定义一个阈值并过滤此阈值下面的cv2.matchTemplate
结果
我认为以下代码适合您:
import cv2 as cv
import numpy as np
img_rgb = cv.imread('image.png')
img_gray = cv.cvtColor(img_rgb, cv.COLOR_BGR2GRAY)
template = cv.imread('template.png',0)
w, h = template.shape[::-1]
res = cv.matchTemplate(img_gray,template,cv.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where( res >= threshold)
for pt in zip(*loc[::-1]):
cv.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
cv.imwrite('res.png',img_rgb)
了解更多信息see