-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRect.cs
More file actions
72 lines (63 loc) · 2.24 KB
/
Rect.cs
File metadata and controls
72 lines (63 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System;
using MathNet.Numerics.LinearAlgebra;
namespace ByteTrackCSharp;
public class Rect
{
private readonly Matrix<float> tlwh;
public Rect(Rect other)
{
tlwh = other.tlwh.Clone();
}
public Rect(float x, float y, float width, float height)
{
tlwh = Matrix<float>.Build.DenseOfRowMajor(1, 4, new float[] { x, y, width, height });
}
public float x() => tlwh[0, 0];
public void setX(float value) => tlwh[0, 0] = value;
public float y() => tlwh[0, 1];
public void setY(float value) => tlwh[0, 1] = value;
public float width() => tlwh[0, 2];
public void setWidth(float value) => tlwh[0, 2] = value;
public float height() => tlwh[0, 3];
public void SetHeight(float value) => tlwh[0, 3] = value;
public float br_x() => tlwh[0, 0] + tlwh[0, 2];
public float br_y() => tlwh[0, 1] + tlwh[0, 3];
public Matrix<float> getTlbr()
{
return Matrix<float>.Build.DenseOfRowMajor(1, 4, new float[4] { x(), y(), x() + width(), y() + height() });
}
public Matrix<float> getXyah()
{
return Matrix<float>.Build.DenseOfRowMajor(1, 4, new float[4] {
x() + width() / 2,
y() + height() / 2,
width() / height(),
height()
});
}
public float calcIoU(Rect other)
{
float box_area = (other.width() + 1) * (other.height() + 1);
float iw = Math.Min(x() + width(), other.x() + other.width()) - Math.Max(x(), other.x()) + 1;
float iou = 0;
if (iw > 0)
{
float ih = Math.Min(y() + height(), other.y() + other.height()) - Math.Max(y(), other.y()) + 1;
if (ih > 0)
{
float ua = (x() + width() - x() + 1) * (y() + height() - y() + 1) + box_area - iw * ih;
iou = iw * ih / ua;
}
}
return iou;
}
public static Rect generate_rect_by_tlbr(Matrix<float> tlbr)
{
return new Rect(tlbr[0, 0], tlbr[0, 1], tlbr[0, 2] - tlbr[0, 0], tlbr[0, 3] - tlbr[0, 1]);
}
public static Rect generate_rect_by_xyah(Matrix<float> xyah)
{
var width = xyah[0, 2] * xyah[0, 3];
return new Rect(xyah[0, 0] - width / 2, xyah[0, 1] - xyah[0, 3] / 2, width, xyah[0, 3]);
}
}