光流法OPenCV 下载本文

*/ floatoptical_flow_feature_error[400]; /* This is the window size to use to avoid the aperture problem (see slide \ CvSizeoptical_flow_window = cvSize(3,3); /* This termination criteria tells the algorithm to stop when it has either done 20 iterations or when * epsilon is better than .3. You can play with these parameters for speed vs. accuracy but these values * work pretty well in many situations. */ CvTermCriteriaoptical_flow_termination_criteria = cvTermCriteria( CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, .3 ); /* This is some workspace for the algorithm. * (The algorithm actually carves the image into pyramids of different resolutions.) */ allocateOnDemand(&pyramid1, frame_size, IPL_DEPTH_8U, 1 ); allocateOnDemand(&pyramid2, frame_size, IPL_DEPTH_8U, 1 ); /* Actually run Pyramidal Lucas Kanade Optical Flow!! * \is the first frame with the known features. * \the first frame's features. * \algorithm. * \frame. * \those features in the second frame. * \is the number of features in the frame1_features array. * \is the size of the window to use to avoid the aperture problem. * \is the maximum number of pyramids to use. 0 would be just one level. * \(non-zero iff feature found by the flow).

* \(error in the flow for this feature). * \above (how long the algorithm should look). * \second array isn't pre-initialized with guesses.) */ cvCalcOpticalFlowPyrLK(frame1_1C, frame2_1C, pyramid1, pyramid2, frame1_features, frame2_features, number_of_features, optical_flow_window, 5, optical_flow_found_feature,

optical_flow_feature_error, optical_flow_termination_criteria, 0 ); /* For fun (and debugging :)), let's draw the flow field. */ for(int i = 0; i

double angle; angle = atan2( (double) p.y - q.y, (double) p.x - q.x ); double hypotenuse; hypotenuse = sqrt( square(p.y - q.y) + square(p.x - q.x) ); /* Here we lengthen the arrow by a factor of three. */ q.x = (int) (p.x - 3 * hypotenuse * cos(angle)); q.y = (int) (p.y - 3 * hypotenuse * sin(angle)); /* Now we draw the main line of the arrow. */ /* \ * \ * \ * \ * \cooridinate or radius. */ cvLine( frame1, p, q, line_color, line_thickness, CV_AA, 0 ); /* Now draw the tips of the arrow. I do some scaling so that the * tips look proportional to the main line of the arrow. */ p.x = (int) (q.x + 9 * cos(angle + pi / 4)); p.y = (int) (q.y + 9 * sin(angle + pi / 4)); cvLine( frame1, p, q, line_color, line_thickness, CV_AA, 0 ); p.x = (int) (q.x + 9 * cos(angle - pi / 4)); p.y = (int) (q.y + 9 * sin(angle - pi / 4)); cvLine( frame1, p, q, line_color, line_thickness, CV_AA, 0 ); } /* Now display the image we drew on. Recall that \Flow\ * the window we created above. */ cvShowImage(\ /* And wait for the user to press a key (so the user has time to look at the image). * If the argument is 0 then it waits forever otherwise it waits that number of milliseconds. * The return value is the key the user pressed.

*/

intkey_pressed;

key_pressed = cvWaitKey(0);

/* If the users pushes \ * Otherwise go forward one frame. */ if (key_pressed == 'b' || key_pressed == 'B') current_frame--; else /* Don't run past the front/end of the AVI. */ if (current_frame< 0) if (current_frame>= number_of_frames - 1) current_frame = number_of_frames - 2; }

}

current_