opencv MatOfPoint2f 按 x 坐标排序



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

我需要获得近似矩形轮廓的左上角、右上角、左下角。因此,考虑对点进行排序并首先将轮廓点划分为左右,然后找到每条边的最大 x、最小 x 和最大 y 最小 y。

有没有办法通过x坐标来sot MatOfPoint2f?

我不能使用

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

因为它需要 API 级别 24

尝试用户Collections.sortJava的方法

//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;
}
});

最新更新