Live adaptive threshold — computer vision in the browser

Adaptive thresholding on a live webcam feed — a computer-vision segmentation step that picks a per-region cutoff, so it copes with uneven lighting where a single global threshold fails. It runs OpenCV’s cv2.adaptiveThreshold on each frame. Press Run and allow the camera.

Live adaptive threshold — example output

Example code (Python / OpenCV)

import cv2

# The camera starts automatically on Run — allow access when prompted.
cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    if not ret:
        print("No camera frame — allow camera access when prompted.")
        break

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray, (5, 5), 0)
    cv2.imshow("threshold", cv2.adaptiveThreshold(
        blur, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2))

    if cv2.waitKey(1) == 27:  # Esc / Stop
        break

More computer-vision techniques →