1

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?

Takis Pan
  • 35
  • 6
  • 1
    Many times it helps to plot the `res` response surface. This can help debug. You probably should do some sort of non-max suppression. Look [here](https://stackoverflow.com/a/21023493/13253198) for a sample implementation. – gnodab May 01 '20 at 13:50
  • 1
    Your template is too similar to other parts. It looks like the top of the regions in the input, but it would match at a high value to the bottom also. You might get better match results if you use a template that is the full size of the non-black icons in the input. Also it might give better results if you masked out the black parts using the full size of the cells you want to detect as the template. – fmw42 May 01 '20 at 18:39

0 Answers0