Skip to content

Instantly share code, notes, and snippets.

@warmspringwinds
Created July 9, 2015 08:47
Show Gist options
  • Save warmspringwinds/12017d40d358ed03125c to your computer and use it in GitHub Desktop.
Save warmspringwinds/12017d40d358ed03125c to your computer and use it in GitHub Desktop.
Excessive detection window elimination.
def intersection_ratio(rect_a, rect_b):
r_a_1 = rect_a["r"]
r_a_2 = rect_a["r"] + rect_a["height"]
c_a_1 = rect_a["c"]
c_a_2 = rect_a["c"] + rect_a["width"]
r_b_1 = rect_b["r"]
r_b_2 = rect_b["r"] + rect_b["height"]
c_b_1 = rect_b["c"]
c_b_2 = rect_b["c"] + rect_b["width"]
area_a = rect_a["height"] * rect_a["width"]
area_b = rect_b["height"] * rect_b["width"]
intersection_area = max(0, min(c_a_2, c_b_2) - max(c_a_1, c_b_1)) * max(0, min(r_a_2, r_b_2) - max(r_a_1, r_b_1))
union_area = area_a + area_b - intersection_area
smaller_area = area_a if area_b > area_a else area_b
ratio = float(intersection_area) / smaller_area
return ratio
clusters = []
clusters.append([res[0], 1])
for current_rect in res[1:]:
new_cluster = True
for cluster in clusters:
if intersection_ratio(current_rect, cluster[0]) > 0.5:
cluster[0]["r"] = (cluster[0]["r"] + current_rect["r"]) / 2.0
cluster[0]["c"] = (cluster[0]["c"] + current_rect["c"]) / 2.0
cluster[0]["width"] = (cluster[0]["width"] + current_rect["width"]) / 2.0
cluster[0]["height"] = (cluster[0]["height"] + current_rect["height"]) / 2.0
cluster[1] += 1
new_cluster = False
if new_cluster:
clusters.append([current_rect, 1])
clusters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment