Skip to content

Instantly share code, notes, and snippets.

@creotiv
Created September 11, 2017 18:42
Show Gist options
  • Select an option

  • Save creotiv/cf6979d7cb4ae7f78200cc815b9ef38d to your computer and use it in GitHub Desktop.

Select an option

Save creotiv/cf6979d7cb4ae7f78200cc815b9ef38d to your computer and use it in GitHub Desktop.
def get_centroid(x, y, w, h):
x1 = int(w / 2)
y1 = int(h / 2)
cx = x + x1
cy = y + y1
return (cx, cy)
def detect_vehicles(fg_mask, min_contour_width=35, min_contour_height=35):
matches = []
# finding external contours
im, contours, hierarchy = cv2.findContours(
fg_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_L1)
# filtering by with, height
for (i, contour) in enumerate(contours):
(x, y, w, h) = cv2.boundingRect(contour)
contour_valid = (w >= min_contour_width) and (
h >= min_contour_height)
if not contour_valid:
continue
# getting center of the bounding box
centroid = get_centroid(x, y, w, h)
matches.append(((x, y, w, h), centroid))
return matches
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment