按x坐标对opencv中的MatOfPoint2f进行排序
创始人
2024-08-22 08:30:28
0

要按照MatOfPoint2f中的点的x坐标对其进行排序,可以使用Collections.sort()方法来实现。下面是一个示例代码:

import org.opencv.core.CvType;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        // 创建一个包含点的MatOfPoint2f对象
        List points = new ArrayList<>();
        points.add(new Point(3, 4));
        points.add(new Point(1, 2));
        points.add(new Point(5, 6));
        points.add(new Point(2, 3));
        MatOfPoint2f matOfPoint2f = new MatOfPoint2f();
        matOfPoint2f.fromList(points);

        // 使用Collections.sort()方法按照x坐标对点进行排序
        Collections.sort(matOfPoint2f.toList(), new PointComparator());

        // 打印排序后的点的x坐标
        for (Point point : matOfPoint2f.toList()) {
            System.out.println(point.x);
        }
    }

    // 定义一个PointComparator类用于比较两个点的x坐标
    static class PointComparator implements Comparator {
        @Override
        public int compare(Point p1, Point p2) {
            return Double.compare(p1.x, p2.x);
        }
    }
}

在上述示例中,我们首先创建一个包含四个点的MatOfPoint2f对象,并使用Collections.sort()方法对其进行排序。为了实现排序,我们定义了一个PointComparator类来比较两个点的x坐标。最后,我们遍历排序后的点列表,并打印每个点的x坐标。

相关内容

热门资讯

第三分钟辅助!微乐小程序辅助器... 第三分钟辅助!微乐小程序辅助器出租,微乐小程序免费黑科技,教材教程(有挂安装)-哔哩哔哩一、微乐小程...
第3分钟脚本!微乐广西小程序,... 第3分钟脚本!微乐广西小程序,微乐小程序免费黑科技,项目教程(有挂辅助)-哔哩哔哩微乐小程序免费黑科...
八分钟安装!微信微乐小程序辅助... 八分钟安装!微信微乐小程序辅助软件,微乐小程序免费黑科技,技法教程(有挂app)-哔哩哔哩微信微乐小...
第九分钟安装!微乐小游戏万能修... 第九分钟安装!微乐小游戏万能修改器,微乐小程序免费黑科技,举措教程(有挂工具)-哔哩哔哩1、微乐小游...
六分钟辅助!小程序微乐辅助器i... 六分钟辅助!小程序微乐辅助器ios,微乐小程序黑科技,举措教程(有挂修改器)-哔哩哔哩小程序微乐辅助...
第7分钟插件!微信小程序微乐辅... 第7分钟插件!微信小程序微乐辅助器贴吧,微乐小程序黑科技,法子教程(有挂脚本)-哔哩哔哩1、游戏颠覆...
9分钟脚本!微信小程序微乐挂如... 9分钟脚本!微信小程序微乐挂如何获取,微乐小程序黑科技,指南书教程(有挂软件)-哔哩哔哩1、9分钟脚...
第九分钟脚本!如何下载微乐自建... 第九分钟脚本!如何下载微乐自建房黑科技入口,微乐小程序免费黑科技,妙招教程(有挂平台)-哔哩哔哩1、...
七分钟脚本!微信小程序微乐辅助... 七分钟脚本!微信小程序微乐辅助,微乐小程序免费黑科技,窍门教程(有挂软件)-哔哩哔哩小薇(辅助器软件...
第3分钟辅助!微乐小程序破解,... 第3分钟辅助!微乐小程序破解,微乐小程序免费黑科技,模块教程(有挂神器)-哔哩哔哩所有人都在同一条线...