Java opencv MatOfPoint2f按x坐标排序

Java opencv MatOfPoint2f按x坐标排序,java,android,opencv,Java,Android,Opencv,我正在使用java中的opencv进行android项目。 我有一个检测到的轮廓作为一个点,我需要得到轮廓的角点。不是边界矩形 我需要得到一个近似矩形轮廓的左上角,右上角,左下角,右下角。因此,考虑对点进行排序,首先将轮廓点向左和向右划分,然后找到每一侧的最大x、最小x和最大y最小y 是否存在通过x坐标来sot MatOfPoint2f的情况 我不能用 MatOfPoint2f contour=新的MatOfPoint2f(contours.get(i.toArray()); toList().

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

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

是否存在通过x坐标来sot MatOfPoint2f的情况

我不能用

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

因为它需要API级别24

尝试使用Java的
方法对集合进行排序

//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;
    }
});
//使用每个轮廓边界框的左上角点按y坐标排序
Collections.sort(轮廓列表,新比较器(){
@凌驾
公共整数比较(点o1,点o2){
Rect rect1=Imgproc.boundingRect(o1);
Rect rect2=Imgproc.boundingRect(o2);
int result=Double.compare(rect1.tl().y,rect2.tl().y);
返回结果;
}
} );
//按x坐标排序
Collections.sort(轮廓列表,新比较器(){
@凌驾
公共整数比较(点o1,点o2){
Rect rect1=Imgproc.boundingRect(o1);
Rect rect2=Imgproc.boundingRect(o2);
int结果=0;
双倍合计=rect1.tl().y/rect2.tl().y;
如果(总数>=0.9&&total