dyn-object-masks
Generate dynamic-object binary masks after global motion compensation, output CSR sparse format.
- License 许可
- Apache-2.0
- 最近更新
- 2026-11-14
- Stars
- ★ 0
- Forks
- 1
01 前言 Front Matter
- name
- dyn-object-masks
- description
- Generate dynamic-object binary masks after global motion compensation, output CSR sparse format.
03 SKILL.md 正文
When to use
- Detect moving objects in scenes with camera motion; produce sparse masks aligned to sampled frames.
Key failure modes to avoid
Two extremes both destroy IoU and must be actively guarded against:
- Flooded masks (foreground fraction ≳ 10% of the image on most frames): usually caused by residual global-motion misalignment (parallax under dolly/large translation, or an affine model where a homography is needed). The whole scene lights up.
- Empty masks (foreground fraction ≈ 0 on most frames): usually caused by an overly aggressive threshold, over-erosion, or a minimum-area filter that is too large.
Aim for per-frame foreground fraction roughly in the 0.2%–8% band for typical scenes with one or a few moving objects. Treat sustained values outside [0.05%, 15%] as a red flag and re-tune before submitting.
Workflow
- Global alignment: warp previous gray frame to current using an estimated transform.
- Default to homography (
cv2.findHomographywith RANSAC) rather than affine when the camera translates significantly (dolly in/out, walking POV), because affine cannot cancel parallax-induced apparent motion of the background. - Fall back to
estimateAffinePartial2Donly if homography estimation fails or produces a near-degenerate matrix.
- Default to homography (
- Valid region: also warp an all-ones mask to get
validpixels; erode the valid mask by a few pixels to discard border/interpolation artifacts before differencing. - Difference + adaptive threshold:
diff = abs(curr - warp_prev); ondiff[valid_eroded]computemedian + k·1.4826·MADwithkin[3, 5]; enforce a reasonable minimum threshold (e.g.>= 15) to avoid triggering on sensor noise. A light Gaussian blur on both frames before the diff reduces sub-pixel misalignment noise. - Morphology + area filter: open (3×3 or 5×5) then close (7×7 to 11×11); keep connected components above a minimum area. Suggested starting range:
min_areabetween0.05%and0.3%ofH*W(e.g. ~460–2800 px for 720p). Largermin_arearemoves noise but also small real objects — do not push past~0.5%without evidence. - Coverage check (mandatory): after building each frame's mask, compute
fg_frac = mask.mean().- If the mean
fg_fracacross sampled frames is> ~15%, alignment is failing — switch/verify homography, blur more, or raise thresholdk. - If the mean
fg_fracis< ~0.05%on scenes that clearly contain motion, threshold ormin_areais too aggressive — lower them. - Do not submit before this loop stabilizes.
- If the mean
- CSR encoding: for the final bool mask
rows, cols = nonzero(mask)indices = cols.astype(int32);data = ones(nnz, uint8)counts = bincount(rows, minlength=H);indptr = cumsum(counts, prepend=0).astype(int32)- store as
f_{i}_data/indices/indptr
Code sketch
# 1) Global alignment via homography (parallax-tolerant); affine fallback.
p0 = cv2.goodFeaturesToTrack(prev_gray, maxCorners=2000, qualityLevel=0.01,
minDistance=8, blockSize=7)
p1, st, _ = cv2.calcOpticalFlowPyrLK(prev_gray, curr_gray, p0, None,
winSize=(21,21), maxLevel=3)
g0 = p0[st.reshape(-1)==1]; g1 = p1[st.reshape(-1)==1]
Hmat, _ = cv2.findHomography(g0, g1, cv2.RANSAC, 3.0)
if Hmat is not None:
warped_prev = cv2.warpPerspective(prev_gray, Hmat, (W,H), flags=cv2.INTER_LINEAR, borderValue=0)
valid = cv2.warpPerspective(np.ones((H,W),np.uint8), Hmat, (W,H),
flags=cv2.INTER_NEAREST) > 0
else:
# affine fallback ...
pass
valid = cv2.erode(valid.astype(np.uint8),
cv2.getStructuringElement(cv2.MORPH_RECT,(7,7))) > 0
curr_b = cv2.GaussianBlur(curr_gray, (5,5), 0)
prev_b = cv2.GaussianBlur(warped_prev, (5,5), 0)
diff = cv2.absdiff(curr_b, prev_b)
vals = diff[valid]
med = float(np.median(vals)); mad = float(np.median(np.abs(vals-med)))
thr = max(15.0, med + 3.5*1.4826*mad) # tune k in [3,5]
raw = (diff > thr) & valid
k3 = cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))
k7 = cv2.getStructuringElement(cv2.MORPH_RECT,(7,7))
m = cv2.morphologyEx(raw.astype(np.uint8)*255, cv2.MORPH_OPEN, k3)
m = cv2.morphologyEx(m, cv2.MORPH_CLOSE, k7)
n, cc, stats, _ = cv2.connectedComponentsWithStats((m>0).astype(np.uint8), connectivity=8)
min_area = max(200, int(0.0008*H*W)) # ~0.08% of image; tune in [0.05%, 0.3%]
mask = np.zeros((H,W), dtype=bool)
for cid in range(1,n):
if stats[cid, cv2.CC_STAT_AREA] >= min_area:
mask |= (cc==cid)
fg_frac = float(mask.mean()) # inspect across all frames; retune if extreme
Tuning loop (do this before submitting)
- Run the pipeline on all sampled frames and record
fg_fracper frame. - Compute
meanandmaxoffg_frac. - If
mean > 0.15or many frames > 0.25: alignment/threshold too permissive → use homography (not affine), blur more, or raise thresholdkormin_area. - If
mean < 0.001and the video clearly contains moving objects: too strict → lower thresholdk, reducemin_area, shrink morphology closing. - Only submit when the distribution is plausible (typical band
0.2%–8%).
Self-check
- [ ] Masks only for sampled frames; keys match sampled indices.
- [ ]
shapestored as[H, W]int32;len(indptr)==H+1;indptr[-1]==indices.size. - [ ] Border fill not treated as foreground; threshold stats computed on eroded valid region only.
- [ ] Threshold + morphology + area filter applied.
- [ ] Homography used when camera translates significantly; affine only as fallback.
- [ ] Per-frame foreground fraction inspected; not near-zero and not near-total on scenes with motion.
04 同仓库更多 Skills
video-frame-extraction
Extract frames from video files and save them as images using OpenCV
openai-vision
Analyze images and multi-frame sequences using OpenAI GPT vision models
image-ocr
Extract text content from images using Tesseract OCR via Python, including structured field extraction from receipts and forms
video-frame-extraction
Extract frames from video files and save them as images using OpenCV
openai-vision
Analyze images and multi-frame sequences using OpenAI GPT vision models
image-ocr
Extract text content from images using Tesseract OCR via Python, including structured field extraction from receipts and forms