Reputation: 358
Using OpenCV-2.4.5-android-sdk, I try to match two images with feature detection (ORB detector and hamming matcher). Unfortunateley, I'm always getting a NullPointerException when computing the descriptors. What am I doing wrong?
FeatureDetector detector = FeatureDetector.create("ORB");
DescriptorExtractor descriptor = DescriptorExtractor.create("ORB");
BFMatcher matcher = new BFMatcher(Hamming.normType, true);
KeyPoint keypoints1 = new KeyPoint();
KeyPoint keypoints2 = new KeyPoint();
CvMat[] descriptors = new CvMat[2];
//ORB orb = new ORB();
//orb.detect(image1, null, keypoints1);
detector.detect(image1, keypoints1, null);
descriptor.compute(image1, keypoints1, descriptors[0]);
detector.detect(image2, keypoints2, null);
//orb.detect(image2, null, keypoints2);
descriptor.compute(image2, keypoints2, descriptors[1]);
// matcher should include 2 different image's descriptors
DMatch matches = new DMatch();
matcher.match(descriptors[0], descriptors[1], matches, null);
I wonder, if I have a change to perform feature detection with openCV on Android without android-ndk. Would you suggest trying to write and integrate native c++ code?
Update: After restructureing the project's setup, following this: http://docs.opencv.org/trunk/doc/tutorials/introduction/android_binary_package/dev_with_OCV_on_Android.html#dev-with-ocv-on-android description, the code looks like this:
FeatureDetector detector = FeatureDetector.create(FeatureDetector.ORB);
DescriptorExtractor descriptor = DescriptorExtractor.create(DescriptorExtractor.ORB);
DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);
MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
MatOfKeyPoint keypoints2 = new MatOfKeyPoint();
Mat[] descriptors = new Mat[2];
//ORB orb = new ORB();
//orb.detect(image1, null, keypoints1);
detector.detect(image1, keypoints1, null);
descriptor.compute(image1, keypoints1, descriptors[0]);
detector.detect(image2, keypoints2, null);
//orb.detect(image2, null, keypoints2);
descriptor.compute(image2, keypoints2, descriptors[1]);
// matcher should include 2 different image's descriptors
MatOfDMatch matches = new MatOfDMatch();
matcher.match(descriptors[0], descriptors[1], matches);
The NPE still occurs.
Upvotes: 2
Views: 3374
Reputation: 1536
Try to initializate thouse matrix.
Instead of saying Mat[] descriptors = new Mat[2];
try:
Mat descriptors1= new Mat();
Mat descriptors2= new Mat();
Upvotes: 0
Reputation: 1
Allocate objects eg.
Mat descriptortwo = new Mat();
and then remove the null argument from the optional mask parameter, like so:
detector.detect(image1,keypoints1);
Think it should do the trick :)
Upvotes: 0
Reputation: 376
You seem to have missed allocating objects to the descriptors[]
array.
descriptors[0] = new CvMat();
descriptors[1] = new CvMat();
Upvotes: 1