gpt4 book ai didi

java - opencv MatOfPoint2f 按 x 坐标排序

转载 作者:搜寻专家 更新时间:2023-11-01 08:17:37 26 4
gpt4 key购买 nike

我在 java 中为 android 项目使用 opencv。我有一个检测到的轮廓作为 MatofPoint,我需要获取轮廓的角点。不是边界矩形。

我需要获取近似矩形轮廓的左上角、右上角、左下角和右下角。所以想到先对点进行排序,然后将轮廓点左右划分,然后找到每边的最大x,最小x和最大y min y。

有没有办法通过 x 坐标对 MatOfPoint2f 进行排序?

我不会用

MatOfPoint2f contour = new MatOfPoint2f(contours.get(i).toArray());
contour.toList().sort();

因为它需要 API 级别 24

最佳答案

尝试使用Java的Collections.sort方法

//sort by y coordinates using the topleft point of every contour's bounding box
Collections.sort(contourList, new Comparator<MatOfPoint>() {
@Override
public int compare(MatOfPoint o1, MatOfPoint o2) {
Rect rect1 = Imgproc.boundingRect(o1);
Rect rect2 = Imgproc.boundingRect(o2);
int result = Double.compare(rect1.tl().y, rect2.tl().y);
return result;
}
} );


//sort by x coordinates
Collections.sort(contourList, new Comparator<MatOfPoint>() {
@Override
public int compare(MatOfPoint o1, MatOfPoint o2) {
Rect rect1 = Imgproc.boundingRect(o1);
Rect rect2 = Imgproc.boundingRect(o2);
int result = 0;
double total = rect1.tl().y/rect2.tl().y;
if (total>=0.9 && total<=1.4 ){
result = Double.compare(rect1.tl().x, rect2.tl().x);
}
return result;
}
});

关于java - opencv MatOfPoint2f 按 x 坐标排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57721225/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com