Skip to content

Instantly share code, notes, and snippets.

@shoaibmehedi7
Created July 15, 2021 19:56
Show Gist options
  • Save shoaibmehedi7/aab2865ae81a225284ad9b0bb81afcdc to your computer and use it in GitHub Desktop.
Save shoaibmehedi7/aab2865ae81a225284ad9b0bb81afcdc to your computer and use it in GitHub Desktop.
while ret:
d = cv2.absdiff(frame1, frame2)
grey = cv2.cvtColor(d, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(grey, (5, 5), 0)
ret, th = cv2.threshold(blur, 20, 255, cv2.THRESH_BINARY)
dilated = cv2.dilate(th, np.ones((3, 3)))
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2, 2))
closing = cv2.morphologyEx(dilated, cv2.MORPH_CLOSE, kernel)
contours, h = cv2.findContours(
closing, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for(i, c) in enumerate(contours):
(x, y, w, h) = cv2.boundingRect(c)
contour_valid = (w >= min_contour_width) and (
h >= min_contour_height)
if not contour_valid:
continue
cv2.rectangle(frame1, (x-10, y-10), (x+w+10, y+h+10), (255, 0, 0), 2)
cv2.line(frame1, (0, line_height), (1200, line_height), (0, 255, 0), 2)
centrolid = get_centrolid(x, y, w, h)
matches.append(centrolid)
cv2.circle(frame1, centrolid, 5, (0, 255, 0), -1)
cx, cy = get_centrolid(x, y, w, h)
for (x, y) in matches:
if y < (line_height+offset) and y > (line_height-offset):
cars = cars+1
matches.remove((x, y))
cv2.putText(frame1, "Total Cars Detected: " + str(cars), (10, 90), cv2.FONT_HERSHEY_SIMPLEX, 1,
(0, 170, 0), 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment