Contour detection — computer vision in the browser
Contour detection finds the connected outlines of shapes in a binary image — a building block of many computer-vision pipelines, from counting objects to measuring their size. OpenCV’s cv2.findContours walks the boundaries and cv2.drawContours draws them. Run it live and tweak the threshold to see how the contours change.
Example code (Python / OpenCV)
import cv2
import numpy as np
img = np.zeros((300, 400, 3), dtype=np.uint8)
cv2.rectangle(img, (40, 60), (160, 220), (255, 255, 255), -1)
cv2.circle(img, (290, 150), 70, (255, 255, 255), -1)
cv2.imshow("shapes", img)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
out = img.copy()
cv2.drawContours(out, contours, -1, (0, 0, 255), 3)
cv2.imshow("contours", out)
print(f"Found {len(contours)} contours.")