Skip to content

Commit ddf98d9

Browse files
authored
Merge pull request Python-World#467 from JohnMasoner/master
upload a function to calculate IoU
2 parents c0feb15 + c67e4f1 commit ddf98d9

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

projects/Compute_IoU/Compute_IoU.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import numpy as np
2+
3+
def Cal_IoU(GT_bbox, Pred_bbox):
4+
'''
5+
Args:
6+
GT_bbox: the bounding box of the ground truth
7+
Pred_bbox: the bounding box of the predicted
8+
Returns:
9+
IoU: Intersection over Union
10+
'''
11+
#1. Calculate the area of the intersecting area
12+
ixmin = max(GT_bbox[0], Pred_bbox[0])
13+
iymin = max(GT_bbox[1], Pred_bbox[1])
14+
ixmax = min(GT_bbox[2], Pred_bbox[2])
15+
iymax = min(GT_bbox[3], Pred_bbox[3])
16+
iw = np.maximum(ixmax - ixmin + 1., 0.) # the weight of the area
17+
ih = np.maximum(iymax - iymin + 1., 0.) # the height of the area
18+
area = iw * ih
19+
20+
#2. Calculate the area of all area
21+
#S = S1 + S2 - area
22+
S1 = (Pred_bbox[2] - GT_bbox[0] + 1) * (Pred_bbox[3] - GT_bbox[1] + 1)
23+
S2 = (GT_bbox[2] - GT_bbox[0] + 1) * (GT_bbox[3] - GT_bbox[1] + 1)
24+
S = S1 + S2 - area
25+
26+
#3. Calculate the IoU
27+
iou = area / S
28+
return iou
29+
30+
if __name__ == "__main__":
31+
pred_bbox = np.array([40, 40, 100, 100])
32+
gt_bbox = np.array([70, 80, 110, 130])
33+
print(Cal_IoU(pred_bbox, gt_bbox))

projects/Compute_IoU/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
# Image-Watermark
3+
4+
## Description
5+
6+
The project will help you calculate Intersection over Union (IoU).
7+
8+
## About this Project
9+
10+
Intersection over Union (IoU) is used when calculating mAP. It is a number from 0 to 1 that specifies the amount of overlap between the predicted and ground truth bounding box.
11+
12+
## Usage
13+
14+
Use the Script [Compute_IoU.py](https://github.com/Python-World/python-mini-projects/blob/master/projects/Compute_IoU/Compute_IoU.py) . In the command line, Enter
15+
16+
`python3 Compute_IoU.py `
17+
18+
## Author
19+
[Mason](https://github.com/JohnMasoner)

0 commit comments

Comments
 (0)