Very bad fps using Surf in Android

asked 2016-06-21 06:17:01 -0500

I'm trying to build an android app which detects a particular object in real time using SURF but I'm getting very bad fps (~0.2fps). I have little idea about OpenCv so please suggest what I can do or some alternative to improve the fps.

@Override
public void onCameraViewStarted(int width, int height) {
    DETECTED_COLOR = new Scalar(255, 0, 255, 255);
    Mat temp = Highgui.imread(getAbsPath());
    Imgproc.cvtColor(temp, imgFile, Imgproc.COLOR_BGR2BGRA);
    imgKeyPoints = new MatOfKeyPoint();
    featureDetector = FeatureDetector.create(FeatureDetector.SURF);
    featureDetector.detect(imgFile, imgKeyPoints);
    KeyPoint[] keypoints = imgKeyPoints.toArray();
    imgDescriptors = new MatOfKeyPoint();
    descriptorExtractor = DescriptorExtractor.create(DescriptorExtractor.SURF);
    //object image is mat of img file,object keypoints features??,object descriptor placed
    descriptorExtractor.compute(imgFile, imgKeyPoints, imgDescriptors);
}

private String getAbsPath() {
    File mFile = new File(getContext().getFilesDir(), "board.png");
    return mFile.getAbsolutePath();
}

@Override
public void onCameraViewStopped() {

}

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
    mFrame = inputFrame.rgba();
    MatOfKeyPoint frameKeyPoints = new MatOfKeyPoint();
    MatOfKeyPoint frameDescriptors = new MatOfKeyPoint();
    featureDetector.detect(mFrame, frameKeyPoints);
    descriptorExtractor.compute(mFrame, frameKeyPoints, frameDescriptors);
    Mat matchoutput = new Mat(mFrame.rows() * 2, mFrame.cols() * 2, Highgui.CV_LOAD_IMAGE_COLOR);
    List matches = new LinkedList();
    if(frameDescriptors.empty()){
        Log.i("fall into","Descriptors not found");
        return mFrame;
    }
    DescriptorMatcher descriptorMatcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);
    descriptorMatcher.knnMatch(imgDescriptors, frameDescriptors, matches,2);
    LinkedList<DMatch> goodMatchesList = new LinkedList<>();
    float nndrRatio = 0.7f;
    for (int i = 0; i < matches.size(); i++) {
        //self
        MatOfDMatch matOfDMatch = (MatOfDMatch) matches.get(i);
        DMatch[] dmatcharray = matOfDMatch.toArray();
        DMatch m1 = dmatcharray[0];
        DMatch m2 = dmatcharray[1];

        if (m1.distance <= m2.distance * nndrRatio) {
            goodMatchesList.addLast(m1);
        }
    }

    if (goodMatchesList.size() >= 17) {
        //found object
        Log.i("fall into","Object found :"+ goodMatchesList.size());
        List<KeyPoint> imgKeypointList = imgKeyPoints.toList();
        List<KeyPoint> frmKeypointList = frameKeyPoints.toList();

        LinkedList imgPoints = new LinkedList<>();
        LinkedList frmPoints = new LinkedList<>();
        for (int i = 0; i < goodMatchesList.size(); i++) {
            imgPoints.addLast(imgKeypointList.get(goodMatchesList.get(i).queryIdx).pt);
            frmPoints.addLast(frmKeypointList.get(goodMatchesList.get(i).trainIdx).pt);
        }

        MatOfPoint2f imgMatOfPoint2f = new MatOfPoint2f();
        imgMatOfPoint2f.fromList(imgPoints);
        MatOfPoint2f frmMatOfPoint2f = new MatOfPoint2f();
        frmMatOfPoint2f.fromList(frmPoints);
        Mat homography = Calib3d.findHomography(imgMatOfPoint2f, frmMatOfPoint2f, Calib3d.RANSAC, 3);
        Mat img_corners = new Mat(4, 1, CvType.CV_32FC2);
        Mat frm_corners = new Mat(4, 1, CvType.CV_32FC2);

        img_corners.put(0, 0, new double[]{0, 0});
        img_corners.put(1, 0, new double[]{imgFile.cols(), 0});
        img_corners.put(2, 0, new double[]{imgFile.cols(), imgFile.rows()});
        img_corners.put(3, 0, new double[]{0, imgFile.rows()});

        Core.perspectiveTransform(img_corners, frm_corners, homography);
        Core.line(mFrame, new Point(frm_corners.get(0, 0)), new Point(frm_corners.get(1, 0)), DETECTED_COLOR, 4);
        Core.line(mFrame, new Point(frm_corners.get(1, 0)), new Point(frm_corners.get(2, 0)), DETECTED_COLOR, 4);
        Core.line(mFrame, new Point(frm_corners.get(2, 0)), new Point(frm_corners.get(3, 0)), DETECTED_COLOR, 4);
        Core.line(mFrame, new Point(frm_corners.get(3, 0)), new Point(frm_corners.get(0, 0)), DETECTED_COLOR, 4);

    } else {
        Log.i("fall into","Object not found");
    }

    return mFrame;
}
edit retag flag offensive close merge delete

Comments

btw, which opencv version is it ?

berak gravatar imageberak ( 2016-06-21 06:24:47 -0500 )edit

Its 2.4.11

Sande96 gravatar imageSande96 ( 2016-06-21 06:32:13 -0500 )edit