#图像截取与文字识别
# coding:utf8
import cv2
import numpy as np
import pytesseract
def match_img(image, Target, value):
img_rgb = cv2.imread(image)
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
template = cv2.imread(Target,0)
w, h = template.shape[::-1]
res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
threshold = value
loc = np.where( res >= threshold)
for pt in zip(*loc[::-1]):
print pt,(pt[0] + w, pt[1] + h)
get_loc_img(img_gray, pt[1], h, pt[0], w)
cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (7,249,151), 2)
cv2.imshow('Detected',img_rgb)
cv2.waitKey(0)
cv2.destroyAllWindows()
def get_loc_img(img_src, y, h, x, w):
# img = cv2.imread("./data/cut/thor.jpg")
print y, h, x, w
cropped = img_src[y:y+h, x:x+w] # 裁剪坐标为[y0:y1, x0:x1]
cv2.imwrite("./images/new.jpg", cropped)
roi = cv2.imread("./images/new.jpg")
tessdata_dir_config = '-l eng --oem 1 --psm 7 --tessdata-dir "C:\\Program Files (x86)\\Tesseract-OCR\\tessdata"'
text = pytesseract.image_to_string(roi, config=tessdata_dir_config)
print text
image=("./images/1.png")
Target=('./images/2.png')
value=0.9
match_img(image,Target,value)