the average of optical flow
I want to find the average of the optical flow, I could extract the features from the first frame and found their location in the next frame. Now, I want to find the average of the displacement in order to translate the image back to its stationary background in order to stabilize the image.
// here I take the optical flow
cvCalcOpticalFlowPyrLK(frame1_1C, frame2_1C, pyramid1, pyramid2, frame1_features,
frame2_features, corner_count, optical_flow_window, 5,
optical_flow_found_feature, NULL,
开发者_Python百科 optical_flow_termination_criteria, NULL);
// here the features that I extract them
for( int i=0; i < corner_count; i++ ) {
CvPoint p,q;
if ( optical_flow_found_feature[i] == 0 ) continue;
p.x = (int) frame1_features[i].x;
p.y = (int) frame1_features[i].y;
q.x = (int) frame2_features[i].x;
q.y = (int) frame2_features[i].y;
double angle = atan2( (double) p.y - q.y, (double) p.x - q.x );
double hypotenuse = sqrt( square(p.y - q.y) + square(p.x - q.x) );
Now I want to take the average for them, and if you want me to show you more of the code I'm ready to do that.
The corner_count is the number of the features.
I am assuming from what you have written that you want to find the average displacement of all the feature points between the two frames. In that case all you have to do is compute the "hypotenuse" in a loop over the features, add up all its values, and then divide by the number of features.
Ok, here's your answer:
cvCalcOpticalFlowPyrLK(frame1_1C, frame2_1C, pyramid1, pyramid2, frame1_features, frame2_features, corner_count, optical_flow_window, 5, optical_flow_found_feature, NULL, optical_flow_termination_criteria, NULL);
//here the features that I extract them
double sumOfDistances = 0;
for(int i = 0; i < corner_count; ++i)
{
int x1 = (int) frame1_features[i].x;
int y1 = (int) frame1_features[i].y;
int x2 = (int) frame2_features[i].x;
int y2 = (int) frame2_features[i].y;
int dx = x2 - x1;
int dy = y2 - y1;
sumOfDistances += sqrt(dx * dx + dy * dy);
}
double averageDistance = sumOfDistances / corner_count;
I am assuming here that corner_count
is the number of features. You would need to make sure that is indeed correct. Also, I haven't tried to compile this. If I had made any mistakes, it would be up to you to fix them.
However, if you are planning to do image processing more than just this one time, I suggest that you actually learn some programming. What I did here is very basic stuff. Without understanding this for yourself you will not get very far.
精彩评论