2D-iou用Java实现

一、需求

  1. 计算两个多边形iou的值,iou代表两图形的交集除以两图形的并集
  2. 计算图形2的每个点距离图形1的最短距离

二、依赖库

<!-- 几何库 -->
<dependency>
  <groupId>org.locationtech.jts</groupId>
  <artifactId>jts-core</artifactId>
  <version>1.16.1</version>
</dependency>

三、代码例子

import com.lll.toolsky.tool.PolygonUtils;
import org.locationtech.jts.algorithm.distance.DistanceToPoint;
import org.locationtech.jts.algorithm.distance.PointPairDistance;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.jts.io.ParseException;

import java.util.List;

/**
 * Description:
 *
 * @author laoliangliang
 * @version 1.0
 * @date 2022/11/1
 **/
public class JtsTest {
    public static void main(String[] args) throws ParseException {
        // 图形1
        List<String> xyListStr1 = List.of("621.9920676494789,562.6872757226483,1",
                "659.3489485893876,558.66799038183,2",
                "684.6366833794797,542.0166653984401,3",
                "729.4649405073701,536.2748291972712,4",
                "787.511786275536,540.8682981582062,5",
                "801.8798174062701,546.0359507392584,6",
                "805.3281448776463,551.2036033203104,7",
                "806.477587368105,602.880129130831,8",
                "802.4545386514994,614.9379851532858,9",
                "632.3370500636075,615.5121687734027,10");
        // 图形2
        List<String> xyListStr2 = List.of("627.0252590425256,560.7213438679515,1",
                "658.6209099387936,557.1782107974741,2",
                "684.0908734163975,540.7509574707152,3",
                "729.8723267558878,537.5299274066448,4",
                "763.7248098590321,540.753973416468,5",
                "785.0035135238656,543.0086944613173,6",
                "806.927026390664,543.0086944613173,7",
                "809.1838585975402,619.9913129925997,8",
                "718.2657611205241,611.9387378324236,9",
                "636.3749924710131,610.9736351915037,10");
        double[][] d1 = new double[xyListStr1.size()][2];
        double[][] d2 = new double[xyListStr2.size()][2];
        for (int i = 0; i < xyListStr1.size(); i++) {
            String s = xyListStr1.get(i);
            String[] split = s.split(",");
            d1[i][0] = Double.parseDouble(split[0]);
            d1[i][1] = Double.parseDouble(split[1]);
        }
        for (int i = 0; i < xyListStr2.size(); i++) {
            String s = xyListStr2.get(i);
            String[] split = s.split(",");
            d2[i][0] = Double.parseDouble(split[0]);
            d2[i][1] = Double.parseDouble(split[1]);
        }

        calculate(d1, d2);
    }

    private static void calculate(double[][] d1, double[][] d2) {
        Polygon polygon1 = PolygonUtils.createPolygon(d1);
        Polygon polygon2 = PolygonUtils.createPolygon(d2);
        double v = polygonIou(polygon1, polygon2);
        System.out.println("iou:" + v);
        System.out.println("-------");
        System.out.println("计算图形2每个点距离图形1最短距离");
        for (double[] dou : d2) {
            PointPairDistance ppd = new PointPairDistance();
            Coordinate coordinate = new Coordinate(dou[0],dou[1]);
            DistanceToPoint.computeDistance(polygon1, coordinate, ppd);
            System.out.println("点距离图形1最短距离:"+ppd.getDistance());
        }
    }
    public static double polygonIou(Polygon polygon1, Polygon polygon2) {
        // 交集
        Geometry intersection = polygon1.intersection(polygon2);
        double intersectionArea = intersection.getArea();
    
        // 并集
        Geometry union = polygon1.union(polygon2);
        double unionArea = union.getArea();
        return intersectionArea / unionArea;
    }
}
public class PolygonUtils {
    private static GeometryFactory geometryFactory = new GeometryFactory();

    /**
     * 创建多边形对象
     *
     * @param points
     * @return
     */
    public static Polygon createPolygon(double[][] points) {
        int pointCount = points.length;
        if (!Arrays.equals(points[0], points[pointCount - 1])) {
            double[][] newPoints = Arrays.copyOf(points, pointCount + 1);
            newPoints[pointCount] = newPoints[0];
            points = newPoints;
            pointCount = points.length;
        }
        Coordinate[] coordinates = new Coordinate[pointCount];
        for (int i = 0; i < pointCount; i++) {
            double[] point = points[i];
            coordinates[i] = new Coordinate(point[0], point[1]);
        }
        return geometryFactory.createPolygon(coordinates);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值