使用 OpenCV-2.4.5-android-sdk,我尝试将两个图像与特征检测(ORB 检测器和汉明匹配器)进行匹配。不幸的是,我在计算描述符时总是得到 NullPointerException。我究竟做错了什么?
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);
我想知道,如果我在没有 android-ndk 的情况下在 Android 上使用 openCV 执行特征检测进行更改。您是否建议尝试编写和集成本机 C++ 代码?
更新:重组项目的设置后,如下:http ://docs.opencv.org/trunk/doc/tutorials/introduction/android_binary_package/dev_with_OCV_on_Android.html#dev-with-ocv-on-android描述,代码看起来像这个:
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);
NPE 仍然发生。