Java计算Iou

Java计算Iou,使用请注明出处。

package com.awaymeet.fly.common.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @ClassName IouTools
 * @Description:
 * @Author Jayvia 1210287094@qq.com
 * @Date 2020/8/20 16:42
 * @Version V1.0
 **/
public class IouTools {
    public static volatile boolean debug = false;
    public static synchronized float calculationIouValue(String xmin, String ymin, String xmax, String ymax, String xminp, String yminp, String xmaxp, String ymaxp){
        return calculationIouValue(Integer.parseInt((int)Float.parseFloat(xmin)+""),Integer.parseInt((int)Float.parseFloat(ymin)+""),Integer.parseInt((int)Float.parseFloat(xmax)+""),Integer.parseInt((int)Float.parseFloat(ymax)+""),Integer.parseInt((int)Float.parseFloat(xminp)+""),Integer.parseInt((int)Float.parseFloat(yminp)+""),Integer.parseInt((int)Float.parseFloat(xmaxp)+""),Integer.parseInt((int)Float.parseFloat(ymaxp)+""));
    }
    public static synchronized float calculationIouValue(int xmin, int ymin, int xmax, int ymax, int xminp, int yminp, int xmaxp, int ymaxp){
        //Algorithm core: area_intersection/(area_box1+area_box2-area_intersection)
        int width=xmax-xmin;
        int height=ymax-ymin;
        int widthp=xmaxp-xminp;
        int heightp=ymaxp-yminp;
        int union=width*height+widthp*heightp;
        if(width >= widthp){
            if(xmaxp <= xmin){
                if(debug)System.out.print("x left>");
                return 0;
            }else if(xminp >= xmax){
                if(debug)System.out.print("x right>");
                return 0;
            }else{
                if((xmaxp > xmin && xmaxp < xmax) && (xmin > xminp)){
                    if(debug)System.out.print("x left>");
                    if(ymaxp <= ymin){
                        if(debug)System.out.print("y top>");
                        return 0;
                    }else if(yminp >= ymax){
                        if(debug)System.out.print("y bottom>");
                        return 0;
                    }else{
                        if((ymaxp > ymin && ymaxp < ymax) && (ymin > yminp)){
                            if(debug)System.out.print("y top>");
                            int intersection=(ymaxp-ymin)*(xmaxp-xmin);
                            return new Float(intersection)/new Float(union-intersection);
                        }else if((yminp > ymin && yminp < ymax) && (ymax < ymaxp)){
                            if(debug)System.out.print("y bottom>");
                            int intersection=(ymax-yminp)*(xmaxp-xmin);
                            return new Float(intersection)/new Float(union-intersection);
                        }else{
                            if(height>=heightp){
                                if(debug)System.out.print("y middle");
                                int intersection=(heightp)*(xmaxp-xmin);
                                if(debug)System.out.println();
                                if(debug)System.out.println("heightp:"+heightp);
                                if(debug)System.out.println("(xmaxp-xmin):"+(xmaxp-xmin));
                                if(debug)System.out.println("intersection:"+intersection);
                                if(debug)System.out.println("union:"+union);
                                if(debug)System.out.println("union-intersection:"+(union-intersection));
                                return new Float(intersection)/new Float(union-intersection);
                            }else{
                                int dy = ymax-ymin;
                                int dx = xmaxp-xmin;
                                int intersection=dy*dx;
                                return new Float(intersection)/new Float(union-intersection);
                            }
                        }
                    }
                }else if((xminp > xmin && xminp < xmax) && (xmax < xmaxp)){
                    if(debug)System.out.print("x right>");
                    if(ymaxp <= ymin){
                        if(debug)System.out.print("y top>");
                        return 0;
                    }else if(yminp >= ymax){
                        if(debug)System.out.print("y bottom>");
                        return 0;
                    }else{
                        if((ymaxp > ymin && ymaxp < ymax) && (ymin > yminp)){
                            if(debug)System.out.print("y top>");
                            int intersection=(ymaxp-ymin)*(xmax-xminp);
                            return new Float(intersection)/new Float(union-intersection);
                        }else if((yminp > ymin && yminp < ymax) && (ymax < ymaxp)){
                            if(debug)System.out.print("y bottom>");
                            int intersection=(ymax-yminp)*(xmax-xminp);
                            return new Float(intersection)/new Float(union-intersection);
                        }else{
                            if(height>=heightp){
                                if(debug)System.out.print("y middle");
                                int intersection=(heightp)*(xmaxp-xmax);
                                return new Float(intersection)/new Float(union-intersection);
                            }else{
                                int dy = ymax-ymin;
                                int dx = xmax-xminp;
                                int intersection=dy*dx;
                                return new Float(intersection)/new Float(union-intersection);
                            }
                        }
                    }
                }else{
                    if(debug)System.out.print("x middle");
                    if(ymaxp <= ymin){
                        if(debug)System.out.print("y top>");
                        return 0;
                    }else if(yminp >= ymax){
                        if(debug)System.out.print("y bottom>");
                        return 0;
                    }else{
                        if((ymaxp > ymin && ymaxp < ymax) && (ymin > yminp)){
                            if(debug)System.out.print("y top>");
                            int intersection=widthp*(ymaxp-ymin);
                            return new Float(intersection)/new Float(union-intersection);
                        }else if((yminp > ymin && yminp < ymax) && (ymax < ymaxp)){
                            if(debug)System.out.print("y bottom>");
                            int intersection=widthp*heightp;
                            return new Float(intersection)/new Float(union-intersection);
                        }else{
                            if(height>=heightp){
                                if(debug)System.out.print("y middle");
                                int intersection=(widthp)*(ymax-yminp);
                                return new Float(intersection)/new Float(union-intersection);
                            }else{
                                int dy = ymax-ymin;
                                int dx = xmaxp-xminp;
                                int intersection=dy*dx;
                                return new Float(intersection)/new Float(union-intersection);
                            }
                        }
                    }
                }
            }
        }else{
            if(debug)System.out.println("need exchange width");
            return calculationIouValue(xminp, yminp, xmaxp, ymaxp, xmin, ymin, xmax, ymax);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值