37,744
社区成员




import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('d:/学习办公盘/python学习代码/opencv/捕获.png')
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
template = cv2.imread('d:/学习办公盘/python学习代码/opencv/text3.jpg')
w= template.shape[0]
w= template.shape[1]
res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where( res >= threshold)
for pt in zip(*loc[::-1]):
cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
#cv2.imwrite('res.png',img_rgb)
cv2.imshow('res',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 08 17:15:22 2017
@author: SGA-ligure
"""
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('E:\\ligure\\code\\py\\data\\img\\ori.png',0)
temp = cv2.imread('E:\\ligure\\code\\py\\data\\img\\temp.png',0)
img2 = img.copy()
w,h = temp.shape[::-1]
method = eval('cv2.TM_CCOEFF_NORMED')
res = cv2.matchTemplate(img,temp,method)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
top_left = min_loc
else:
top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
cv2.rectangle(img2,top_left, bottom_right, 255, 2)
plt.subplot(221), plt.imshow(img,cmap= "gray")
plt.title('Original Image'), plt.xticks([]),plt.yticks([])
plt.subplot(222), plt.imshow(temp,cmap= "gray")
plt.title('template Image'),plt.xticks([]),plt.yticks([])
plt.subplot(223), plt.imshow(res,cmap= "gray")
plt.title('Matching Result'), plt.xticks([]),plt.yticks([])
plt.subplot(224), plt.imshow(img2,cmap= "gray")
plt.title('Detected Point'),plt.xticks([]),plt.yticks([])
plt.show()
void cv::matchTemplate( InputArray image,
InputArray templ,
OutputArray result,
int method,
InputArray mask = noArray()
)
参考:
http://docs.opencv.org/master/df/dfb/group__imgproc__object.html#ga586ebfb0a7fb604b35a23d85391329be