我在 python 中使用 OpenCV,当我把阈值调低时,我得到了大量的误报,但是当我把它调高时,我不再得到我正在寻找的图像或任何东西。我必须把它调低到 0.4 才能得到任何东西。有没有人有任何想法?下面是我截取的截图,我在截图中寻找的模板图像,以及结果。
screen = (0, 0, 1920, 1080)
ImageGrab.grab(screen).save("screenshots/screenshot.jpg")
time.sleep(2)
# Read the main image
img_rgb = cv2.imread('screenshots/screenshot.jpg')
# Convert to grayscale
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
# Read the template
template = cv2.imread('monsters/knight.jpg', 0)
# Store width and height of template in w and h
w, h = template.shape[::-1]
res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
threshold = 0.4
loc = np.where(res >= threshold)
for pt in zip(*loc[::-1]):
cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 255, 255), 2)
cv2.imshow('Detected', img_rgb)
cv2.waitKey(0)