首页 > 编程语言 >2D-iou用Java实现

2D-iou用Java实现

时间:2022-11-01 17:48:09浏览次数:40  
标签:Java double iou jts 2D split import 图形 d2

一、需求

  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;
    }
}

标签:Java,double,iou,jts,2D,split,import,图形,d2
From: https://www.cnblogs.com/sky-chen/p/16848530.html

相关文章

  • org.xml.sax.SAXNotRecognizedException: SAX feature 'http://apache.org/xml/featur
    tomcat启动服务后,解析xml等文件会报错org.xml.sax.SAXNotRecognizedException:SAXfeature'http://apache.org/xml/features/allow-java-encodings'notrecognized. ......
  • Java Timer使用介绍
    java.util包下提供了对定时任务的支持,涉及2个类:Timer:定时器类TimerTask:任务抽象类使用该定时任务我们需要继承TimerTask抽象类,覆盖run方法编写任务执行代码,并利用Ti......
  • javaweb四大域
    什么是域?一句话总结:就是用来存数据的地方,里面存储的数据都是以key/value的形式存储javaweb中有四大域,他们的作用范围如下:PageContext<request<session<ServletConte......
  • JavaScript对象Function和JavaScript对象Array
    2.基本对象: 1.Function:函数(方法)对象1.创建:1.varfun=newFunction(形式参数列表,方法体);//忘掉吧......
  • Java接收json参数
     Java接收json参数importjava.util.List;importjava.util.Map;importorg.springframework.web.bind.annotation.RequestBody;importorg.springframework.web.bind.anno......
  • JAVA SPI机制
    什么是SPI机制?SPI(ServiceProviderInterface),是JDK内置的一种服务提供发现机制,可以用来启用框架扩展和替换组件,主要是被框架的开发人员使用,比如java.sql.Driver接口,其他不......
  • JavaScript this全解析
    ​​JavaScript​​​中的​​this​​只有如下几种情况,并按他们的优先级从低到高划分如下:独立函数调用,例如​​getUserInfo()​​​,此时​​​this​​​指向全局对......
  • java_day23~24
    Java基础GUI编程核心技术:Swing、AWT现在GUI并不流行因为其界面不美观、需要依赖jre环境SwingpublicclassDemo1{//init();初始化publicvoidinit(){......
  • JavaScript类型转换
    ​​JavaScript​​中,类型转换只有三种:转换成数字转换成布尔值转换成字符串经典类型面试题console.log([]==![]);//true代码分析:左侧是一个对象(数组)右侧是一个布尔值,对......
  • Java表达式解析器
    1、阿里巴巴QLExpresshttps://github.com/alibaba/QLExpress/blob/master/README.md由阿里的电商业务规则、表达式(布尔组合)、特殊数学公式计算(高精度)、语法分析、脚本二......