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

时间:2014-09-16 18:22:22

标签: python opencv ocr

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

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)

我的图片与模板基本相同,我使用相同的字体和像素大小24x26制作了两个字,但我得到的只是我写的字符

 character = 'A'
谁能告诉我我做错了什么?

0 个答案:

没有答案