简体   繁体   中英

Android App Tracking Points: Video.calcOpticalFlow(…) method not working properly?

I am currently trying to create an Android App using OpenCV that allows the user to track points of moving objects using the smartphone camera. An analogous code in C++ that does exactly what I am looking for can be found in the following link: OpticalFlow C++ Sample Code

I have been Googling and looking around in StackOverflow, but I still can't figure out why my code is not working. I am able to place points on the screen every time I press on a certain spot, but the points seem motionless even as I move objects in front of the camera. The method used to calculate the opitcal flow is the following:

void org.opencv.video.Video.calcOpticalFlowPyrLK(Mat prevImg, Mat nextImg, MatOfPoint2f prevPts, MatOfPoint2f nextPts, MatOfByte status, MatOfFloat err)

I believe I am passing the exact parameters needed to calculate the optical flow of consecutive images, but for some reason it's not working. Below is my code:

package org.opencv.UActivity;

//INCLUDE FILES
...

public class U2Activity extends Activity implements OnTouchListener,CvCameraViewListener2{



  private static final String  TAG              = "OCVSample::Activity";

  private Mat                       nextGray,Rscale;  
  private Mat                       prevGray;
  private MatOfPoint2f              prev2D,next2D;
  private MatOfByte                 status; 
  private MatOfFloat                err; 
  private Scalar                    color;

  private CameraBridgeViewBase mOpenCvCameraView;
  private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
      @Override
      public void onManagerConnected(int status) {
          switch (status) {
              case LoaderCallbackInterface.SUCCESS:
              {
                  Log.i(TAG, "OpenCV loaded successfully");
                  mOpenCvCameraView.enableView();
                  mOpenCvCameraView.setOnTouchListener(U2Activity.this);
              } break;
              default:
              {
                  super.onManagerConnected(status);
              } break;
          }
      }
  };

  public U2Activity() {
      Log.i(TAG, "Instantiated new " + this.getClass());
  }

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      Log.i(TAG, "called onCreate");
      super.onCreate(savedInstanceState);
      requestWindowFeature(Window.FEATURE_NO_TITLE);
      getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

      setContentView(R.layout.u2_surface_view);

      mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.u2_activity_surface_view);
      mOpenCvCameraView.setCvCameraViewListener(this);

      color = new Scalar(0, 255, 0);

  }

  @Override
  public void onPause()
  {
      super.onPause();
      if (mOpenCvCameraView != null)
          mOpenCvCameraView.disableView();
  }

  @Override
  public void onResume()
  {
      super.onResume();
      OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback);
  }

  public void onDestroy() {
      super.onDestroy();
      if (mOpenCvCameraView != null)
          mOpenCvCameraView.disableView();
  }

  public void onCameraViewStarted(int width, int height) {
      nextGray = new Mat(height, width, CvType.CV_8UC1); //unsigned char
      Rscale = new Mat(height, width, CvType.CV_8UC1);
      prevGray = new Mat(height, width, CvType.CV_8UC1);

      prev2D = new MatOfPoint2f(new Point());
      next2D = new MatOfPoint2f(new Point());                           
      status = new MatOfByte();                             
      err = new MatOfFloat();   
  }

  public void onCameraViewStopped() {
      nextGray.release();
      Rscale.release();
  }

  public boolean onTouch(View v, MotionEvent event) {

      int cols = nextGray.cols();
      int rows = nextGray.rows();

      int xOffset = (mOpenCvCameraView.getWidth() - cols) / 2;
      int yOffset = (mOpenCvCameraView.getHeight() - rows) / 2;

      int x = (int)event.getX() - xOffset;
      int y = (int)event.getY() - yOffset;

      if ((x < 0) || (y < 0) || (x > cols) || (y > rows)) return false;

      prev2D.push_back(new MatOfPoint2f(new Point((double)x,(double)y)));
      next2D.push_back(new MatOfPoint2f(new Point()));

      return false; // don't need subsequent touch events
  }

  public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
      nextGray = inputFrame.gray(); //get current image
      Rscale = nextGray; //make a copy of current image
      if(prevGray.empty()) prevGray = nextGray; //on start there is no prevGray. Copy current.
      Video.calcOpticalFlowPyrLK(prevGray,nextGray,prev2D,next2D,status,err);  //Calc the Optical Flow
      prevGray = nextGray; //Overwrite old Image (prevGray)
      prev2D = next2D; //Overwrite old point coordinates
      for(int i=0;i<next2D.toArray().length;i++){ //Draw the points in the image
          Core.circle(Rscale, next2D.toArray()[i], 3, color);
      }
      return Rscale;   
  }
}

SOLVED :

I changed:

  prevGray = nextGray;
  prev2D = next2D;

to:

  nextGray.copyTo(prevGray);
  next2D.copyTo(prev2D);

I hope it helps anyone encountering similar problems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM