Filter x,y coordinates if x or y greater/less than N

Viewed 188

I'm struggling to find the right algorithm to filter a list of x,y coordinates (source : cv2.matchTemplate).

My data is like: [(552, 429), (553, 429), (457, 477), (458, 478), (1208, 671), (1209, 671), (467, 712), (468, 712)]

These coordinates are useful to draw rectangles to locate where template is found in a picture but I need to "click" on this template so I need only one angle. I want to keep only coordinates whose distance is greater or less than N from all other points.

In this picture, I want to keep only red dots coordinates not blue rectangle coordinates.

enter image description here

2 Answers

I understand this is the code block you needed.

N = 5
result = []
removed = []
for index, (x, y) in enumerate(data):
    if not (x, y) in result and not (x, y) in removed:
        removed += list(filter(lambda c: abs(c[0] - x) < N, data))
        result.append((x, y))
print(result)

I beleive it should work:

data = [(552, 429), (553, 429), (457, 477), (458, 478), (1208, 671), (1209, 671), (467, 712), (468, 712)]
result = []
for item in data:
  topLeftFilter = lambda n: n[0] == item[0] and n[1] == item[1] - 1 or n[0] == item[0] - 1 and n[1] == item[1] or n[0] == item[0] - 1 and n[1] == item[1] - 1
  topLeftPoint = list(filter(topLeftFilter, data))
  if len(topLeftPoint) == 0:
    result.append(item)

print(result)    
Related