我正在为 Android 编写代码以限制我从 FAST 检测器获得的关键点数量(现在我得到大约 9000 个关键点)。我想根据响应保留最好的 500 个关键点。我做了一个比较器,可以根据他们的 react 对这些关键点进行排序。现在我想找到一种方法来获得 500 个最佳关键点,并将它们放在一个新列表中。
这是我的代码
// gets the keypoints from the detector, and puts them in a list
List<KeyPoint> pointstest = points1.toList();
// comparator orders the keypoints (check image for output)
order(pointstest);
// make a new list to put the 500 best keypoints in
List<KeyPoint> nieuw = new ArrayList<KeyPoint>();
所以我现在需要用最好的点“重新创建”列表,但我目前还在思考如何解决这个问题。有人有建议吗?我可能在考虑 for 循环,但它可以针对这些关键点实现吗?
最佳答案
实际上,您应该结合@Ashwini Bhangi 和@Peter Lawrey 的建议:首先 sort你的列表,然后得到一个从 0 到 499 的子列表。
比较器正式Comparator<T>
,在你的情况下 Comparator<KeyPoint>
,所以:
int count = 500;
Collections.sort(keypoints, new Comparator<KeyPoint>() {
public int compare(KeyPoint o1, KeyPoint o2) {
//TODO add code for deciding values to compare on
// Note that here you should implement DESCENDING logic
// to get greater values at the beginning, not at the end
return value;
}
});
然后获取子列表:
List<KeyPoint> theBest = new ArrayList<KeyPoint>(keypoints.subList(0, count));
请记住,对原始列表进行排序会对其进行适当的修改。如果您想保留原始列表,您应该在排序前制作一份副本。
关于java - 如何从 Java/Android 的 Arraylist 中保留最好的 500 个关键点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16286679/