def recognizeDigit(digit, method = REC_METHOD_TEMPLATE_MATCHING, threshold= 55):
"""
Finds the best match for the given digit(RGB or gray color scheme). And returns the result and percentage as an integer.
@threshold percentage of similarity
"""
__readDigitTemplates()
digit = digit.copy()
if digit.shape[2] == 3:
digit = cv2.cvtColor(digit, cv2.COLOR_RGB2GRAY)
ret, digit = cv2.threshold(digit, 90, 255, cv2.THRESH_BINARY_INV)
bestDigit = -1
if method == REC_METHOD_TEMPLATE_MATCHING:
bestMatch = None
for i in range(len(__DIGIT_TEMPLATES)):
template = __DIGIT_TEMPLATES[i].copy()
if digit.shape[1] < template.shape[1]:
template = cv2.resize(template, (digit.shape[1], digit.shape[0]))
else:
digit = cv2.resize(digit, (template.shape[1], template.shape[0]))
result = cv2.matchTemplate(digit, template, cv2.TM_CCORR_NORMED)#cv2.TM_CCOEFF_NORMED)
(_, max_val, _, max_loc) = cv2.minMaxLoc(result)
if bestMatch is None or max_val > bestMatch:
bestMatch = max_val
bestDigit = i
print("New Best Match:", bestMatch, bestDigit)
if (bestMatch * 100) >= threshold:
return (bestDigit, bestMatch * 100)
return (-1, 0)
评论列表
文章目录