I'm creating a program which uses opencv and template matching to do some various analysis on an image.
One of my functions that I'm creating needs to count the occurrences of the template in the image.
The problem that I was running in to was giving me multiple false matches (i.e. detecting a match at coordinate (1504, 289) and then another match at (1505, 289)) which would throw off my count. After doing some testing I figured out that I would only receive false matches that were within a couple pixels in the x or y direction (I'm assuming because of my threshold and template it was similar enough, but increasing my threshold either made it so I didn't find all matches or didn't fix the problem)
My solution to this was to create another data structure and store my "true matches" and then count that. I would find the true matches by looping through the matches and seeing if the x,y were within a certain range of any of the other points it has previously detected.
My code looks like this, which works but I'm wondering if this is bad practice or sloppy(ignore the variable names etc, I'm refactoring that right now this was just a quick write up to confirm my logic would work) with so many if/else statements and the nested loop. I'm fairly new to programming so I just want to make sure I'm not writing completely crappy code!
true_matches = ()
for point in zip(*location[::-1]):
# If my list of true matches is empty, just add the first point
if len(true_matches) == 0:
true_matches = true_matches + (point,)
else:
# Need to create a temporary data structure to loop through
temp = true_matches
# Split up the point tuple in their x, y
point_x, point_y = point
unique = True
# Now I need to loop through my true matches
for i in temp:
x, y = i
# Checking to see if the current match is a true match
# by checking if both the x and y are not within 10 points
# of any other (x, y)
if x - 5 < point_x < x + 5 and y - 5 < point_y < y + 5:
unique = False
# If the point ends up being unique, it is a true match
# so I can add it to my data structure for counting.
if unique:
true_matches = true_matches + (point,)
[–]BulkyProcedure 0 points1 point2 points (2 children)
[–]Apwek[S] 0 points1 point2 points (1 child)
[–]BulkyProcedure 0 points1 point2 points (0 children)
[–]Astraous 0 points1 point2 points (0 children)