光流法返回意外值

时间:2014-06-13 18:24:19

标签: java android opencv image-processing

在过去的两周里,我一直试图创建一个Android应用程序来跟踪空间中的点,因为我移动了三星Galaxy III的相机。简而言之,我使用OpenCV库尝试使用以下两种方法跟踪所述点:

public static void goodFeaturesToTrack(
                   Mat image,
                   MatOfPoint corners,
                   int maxCorners,
                   double qualityLevel,
                   double minDistance);

图像 对应于8位灰色图像, 角落 对应于最佳输出要用于跟踪的点, maxCorners 对应于要获得的最大点数, qualityLevel 对应于要获得的所有点必须> = BestQualityPointValue * qualityLevel minDistance 的分数对应于要找到的点之间的最小距离。 (Link

public static void calcOpticalFlowPyrLK(
                    Mat prevImg,
                    Mat nextImg,
                    MatOfPoint2f prevPts,
                    MatOfPoint2f nextPts,
                    MatOfByte status,
                    MatOfFloat err);

prevImg 对应于 t 时的8位灰色图像, nextImg < / strong>对应于 t + dt 时的8位灰度图像, prevPts 对应于包含2D(x,y)的矩阵)要跟踪的点, nextPts 对应于包含 NEW POSITION 点的 OUTPUT 矩阵, 状态 表示已跟踪哪些点( 1 ),哪些点未被跟踪( 0 )和 错误 包含与已计算位移的点相关联的错误。 (Link

到目前为止,我已成功使用 goodFeaturesToTrack(...)方法,但我仍然无法使用 calcOpticalFlowPyrLK计算点的 FLOW (...)方法。

以下是负责初始化变量和跟踪点数的代码块:

  private static final double MIN_FEATURE_QUALITY = 0.05;
  private static final double MIN_FEATURE_DISTANCE = 4.0;

  private Mat               prevGray;
  private MatOfPoint2f      prev2D,next2D;
  private MatOfPoint        corners;
  private MatOfByte         status;
  private MatOfFloat        err;
  private Scalar            color;
  private Size              winSize;
  private int               maxCorners,maxLevel;

  ...

  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();   
  corners = new MatOfPoint();
  maxLevel = 0;
  winSize = new Size(21,21);
  color = new Scalar(0, 255, 0);
  maxCorners = 1;
  }

  //THIS IS THE METHOD THAT TAKES CARE OF TRACKING THE POINTS
  public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

  nextGray = inputFrame.gray();
  Rscale = nextGray;

  if(!corners.empty()){
      Video.calcOpticalFlowPyrLK(
              prevGray,
              nextGray,
              prev2D,
              next2D,
              status,
              err,
              winSize,
              maxLevel);
      System.out.println("status = " + status.toArray()[0]);
      System.out.println("err = " + err.toArray()[0]);
  }



  prevGray = nextGray;
  prev2D = next2D;
  for(int i=0;i<next2D.toArray().length;i++){
      Core.circle(Rscale, next2D.toArray()[i], 3, color,-1);
  }
  System.out.println("Prev2D = " + prev2D.toArray()[0].toString());
  System.out.println("Next2D = " + next2D.toArray()[0].toString());       
  return Rscale;

  }

问题: 如前所述,参数 status 告诉用户是否已为每个点计算流量。使用 System.out.println(...) ,我会检查每个点状态,它们都是1 。此外,我还检查错误并得到错误= 0.0 但是,这就是杀死我的,新的计算点总是与输入点相同(即nextPts = prevPts )。这就是说,有时这些点可能会通过微不足道的数量改变位置,但很少发生......

1 个答案:

答案 0 :(得分:1)

prevGray = nextGray;

这个副本将导致Mat指向相同的像素数据。所以,在下一次迭代中,当你说:

nextGray = inputFrame.gray();

prevGray也会被上传到相同的像素;)

你想要的是副本:

 prevGray = nextGray.clone();
 prev2D = next2D.clone();  // same story..