Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 22nd, 2010 | Syntax: Java 5 | Size: 3.86 KB | Hits: 361 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1.         public Form1()
  2.         {
  3.             InitializeComponent();
  4.             AcquireInputVideo();
  5.             Application.Idle += new EventHandler(Optical_Flow_Worker);
  6.  
  7.         }
  8.  
  9.  
  10.         void Optical_Flow_Worker(object sender, EventArgs e)
  11.         {
  12.             // If there are still frames to process
  13.                 //We position at the current frame
  14.                 Input_Capture.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_POS_FRAMES, ActualFrameNumber);
  15.                 // We grab the current frame              
  16.                 ActualFrame = Input_Capture.QueryFrame().Flip(Emgu.CV.CvEnum.FLIP.HORIZONTAL);
  17.                
  18.                 ActualGrayFrame = ActualFrame.Convert<Gray, Byte>();
  19.                 // the next frame
  20.                 NextFrame = Input_Capture.QueryFrame().Flip(Emgu.CV.CvEnum.FLIP.HORIZONTAL); ;
  21.                 NextGrayFrame = NextFrame.Convert<Gray, Byte>();
  22.                
  23.                 // We extract some features to be tracked to compute optical flow
  24.                 ActualFeature  = ActualGrayFrame.GoodFeaturesToTrack(300, 0.01d, 0.01d, 5);
  25.              
  26.                
  27.            
  28.                 // FindCornerSubPix performa a better keypoints localization, it's a little improvement over GoodFeaturesToTrack
  29.                 ActualGrayFrame.FindCornerSubPix(ActualFeature, new System.Drawing.Size(10, 10), new System.Drawing.Size(-1, -1), new MCvTermCriteria(20, 0.3d));
  30.                 // Compute optical flow using Lukas Kanade Method
  31.                 OpticalFlow.PyrLK(ActualGrayFrame, NextGrayFrame, ActualFeature[0], new System.Drawing.Size(10, 10), 3, new MCvTermCriteria(20, 0.03d), out NextFeature, out Status, out TrackError);
  32.              
  33.            
  34.                 OpticalFlowFrame = new Image<Bgr, Byte>(ActualFrame.Width, ActualFrame.Height);
  35.                 OpticalFlowFrame = NextFrame.Copy();
  36.                 // We draw some vectors to visualize optical flow result
  37.                 for (int i = 0; i < ActualFeature[0].Length ; i++)
  38.                 {
  39.                    DrawFlowVectors(i);
  40.                 }
  41.                 ActualFrameNumber++;
  42.                 // Frame binding to emguCV imageboxes
  43.                 //imageBoxActualFrame.Image = ActualFrame.ToBitmap();
  44.                // imageBoxNextFrame.Image = NextFrame.ToBitmap();
  45.                 imageBoxOpticalFlow.Image = OpticalFlowFrame.ToBitmap();
  46.            
  47.  
  48.         }
  49.  
  50.         private void DrawFlowVectors(int i)
  51.         {
  52.            
  53.             System.Drawing.Point p = new Point();
  54.             System.Drawing.Point q = new Point();
  55.  
  56.             p.X = (int)ActualFeature[0][i].X;
  57.             p.Y = (int)ActualFeature[0][i].Y;
  58.             q.X = (int)NextFeature[i].X;
  59.             q.Y = (int)NextFeature[i].Y;
  60.            
  61.            
  62.  
  63.             double angle;
  64.             angle = Math.Atan2((double)p.Y - q.Y, (double)p.X - q.X);
  65.  
  66.             LineSegment2D line = new LineSegment2D(p, q);
  67.             OpticalFlowFrame.Draw(line, new Bgr(255, 255, 255), 1);
  68.  
  69.             p.X = (int)(q.X + 20 * Math.Cos(angle + Math.PI / 4));
  70.             p.Y = (int)(q.Y + 20 * Math.Sin(angle + Math.PI / 4));
  71.             OpticalFlowFrame.Draw(new LineSegment2D(p, q), new Bgr(255, 255, 255), 1);
  72.             p.X = (int)(q.X + 20 * Math.Cos(angle - Math.PI / 4));
  73.             p.Y = (int)(q.Y + 20 * Math.Sin(angle - Math.PI / 4));
  74.             OpticalFlowFrame.Draw(new LineSegment2D(p, q), new Bgr(255, 255, 255), 1);
  75.  
  76.            
  77.          
  78.            
  79.              
  80.         }
  81.  
  82.         private void AcquireInputVideo()
  83.         {
  84.  
  85.             Input_Capture = new Capture();
  86.             Input_Capture.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_WIDTH,500);
  87.             Input_Capture.QueryFrame();
  88.             Input_height = Input_Capture.Height;
  89.             Input_width = Input_Capture.Width;
  90.            
  91.         }