Search code examples
pythonopencvduplicatestemplate-matching

Check list for similar coordinates bevor adding entry


I am working on template matching using OpenCV. The matching itself yielded all templates, however the best fits are found in multiple slight variations before all templates are found. The template should be 12 times on the checked image. However the arrays for the x location xloc and the y location yloc have a length of 34.

locations = []
for (x,y) in zip (xloc,yloc) :
  same = False

  for (xn,yn) in locations :
    if x in range (xn, xn+w) : #w being the width of the template, around 40px
        if y in range (yn, yn+h) : # being the height
            same = True
    
  if not same :
     locations.append ([int(x), int(y)])

As you can see I tried to write a new list for the locations, only adding values that aren't too similar to values present in the list. This reduced the number of entries to 15, but there are still 3 duplicates. I highlighted the duplicate values in the screenshot. I wonder how they managed to slip through, even though they should be in the defined range. What's important for this project are the locations and number of the matches. Can anyone please explain me how to get rid of the 3 remaining duplicates?

jupyter screenshot


Solution

  • Thank you for the comments. Checking the negative range (-w and -h) solved the problem.

    Changed part:

     if x in range (xn-w, xn+w) :
            if y in range (yn-h, yn+h) :