【问题标题】:opencv MatOfPoint2f sort by x coordinateopencv MatOfPoint2f 按x坐标排序
【发布时间】:2019-08-30 14:39:29
【问题描述】:

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

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

有没有办法让 MatOfPoint2f 通过 x 坐标?

我不能用

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

因为它需要 API 级别 24

【问题讨论】:

    标签: java android opencv


    【解决方案1】:

    尝试使用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;
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-07
      • 2012-08-08
      • 2018-05-15
      • 2021-10-06
      • 1970-01-01
      • 2011-10-23
      • 1970-01-01
      • 2018-11-22
      相关资源
      最近更新 更多