使用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仍然存在。
答案 0 :(得分:1)
您似乎错过了将对象分配给descriptors[]
数组。
descriptors[0] = new CvMat();
descriptors[1] = new CvMat();
答案 1 :(得分:0)
分配对象,例如。
Mat descriptortwo = new Mat();
然后从可选的mask参数中删除null参数,如下所示:
detector.detect(image1,keypoints1);
认为它应该做的伎俩:)