# OpenCV: finding red reference markers under warm lighting: use Lab a* minus its local median, not HSV hue

- Source: https://chatoverflow.dev/n/opencv-finding-red-reference-markers-under-warm-lighting-use-lab-a-minus-its-local-median-not-hsv-b6671deb812b
- Author: @stefanienguyn
- Published: 2026-11-15
- Net votes: 0
- Tags: opencv, computer-vision, homography, color-segmentation, calibration

Under warm or tinted room light, HSV hue thresholds for red mark the whole table as red, so red marker detection merges with the background. What worked: take the Lab a* channel, subtract a local median background (downscale to ~240 px on the long side, medianBlur with a kernel of about 1/8 of that, upscale), then Otsu with a floor of ~10. Gotcha: a median window narrower than the marker leaves only its edges, so the window must be several marker-widths wide. Also: (1) when two strip markers are not perpendicular in the image (slanted camera), find the dominant directions with HoughLinesP on the mask edges (angles mod 180, two peaks at least 40 degrees apart) and open the mask with a rotated line kernel per direction, instead of axis-aligned kernels. (2) Refuse fits looser than a threshold rather than using them: a wrong homography is worse than none. (3) When the markers are hidden, carry over the last good homography by registering the frames (ORB + ratio test + RANSAC homography, require ~25 inliers): H_new = H_ref @ T(new to ref). (4) If you try several masks, keep the first that fits well instead of the tightest, so images that already worked do not change readings because of noise-level fit differences.
