import numpy as np
import cv2
from PIL import ImageGrab
from demo import get_XY, classify_hist_with_split
import time
cap = cv2.VideoCapture(0) # 开启摄像头
def find_picture(target, template):
theight, twidth = template.shape[:2]
result = cv2.matchTemplate(target, template, cv2.TM_SQDIFF_NORMED)
cv2.normalize(result, result, 0, 1, cv2.NORM_MINMAX, -1)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
cv2.rectangle(target, min_loc, (min_loc[0] + twidth, min_loc[1] + theight), (0, 0, 225), 2)
x = min_loc[0]
y = min_loc[1]
return x, y
def get_xy(A, base='image.jpg'):
"""获取A在B的坐标, 反返回A的高和宽"""
target = cv2.imread(A)
w, h, = target.shape[0], target.shape[1] # 图片的高宽
if base == 'image.jpg':
template = cv2.imread(base)
else:
template = base
x, y = find_picture(target, template)
print(A, "的坐标", x, y, h, w)
return x, y, w, h
def get_pic_from_pic(x, y, w, h, target):
region = target[y:y + h, x:x + w]
return region
if __name__ == '__main__':
# x, y, h, w = get_xy("yanjing.jpg", "image.jpg")
#
# human_box = [x, y, w+x, h+y]
while True:
screen = ImageGrab.grab(bbox=(0, 0, 800, 600))
print(screen)
frame = cv2.cvtColor(np.array(screen), cv2.COLOR_RGB2BGR)
# cv2.imshow("threshold", frame) # 显示图像
x, y, h, w = get_xy("yanjing.jpg", frame)
human_box = [x, y, x + w, h + y]
# cv2.waitKey(25)
# img = cv2.imread(image_path)
# img = cv2.imread("image.jpg")
cv2.rectangle(frame, (human_box[0], human_box[1]), (human_box[2], human_box[3]), (255, 255, 0), 3) #
cv2.imshow("output_path", frame)
cv2.waitKey(25)
标签:loc,target,min,cv2,template,openCV,human,图片
From: https://www.cnblogs.com/zxyg/p/16834654.html