Opencv4android: Runtime Error org.opencv.features2d.DescriptorMatcher.match

asked 2017-05-15 04:00:49 -0600

bebeer gravatar image

updated 2017-05-16 03:06:07 -0600

I run my application, It's build successes. But when i run on my android device, It's crash. This error from LogCat (see below). I work with Opencv4Android 3.1.0 message from LogCat:

AndroidRuntime: FATAL EXCEPTION: Thread-1871 Process: example.bebeer.com.thaibanknotereader, PID: 12208 java.lang.NullPointerException: Attempt to read from field 'long org.opencv.core.Mat.nativeObj' on a null object reference at org.opencv.features2d.DescriptorMatcher.match(DescriptorMatcher.java:206) at example.bebeer.com.thaibanknotereader.MainActivity.recognize(MainActivity.java:166) at example.bebeer.com.thaibanknotereader.MainActivity.onCameraFrame(MainActivity.java:203) at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:391) at org.opencv.android.JavaCameraView$CameraWorker.run(JavaCameraView.java:350) at java.lang.Thread.run(Thread.java:818)

code is here:

 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();
                    try {
                        initializeOpenCVDependencies();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                break;
                default: {
                    super.onManagerConnected(status);
                }
                break;
            }
        }
    };

initialized :

 private void initializeOpenCVDependencies() throws IOException {
        mOpenCvCameraView.enableView();
        detector = FeatureDetector.create(FeatureDetector.ORB);
        descriptor = DescriptorExtractor.create(DescriptorExtractor.ORB);
        matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);
        img1 = new Mat();
        AssetManager assetManager = getAssets();
        InputStream istr = assetManager.open("bank20.jpg");
        Bitmap bitmap = BitmapFactory.decodeStream(istr);
        Utils.bitmapToMat(bitmap, img1);
        Imgproc.cvtColor(img1, img1, Imgproc.COLOR_RGB2GRAY);
        img1.convertTo(img1, 0); //converting the image to match with the type of the cameras image
        descriptors1 = new Mat();
        keypoints1 = new MatOfKeyPoint();
        detector.detect(img1, keypoints1);
        descriptor.compute(img1, keypoints1, descriptors1);

    }

and recognize method here:

 public Mat recognize(Mat aInputFrame) {
        Imgproc.cvtColor(aInputFrame, aInputFrame, Imgproc.COLOR_RGB2GRAY);
        descriptors2 = new Mat();
        keypoints2 = new MatOfKeyPoint();
        detector.detect(aInputFrame, keypoints2);
        descriptor.compute(aInputFrame, keypoints2, descriptors2);
        MatOfDMatch matches = new MatOfDMatch();
        if (img1.type() == aInputFrame.type()) {
            matcher.match(descriptors1, descriptors2, matches);
        } else {
            return aInputFrame;
        }
        List<dmatch> matchesList = matches.toList();

    Double max_dist = 0.0;
    Double min_dist = 100.0;

    for (int i = 0; i < matchesList.size(); i++) {
        Double dist = (double) matchesList.get(i).distance;
        if (dist < min_dist)
            min_dist = dist;
        if (dist > max_dist)
            max_dist = dist;
    }

    LinkedList<DMatch> good_matches = new LinkedList<DMatch>();
    for (int i = 0; i < matchesList.size(); i++) {
        if (matchesList.get(i).distance <= (1.5 * min_dist))
            good_matches.addLast(matchesList.get(i));
    }

    MatOfDMatch goodMatches = new MatOfDMatch();
    goodMatches.fromList(good_matches);
    Mat outputImg = new Mat();
    MatOfByte drawnMatches = new MatOfByte();
    if (aInputFrame.empty() || aInputFrame.cols() < 1 || aInputFrame.rows() < 1) {
        return aInputFrame;
    }
    Features2d.drawMatches(img1, keypoints1, aInputFrame, keypoints2, goodMatches, outputImg, GREEN, RED, drawnMatches, Features2d.NOT_DRAW_SINGLE_POINTS);
    Imgproc.resize(outputImg, outputImg, aInputFrame.size());

    return outputImg;
}

How i solve this problem? Please help me. Thanks so much.

edit retag flag offensive close merge delete

Comments

it'S a bit unclear, where the null pointer is, but probably your matcher object was not initialized ?

or desciptors1 is invalid ? (you don't show)

berak gravatar imageberak ( 2017-05-16 00:15:02 -0600 )edit

I edited my post for more code. Could you please check it again?

bebeer gravatar imagebebeer ( 2017-05-16 03:01:14 -0600 )edit

yea, good.

unfortunately, i can't spot anything obvious. ;(

berak gravatar imageberak ( 2017-05-16 03:05:41 -0600 )edit

i found these in LogCat: E/OpenCV/StaticHelper: OpenCV error: Cannot load info library for OpenCV but next line is: D/OpenCV/StaticHelper: Library opencv_java3 loaded 05-16 14:36:35.511 28077-28077/example.bebeer.com.thaibanknotereader D/OpenCV/StaticHelper: First attempt to load libs is OK

Is this log are concern this problem?

bebeer gravatar imagebebeer ( 2017-05-16 03:12:50 -0600 )edit

i do not think, it is relevant.

try to debug your recognize() function. (time to learn, how to use a debugger, eh ?)

berak gravatar imageberak ( 2017-05-16 03:17:13 -0600 )edit