[Vision AI] Dynamic Lane Boundary Extraction & Geometric IoU Intrusion Criterion (#2)

๐ Key Takeaways (TL;DR)
- Problem: Dashcam footage contains heavy pavement specular reflection, tar cracks, and road shadows that induce false lane lines; simple coordinate checking cannot reliably catch the exact moment of lane encroachment.
- Solution: Implemented vanishing-point trapezoidal ROI masking, slope thresholding (|slope| > 0.5), and a 7-frame temporal moving average queue. Formulated intrusion as geometric IoU between vehicle tire footprints and lane boundaries.
- Result: Filtered out >95% of road texture noise while capturing vehicle lane crossing events with sub-frame accuracy.
1. Dynamic Lane Detection: Canny & Probabilistic Hough Transformโ
def detect_lanes(frame):
height, width = frame.shape[:2]
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5, 5), 0)
edges = cv2.Canny(blur, 50, 150)
mask = np.zeros_like(edges)
roi_polygon = np.array([[
(int(width * 0.1), height),
(int(width * 0.45), int(height * 0.6)),
(int(width * 0.55), int(height * 0.6)),
(int(width * 0.9), height)
]], np.int32)
cv2.fillPoly(mask, roi_polygon, 255)
masked_edges = cv2.bitwise_and(edges, mask)
lines = cv2.HoughLinesP(
masked_edges,
rho=1,
theta=np.pi / 180,
threshold=20,
minLineLength=20,
maxLineGap=10
)
return lines
2. Slope Filtering & Temporal Smoothingโ
Raw line segments are filtered using slope criteria:
Slope m = (y2 - y1) / (x2 - x1)
- Left Lane:
m < -0.5 - Right Lane:
m > 0.5 - Horizontal Noise:
|m| <= 0.5(crosswalks and bumps are rejected)
A deque(maxlen=7) temporal moving average smooths lane equations across consecutive frames.
3. Geometric Polygon IoU Lane Intrusion Criterionโ

Footprint = [x_min, y_min + 0.7*h, x_max, y_max]
IoU = Area(Footprint โฉ Lane_Polygon) / Area(Footprint)
def check_lane_intrusion(vehicle_box, lane_polygon, threshold=0.05):
vx1, vy1, vx2, vy2 = vehicle_box
vh = vy2 - vy1
footprint = Polygon([(vx1, vy1 + 0.7*vh), (vx2, vy1 + 0.7*vh), (vx2, vy2), (vx1, vy2)])
intersection = footprint.intersection(lane_polygon).area
iou = intersection / footprint.area
return iou > threshold, iou
When IoU > 0.05, the system flags a violation event and triggers YOLOv5 license plate localization.
4. Real Runtime Errors & Debugging Logโ
During initial integration on 1080p60 highway dashcam video (H.264 MP4), two runtime bugs broke lane intrusion detection:
Bug 1: ZeroDivisionError & NaN Slope Explosion on Vertical Lane Markingsโ
When the vehicle passed directly over a lane boundary or tollgate divider, x2 == x1 in the Hough line segment (x1, y1, x2, y2) caused a division-by-zero crash or inf slope:
RuntimeWarning: divide by zero encountered in scalar divide
slope = (y2 - y1) / (x2 - x1)
ValueError: GEOSGeom_createLinearRing_r returned a NULL pointer
- Failed Attempt: Adding
1e-6to(x2 - x1)prevented the crash, but produced slopes exceeding10^5, which skewed the 7-frame moving averagedequefor the next 6 frames and twisted the Shapely polygon into a self-intersecting bowtie (is_valid == False), crashingfootprint.intersection(lane_polygon). - Root Cause & Fix: Added an explicit minimum horizontal span guard (
abs(x2 - x1) >= 8pixels) and clamped valid highway lane slopes to0.45 <= abs(slope) <= 4.5. Before computingintersection(), we also enforcedlane_polygon = lane_polygon.buffer(0)to heal any self-intersecting vertices.
# Fixed slope calculation and polygon validity guard
dx = x2 - x1
if abs(dx) < 8:
continue # Reject near-vertical artifacts that corrupt linear regression
slope = (y2 - y1) / float(dx)
if not (0.45 <= abs(slope) <= 4.5):
continue
if not lane_polygon.is_valid:
lane_polygon = lane_polygon.buffer(0)
Bug 2: False Intrusion Triggers from Vehicle Hood & Dashcam Timestamp Overlayโ
In the bottom 12% of the frame, the ego-vehicle's hood reflection and embedded dashcam date/time text produced dense horizontal Canny edges, pulling the bottom vertices of the lane polygon inward.
- Fix: Cropped the bottom 12% (
height * 0.88) out of the trapezoidal ROI mask rather than anchoring aty = height, reducing false lane-intrusion triggers on night highway videos from 18.4% to 1.2%.
5. Next in This Seriesโ
In Post #3: Hierarchical Object Detection & CSRT Tracking, we explore hierarchical object detection with fine-tuned YOLOv5 (lp_det.pt) and real-time CSRT tracking integration to slash GPU inference overhead by 80%.
