I have been looking to extract velocity vectors of every pixels b/n two frames. I used the opencv function as flows:
calcOpticalFlowFarneback(Previous_Gray, Current_Gray, Optical_Flow, 0.5, 3, 15, 3, 5, 1.2, 0);
Previous_Gray = previous frame
Current_Gray = current frame
And it gives me the array in the 3rd argument Optical_Flow but I can't really tell how to extract the velocity vectors using this array.
Thanks in Advance.
Dawit
Mat& flow: The computed flow image; will have the same size as prevImg and type CV_32FC2
This type means that you have 2 float variables (presumed x and y) for each pixel's motion. Try this:
Optical_Flow.at<cv::Point2f>(1023,767).x
Optical_Flow.at<cv::Point2f>(1023,767).y
void drawOptFlowMap (const Mat& flow, Mat& cflowmap, int step, double scale, const Scalar& color) {
for(int y = 0; y < cflowmap.rows; y += step)
for(int x = 0; x < cflowmap.cols; x += step)
{
const Point2f& fxy = flow.at<Point2f>(y, x);
line(cflowmap, Point(x,y), Point(cvRound(x+fxy.x), cvRound(y+fxy.y)),
color);
circle(cflowmap, Point(cvRound(x+fxy.x), cvRound(y+fxy.y)), 1, color, -1);
}
}
int main()
{
...read, declare images etc....
cvNamedWindow("optical Flow", CV_WINDOW_NORMAL);
calcOpticalFlowFarneback(Previous_Gray, Current_Gray, flow, 0.5, 3, 15, 3, 5, 1.2, 0);
cvtColor(Previous_Gray, cflow, CV_GRAY2BGR);
drawOptFlowMap(flow, cflow, 32, 50, CV_RGB(0, 255, 0));
imshow("optical Flow", cflow);
etc etc
}
To extract the computed Optical Flow vectors you can use the following function:
static void writeOpticalFlowVectors(const Mat& flow){
std::ofstream myfile;
std::string fullPath = "opticalFlowVectors.txt";
myfile.open (fullPath.c_str());
for(std::size_t x = 0; x < flow.rows; ++x){
for(std::size_t y = 0; y < flow.cols; ++y){
myfile << flow.at<cv::Point2f>(x,y).x;
myfile<< ", ";
myfile<< flow.at<cv::Point2f>(x,y).y;
myfile << "\n";
}
}
myfile.close();
}
This will iterate over your output Mat and write every vector in the form x, y
to a txt
file.
来源:https://stackoverflow.com/questions/16672003/how-to-extract-velocity-vectors-of-a-pixels-from-calcopticalflowfarneback