KeyPoints opencv FAST探测器android

时间:2015-02-16 17:34:45

标签: java android performance opencv

我正在尝试使用Opencv FAST检测器在Android中设置阈值。我找到了一个类似的解决问题here。我按照建议在检测方法后列出了关键点列表,但仍然不适用于我。在我的情况下,我想检测相机框架上的关键点:

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
    MatOfKeyPoint points = new MatOfKeyPoint();

    Mat mat=inputFrame.rgba();
    FeatureDetector fast = FeatureDetector.create(FeatureDetector.FAST);
    fast.detect(mat, points);

    // Sort and select 500 best keypoints
    List<KeyPoint> listOfKeypoints = points.toList();
    Collections.sort(listOfKeypoints, new Comparator<KeyPoint>() {
        @Override
        public int compare(KeyPoint kp1, KeyPoint kp2) {
            // Sort them in descending order, so the best response KPs will come first
            return (int) (kp2.response - kp1.response);
        }
    });
    List<KeyPoint> listOfBestKeypoints = listOfKeypoints.subList(0, 500);
    points.fromList(listOfBestKeypoints);   



    Scalar redcolor = new Scalar(255,0,0);
    Mat mRgba= mat.clone();
    Imgproc.cvtColor(mat, mRgba, Imgproc.COLOR_RGBA2RGB,4);

    Features2d.drawKeypoints(mRgba, points, mRgba, redcolor, 3);

    return mRgba;
}

问题是我的listOfKeypoints保持为null。如果我不尝试设置阈值,代码工作正常但速度太慢。

我在这里做错了什么?

感谢。

1 个答案:

答案 0 :(得分:0)

我发现只有第一次调用onCameraFrame时,我的列表才为空。所以我可以使用类变量并从第二次调用开始填充我的列表。

private int count=0;

然后在onCameraFrame;

if (count!=0) {
        //Sort and select 500 best keypoints
        List<KeyPoint> listOfKeypoints =  points.toList();
        Collections.sort(listOfKeypoints, new Comparator<KeyPoint>() {
            @Override
            public int compare(KeyPoint kp1, KeyPoint kp2) {
                // Sort them in descending order, so the best response KPs will come first
                return (int) (kp2.response - kp1.response);
            }
        });
        List<KeyPoint> listOfBestKeypoints = listOfKeypoints.subList(0, 500);
        points.fromList(listOfBestKeypoints);

    } 
    count++;

通过这种方式它可以工作,但我仍然不明白为什么在第一次调用onCameraFrame时列表为null。有什么想法吗?