This is a training project i am doing for a game to learn some OpenCV.
So I have this Image ,this Template image and this code:
from time import sleep
import pyautogui
import cv2
from tkinter import Tk
import re
img_rgb = pyautogui.screenshot()
img_rgb = np.array(img_rgb)
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
template = cv2.imread("template.png", 0)
w, h = template.shape[::-1]
res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
precision=0.85
loc = np.where(res >= precision)
count = 0
total_currency=0
for pt in zip(*loc[::-1]): # Swap columns and rows
cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), 2)
center_x=pt[0]+w/2
center_y=pt[1]+h/2
count+=1
total_currency+=get_stack() # function to get the sum of the numbers. In this case 4,10,5,10. No openCV is used here
cv2.imwrite('res.png',img_rgb)
print("Found",count,"Total currency: ",total_currency)
This is the output:
4
4
10
5
Found 4 Total currency: 23
and the image output .The problem is that it detects two times the 4 image,one time the 10 and one time the 5. Which i can't even understand why because i have two exactly same 10 images and only detects one.
I tried taking better template pictures, switching the precision without fixing it. What is the problem here?