如何在opencv python中进行模板匹配OCR

如何在opencv python中进行模板匹配OCR,python,opencv,ocr,Python,Opencv,Ocr,iam现在正在python opencv中使用cv2.matchTemplate进行简单的字符识别和模板匹配。到目前为止,这只是我的代码匹配过程: import numpy as np import cv2 im = cv2.imread('test.png') imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) thresh = cv2.adaptiveThreshold(imgray, 255, 1, 1, 11, 2) contours, hier

iam现在正在python opencv中使用cv2.matchTemplate进行简单的字符识别和模板匹配。到目前为止,这只是我的代码匹配过程:

import numpy as np
import cv2

im = cv2.imread('test.png')
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
thresh = cv2.adaptiveThreshold(imgray, 255, 1, 1, 11, 2)

contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)

for contour in contours:
if cv2.contourArea(contour) > 50:# remove the small contour
 [x, y, w, h] = cv2.boundingRect(contour)
 if h > 20:
   cv2.rectangle(im, (x, y), (x+w, y+h), (0, 0, 255), 1)
   print x, y, w, h
   cropped = im[y:y+h, x:y+h]#crop the roi region

 res = 0
 character = 0
这是我混淆的部分,模板匹配部分:

 imref = cv2.imread('a.jpg')#reads the template image
 result = cv2.matchTemplate(cropped, imref, cv2.TM_CCORR_NORMED)
 res = result
 character = 'A'
 print "match"+character


 cv2.imshow('im',im)
 cv2.waitKey(0)
我的图片与模板基本相同,我在word中使用相同的字体和像素大小(24x26),但我得到的只是我写的字符

 character = 'A'

谁能告诉我我做错了什么吗?

你漏了一步。你必须在结果图像上使用minMaxLoc,然后决定使用距离,如果它是匹配的还是不匹配的。对不起,berak,我没有键入它应该是“a.jpg”,然后再次强调,鉴于现有字体的数量等,字符识别决不简单。如果你认为,你可以使用普通模板匹配进行ocr-不可能。你是说像这样吗“min_val,max_val,min_loc,max_loc=cv2.minMaxLoc(结果)”?然后如何决定使用距离并决定其是否匹配?抱歉,因为我是这个opencv python的新手,请从现在开始!