I am using Lucas-Kanade Optical Flow Method to track the points from one image to the next one. In OpenCV, there is an easy to use function:
void cv::calcOpticalFlowPyrLK (
InputArray prevImg,
InputArray nextImg,
InputArray prevPts,
InputOutputArray nextPts,
OutputArray status,
OutputArray err,
Size winSize = Size(21, 21),
int maxLevel = 3,
TermCriteria criteria = TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, 0.01),
int flags = 0,
double minEigThreshold = 1e-4
)
After I got the tracked points in nextPts
, I iterated through all the point pairs and calculated the Euclidean distance:
diff_x = prev_pt.x - curr_pt.x;
diff_y = prev_pt.y - curr_pt.y;
euclidean_dist = sqrt(pow(diff_x, 2) + pow(diff_y, 2));
During the iterations, I also kept the track of maximum distance and to my surprise it turned out to be as high as around 500 px!! How's that even possible!? Because the size of the search window at each pyramid level i.e. winSize
is set to cv::Size(21, 21)
. So, the maximum distant point pairs will look like this:
Hence, from my understanding, for winSize = cv::Size(21, 21)
, the maximum distance cannot go beyond 14.14 px
, right?
(Here is how I got 14.14
number):
Pi = cv::Point(10, 10)
Pj = cv::Point(20, 0)
diff_x = 10
diff_y = 10
euclidean_dist = sqrt(10^2 + 10^2)
= sqrt(200)
= 14.14
So, I don't understand why I'm getting the maximum Euclidean distance between tracked point pairs as high as around 500 px.
As per the OpenCV documentation, the parameter description for winSize
says:
winSize - size of the search window at 'each' pyramid level.
Does that mean winSize
remains constant even when the image is down-scaled in the pyramid? Because of that, some point pairs might be within the search window at upper levels of the pyramid?