我有一個資產的 png,說:

我試圖在我的螢屏上找到它,看起來像:

通常,我會這樣使用 pyautoGUI:
pyautogui.locateCenterOnScreen('banana.png', grayscale=True, confidence=0.9)
但它目前不起作用。看來問題可能出在我的香蕉資產的透明像素上,這顯然不匹配。有沒有辦法通過忽略香蕉資產的透明像素并將它們視為通配符來做到這一點?還是另一種實作方式?
到目前為止,在我的搜索中,我發現這個
模板:

import cv2
import numpy as np
# read game image
img = cv2.imread('game.png')
# read bananas image template
template = cv2.imread('bananas.png', cv2.IMREAD_UNCHANGED)
hh, ww = template.shape[:2]
# extract bananas base image and alpha channel and make alpha 3 channels
base = template[:,:,0:3]
alpha = template[:,:,3]
alpha = cv2.merge([alpha,alpha,alpha])
# do masked template matching and save correlation image
correlation = cv2.matchTemplate(img, base, cv2.TM_CCORR_NORMED, mask=alpha)
# set threshold and get all matches
threshhold = 0.95
loc = np.where(correlation >= threshhold)
# draw matches
result = img.copy()
for pt in zip(*loc[::-1]):
cv2.rectangle(result, pt, (pt[0] ww, pt[1] hh), (0,0,255), 1)
print(pt)
# save results
cv2.imwrite('bananas_base.png', base)
cv2.imwrite('bananas_alpha.png', alpha)
cv2.imwrite('game_bananas_matches.jpg', result)
cv2.imshow('base',base)
cv2.imshow('alpha',alpha)
cv2.imshow('result',result)
cv2.waitKey(0)
cv2.destroyAllWindows()
結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/435695.html
上一篇:抗鋸齒圓在opencv中呈鋸齒狀
