forked from ryoppippi/Gasyori100knock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
answer_94.py
54 lines (41 loc) · 1.2 KB
/
answer_94.py
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
import cv2
import numpy as np
np.random.seed(0)
# read image
img = cv2.imread("imori_1.jpg")
H, W, C = img.shape
# Grayscale
gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0]
gt = np.array((47, 41, 129, 103), dtype=np.float32)
cv2.rectangle(img, (gt[0], gt[1]), (gt[2], gt[3]), (0,255,0), 1)
def iou(a, b):
area_a = (a[2] - a[0]) * (a[3] - a[1])
area_b = (b[2] - b[0]) * (b[3] - b[1])
iou_x1 = np.maximum(a[0], b[0])
iou_y1 = np.maximum(a[1], b[1])
iou_x2 = np.minimum(a[2], b[2])
iou_y2 = np.minimum(a[3], b[3])
iou_w = max(iou_x2 - iou_x1, 0)
iou_h = max(iou_y2 - iou_y1, 0)
area_iou = iou_w * iou_h
iou = area_iou / (area_a + area_b - area_iou)
return iou
# crop and create database
Crop_num = 200
L = 60
for i in range(Crop_num):
x1 = np.random.randint(W-L)
y1 = np.random.randint(H-L)
x2 = x1 + L
y2 = y1 + L
crop = np.array((x1, y1, x2, y2))
_iou = iou(gt, crop)
if _iou >= 0.5:
cv2.rectangle(img, (x1, y1), (x2, y2), (0,0,255), 1)
label = 1
else:
cv2.rectangle(img, (x1, y1), (x2, y2), (255,0,0), 1)
label = 0
cv2.imwrite("out.jpg", img)
cv2.imshow("result", img)
cv2.waitKey(0)