Um eine Funktion mit einem dichten optischen Strömungsfeld verfolgt flow
wie folgt durchgeführt werden kann:
// read images
cv:Mat prevImg = cv::imread(filename0); // image data at time t
cv::Mat currImg = cv::imread(filename1); // image data at time t and t + 1
cv::Mat flowMat; // storage for dese optical flow field
std::vector<cv::Point2f> prevPoints; // points to be track
// initialize points to track (example)
prevPoints.push_back(cv::Point2f(50.3f, 30.f));
std::vector<cv::Point2f> currPoints(prevPoints.size()); // tracked point position
// compute dense flow field (example)
cv::calcOpticalFlowFarneback(prevImg, currImg, flowMat, 0.4, 1, 12, 2, 8, 1.2, 0);
// track points based on dense optical flow field and bilinear interpolation
for(unsigned int n = 0; n < prevPoints.size(); ++n)
{
float ix = floor(prevPoints[n].x);
float iy = floor(prevPoints[n].y);
float wx = prevPoints[n].x - ix;
float wy = prevPoints[n].y - iy;
float w00 = (1.f - wx) * (1.f - wy);
float w10 = (1.f - wx) * wy;
float w01 = wx * (1.f - wy);
float w11 = wx * wy;
if(prevPoints[n].x >= flowMat.cols - 1 || prevPoints[n].y >= flowMat.rows - 1)
{
// these points are out of the image roi and cannot be tracked.
currPoints[n] = prevPoints[n];
}
else
{
/*
bilinear interpolation of the flow vector from the flow field at a given location.
The bilinear interpolation has to be applied since the points to track can be given at subpixel level
*/
currPoints[n] = prevPoints[n]
+ flowMat.at<cv::Point2f>(iy, ix) * w00
+ flowMat.at<cv::Point2f>(iy+1, ix) * w10
+ flowMat.at<cv::Point2f>(iy, ix+1) * w01
+ flowMat.at<cv::Point2f>(iy+1, ix+1) * w11;
}
}
}
Bitte lesen Sie [on-Thema] (https://stackoverflow.com/help/on-topic). –