changeset 15:3ead4bcd001c

cleaned optical flow
author Nicolas Saunier <nico@confins.net>
date Sun, 15 Nov 2009 01:04:10 -0500
parents e7bbe8465591
children 9403759cf2c1
files c/optical-flow.cpp
diffstat 1 files changed, 33 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/c/optical-flow.cpp	Sat Nov 14 19:02:46 2009 -0500
+++ b/c/optical-flow.cpp	Sun Nov 15 01:04:10 2009 -0500
@@ -70,57 +70,46 @@
 
   int pressedKey = '?';
   while (frame && !::interruptionKey(pressedKey)) {
-    cvGoodFeaturesToTrack(frame1_1C, eig_image, temp_image, frame1_features, &nFeatures, .01, .01, NULL);
+    cvGoodFeaturesToTrack(frame1_1C, eig_image, temp_image, frame1_features, &nFeatures, 0.05 /*quality*/, 1/* min dist*/, NULL);
     cvCalcOpticalFlowPyrLK(frame1_1C, frame2_1C, pyramid1, pyramid2, frame1_features, frame2_features, nFeatures, optical_flow_window, 5, optical_flow_found_feature, optical_flow_feature_error, optical_flow_termination_criteria, 0 );
 
     for(int i = 0; i < nFeatures; i++) {
-      /* If Pyramidal Lucas Kanade didn't really find the feature, skip it. */
-      if ( optical_flow_found_feature[i] == 0 )	continue;
-      
-      int line_thickness;				line_thickness = 1;
-      /* CV_RGB(red, green, blue) is the red, green, and blue components
-       * of the color you want, each out of 255.
-       */	
-      CvScalar line_color;			line_color = CV_RGB(255,0,0);
-      
-      /* Let's make the flow field look nice with arrows. */
-      
-      /* The arrows will be a bit too short for a nice visualization because of the high framerate
-       * (ie: there's not much motion between the frames).  So let's lengthen them by a factor of 3.
-       */
-      CvPoint p,q;
-      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;
+      if (optical_flow_found_feature[i] != 0) {
+	int line_thickness = 1;
+	CvScalar line_color = CV_RGB(255,0,0);
       
-      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));
+	CvPoint p,q;
+	p.x = lrintf(frame1_features[i].x);
+	p.y = lrintf(frame1_features[i].y);
+	q.x = lrintf(frame2_features[i].x);
+	q.y = lrintf(frame2_features[i].y);
+	
+	double dx = frame2_features[i].x-frame1_features[i].x;
+	double dy = frame2_features[i].y-frame1_features[i].y;
+
+	double angle = atan2( (double) p.y - q.y, (double) p.x - q.x );
+	double dist = sqrt(square(dx) + square(dy));
+
+	if (dist > 2.) { // min motion distance
+	  q.x = lrintf(frame1_features[i].x+dx*3); //(p.x - 3 * hypotenuse * cos(angle));
+	  q.y = lrintf(frame1_features[i].y+dy*3); //(p.y - 3 * hypotenuse * sin(angle));
       
-      /* Now we draw the main line of the arrow. */
-      /* "frame1" is the frame to draw on.
-       * "p" is the point where the line begins.
-       * "q" is the point where the line stops.
-       * "CV_AA" means antialiased drawing.
-       * "0" means no fractional bits in the center 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 we draw the main line of the arrow. */
+	  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.
+	   */
+	  float arrowSize = 6;
+	  p.x = lrintf(q.x + arrowSize * cos(angle + pi / 4));
+	  p.y = lrintf(q.y + arrowSize * sin(angle + pi / 4));
+	  cvLine( frame1, p, q, line_color, line_thickness, CV_AA, 0 );
+	  p.x = lrintf(q.x + arrowSize * cos(angle - pi / 4));
+	  p.y = lrintf(q.y + arrowSize * sin(angle - pi / 4));
+	  cvLine( frame1, p, q, line_color, line_thickness, CV_AA, 0 );
+	}
+      }
     }
     cvShowImage("Optical Flow", frame1);
-    //cvWaitKey(5);
     pressedKey = cvWaitKey(5);
     frame = cvQueryFrame(inputVideo);
     frameNum++;