Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

c++ - 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,
                       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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...