返回顶部

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;
    }
}
posted @ 2022-11-01 17:32  老梁讲Java  阅读(122)  评论(0编辑  收藏  举报