OpenCV: Issues with Optical Flow Calculation in Low-Light Conditions
I've searched everywhere and can't find a clear answer. I'm working on an optical flow application using OpenCV (version 4.5.1) to track moving objects in low-light conditions, and I'm encountering significant inaccuracies in flow calculations. While the implementation works flawlessly in well-lit environments, the results in dim lighting seem unreliable, with many of the detected motion vectors being either missing or significantly skewed. Iām using the `calcOpticalFlowFarneback` function like this: ```python import cv2 import numpy as np cap = cv2.VideoCapture('video_low_light.mp4') # Parameters for ShiTomasi corner detection feature_params = { 'maxCorners': 100, 'qualityLevel': 0.3, 'minDistance': 7, 'blockSize': 7 } # Parameters for optical flow lk_params = { 'winSize': (15, 15), 'maxLevel': 2, 'criteria': (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03) } # Read the first frame ret, old_frame = cap.read() old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY) # Detect corners in the first frame p0 = cv2.goodFeaturesToTrack(old_gray, mask=None, **feature_params) while cap.isOpened(): ret, frame = cap.read() if not ret: break frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Calculate optical flow p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params) # Select good points good_new = p1[st==1] good_old = p0[st==1] # Draw the tracks for i, (new, old) in enumerate(zip(good_new, good_old)): a, b = new.ravel() c, d = old.ravel() cv2.line(frame, (a, b), (c, d), (0, 255, 0), 2) cv2.circle(frame, (a, b), 5, (0, 0, 255), -1) cv2.imshow('Frame', frame) old_gray = frame_gray.copy() p0 = good_new.reshape(-1, 1, 2) if cv2.waitKey(30) & 0xFF == 27: break cap.release() cv2.destroyAllWindows() ``` In low-light conditions, I often see motion vectors jumping erratically, or the algorithm fails to detect movement entirely, leading to no points being tracked. I've tried adjusting the parameters for `goodFeaturesToTrack`, increasing `maxCorners`, and tweaking the `qualityLevel`, but the results remain poor. Is there a recommended approach or specific settings in OpenCV that can help improve the accuracy of optical flow under low-light scenarios? Any advice on preprocessing the frames or alternative methods would be greatly appreciated. My development environment is Ubuntu. Thanks in advance!