openCV4Android features2d错误错误、openCV4Android、features2d

2023-09-06 07:33:00 作者:彼岸花、落

我想用 features2d 绘制的精彩的比赛(不是所有比赛)二images.So之间我用$ C $的这个片段C:

I want to use features2d to draw good matches (not all matches) between two images.So I used this snippet of code:

Mat gray1 = //image1 converted to gray
Mat gray2 = //image2 converted to gray

MatOfDMatch matches = new MatOfDMatch();
MatOfDMatch gm = new MatOfDMatch();

LinkedList<DMatch> good_matches = new LinkedList<DMatch>();
MatOfKeyPoint keypoints_object = new MatOfKeyPoint();
MatOfKeyPoint keypoints_scene = new MatOfKeyPoint();
Mat descriptors_object = new Mat();
Mat descriptors_scene = new Mat();
FeatureDetector fd = FeatureDetector.create(FeatureDetector.ORB);

fd.detect(gray1, keypoints_object);
fd.detect(gray2, keypoints_scene);
// – Step 2: Calculate descriptors (feature vectors)
DescriptorExtractor extractor = DescriptorExtractor
.create(DescriptorExtractor.ORB);

extractor.compute(gray1, keypoints_object, descriptors_object);
extractor.compute(gray2, keypoints_scene, descriptors_scene);
DescriptorMatcher matcher = DescriptorMatcher
.create(DescriptorMatcher.BRUTEFORCE_HAMMING);

matcher.match(descriptors_object, descriptors_scene, matches);

double max_dist = 0;
double min_dist = 100;
List<DMatch> matchesList = matches.toList();

// – Quick calculation of max and min distances between keypoints
for (int i = 0; i < descriptors_object.rows(); i++) {
    Double dist = (double) matchesList.get(i).distance;
    if (dist < min_dist)
    min_dist = dist;
    if (dist > max_dist)
    max_dist = dist;
}

for (int i = 0; i < descriptors_object.rows(); i++) {
    if (matchesList.get(i).distance <= 3 * min_dist) {
        good_matches.addLast(matchesList.get(i));
    }
}

gm.fromList(good_matches);

List<KeyPoint> keypoints_objectList = keypoints_object.toList();
List<KeyPoint> keypoints_sceneList = keypoints_scene.toList();

MatOfKeyPoint matOfObjectGoodKeyPoints = new MatOfKeyPoint();
MatOfKeyPoint matOfSceneGoodKeyPoints = new MatOfKeyPoint();
LinkedList<KeyPoint> listOfObjectGoodKeyPoints = new LinkedList<KeyPoint>();
LinkedList<KeyPoint> listOfSceneGoodKeyPoints = new LinkedList<KeyPoint>();
for (int i = 0; i < good_matches.size(); i++) {
    listOfObjectGoodKeyPoints.addLast(keypoints_objectList
    .get(good_matches.get(i).queryIdx));
    listOfSceneGoodKeyPoints.addLast(keypoints_sceneList
    .get(good_matches.get(i).trainIdx));
}
matOfObjectGoodKeyPoints.fromList(listOfObjectGoodKeyPoints);
matOfSceneGoodKeyPoints.fromList(listOfSceneGoodKeyPoints);

// feature and connection colors
Scalar RED = new Scalar(255, 0, 0);
// output image
Mat outputImg = new Mat();
MatOfByte drawnMatches = new MatOfByte();
// this would draw good matches,but not works fine:
Features2d.drawMatches(gray1, matOfObjectGoodKeyPoints, gray2,
matOfSceneGoodKeyPoints, gm, outputImg, Scalar.all(-1), RED,
drawnMatches, Features2d.NOT_DRAW_SINGLE_POINTS);

但在运行时,出现此错误:

But in run time,this error occurs:

CvException ... features2d / src目录/ draw.cpp:208:错误:(-215)12> = 0&放大器;&安培; I2&LT;  的static_cast(keypoints2.size())

CvException ... features2d/src/draw.cpp:208: error: (-215) i2 >= 0 && i2 < static_cast(keypoints2.size())

什么是code中的问题?

What is the problem in code?

推荐答案

的问题是,你给经过筛选的列表(即 matOfSceneGoodKeyPoints drawMatches()。但精彩的比赛名单包含在原有基础上列出了指数。所以改为

The problem is, that you give a filtered list (i.e. matOfSceneGoodKeyPoints) to drawMatches(). But the good matches list gm contains indices based on the original lists. So change to

Features2d.drawMatches(gray1, keypoints_object, gray2,
keypoints_scene, gm, outputImg, Scalar.all(-1), RED,
drawnMatches, Features2d.NOT_DRAW_SINGLE_POINTS);

和你有你想要的。绘制的比赛仍局限于最好的,因为只有那些在被使用。

and you have what you wanted. The drawn matches are still restricted to the best ones, since only those in gm are used.