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