-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathbox.cpp
More file actions
146 lines (132 loc) · 3.54 KB
/
box.cpp
File metadata and controls
146 lines (132 loc) · 3.54 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//========================================================================
// Box
//========================================================================
// @brief: sort boxes according to the confidence
#ifndef SRC_BOX_CPP_
#define SRC_BOX_CPP_
#include "box.h"
// sort boxes
void do_nms_sort(box *boxes, float **probs, int total, int classes, float thresh)
{
sortable_box *s = (sortable_box *)calloc(total, sizeof(sortable_box));
for (int i = 0; i < total; i++)
{
s[i].index = i;
s[i].classes = 0;
s[i].probs = probs;
}
for (int k = 0; k < classes; k++)
{
for (int i = 0; i < total; i++)
{
s[i].classes = k;
}
qsort(s, total, sizeof(sortable_box), nms_comparator);
for (int i = 0; i < total; i++)
{
if (probs[s[i].index][k] == 0)
{
continue;
}
box a = boxes[s[i].index];
for (int j = i+1; j < total; j++)
{
box b = boxes[s[j].index];
if (box_iou(a, b) > thresh)
{
probs[s[j].index][k] = 0;
}
}
}
}
free(s);
}
// compare function for qsort
int nms_comparator(const void *pa, const void *pb)
{
sortable_box a = *(sortable_box *)pa;
sortable_box b = *(sortable_box *)pb;
float diff = a.probs[a.index][b.classes] - b.probs[b.index][b.classes];
if (diff < 0) return 1;
else if (diff > 0) return -1;
return 0;
}
//
float box_iou(box a, box b)
{
return box_intersection(a, b)/box_union(a,b);
}
// overlap area
float box_intersection(box a, box b)
{
float w = overlap(a.x, a.w, b.x, b.w);
float h = overlap(a.y, a.h, b.y, b.h);
if (w < 0 || h < 0)
{
return 0;
}
float area = w*h;
return area;
}
// overlap length (width, height, etc.)
// x1, x2 midpoint of the boxes
float overlap(float x1, float w1, float x2, float w2)
{
float l1 = x1 - w1/2;
float l2 = x2 - w2/2;
float left = l1 > l2 ? l1 : l2;
float r1 = x1 + w1/2;
float r2 = x2 + w2/2;
float right = r1 < r2 ? r1 : r2;
return right - left;
}
// union area = total - intersection
float box_union(box a, box b)
{
float i = box_intersection(a, b);
float u = a.w*a.h + b.w*b.h - i;
return u;
}
// select boxes contains a confidence larger than the threshhold
void do_nms_obj(box *boxes, float **probs, int total, int classes, float thresh)
{
sortable_box *s = (sortable_box *)calloc(total, sizeof(sortable_box));
for (int i = 0; i < total; i++)
{
s[i].index = i;
s[i].classes = classes;
s[i].probs = probs;
}
qsort(s, total, sizeof(sortable_box), nms_comparator);
for (int i = 0; i < total; i++)
{
if (probs[s[i].index][classes] == 0)
{
continue;
}
box a = boxes[s[i].index];
for (int j = i+1; j < total; j++)
{
box b = boxes[s[j].index];
if (box_iou(a, b) > thresh)
{
for (int k = 0; k < classes+1; k++)
{
probs[s[j].index][k] = 0;
}
}
}
}
free(s);
}
// store value into box
box float_to_box (float *f)
{
box b;
b.x = f[0];
b.y = f[1];
b.w = f[2];
b.h = f[3];
return b;
}
#endif /* SRC_BOX_CPP_ */