-
Notifications
You must be signed in to change notification settings - Fork 0
/
cv_testing.py
171 lines (142 loc) · 6.24 KB
/
cv_testing.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import cv2
import numpy as np
class ChangeEffects():
saved_blur_amt = 3
saved_saturation_amt = 3
saved_dilate_amt = 3
saved_threshold_limit_dist = 100
saved_lower_color_limit = 0
saved_upper_color_limit = 360
def change_blur_amt(self, val):
self.saved_blur_amt = (val*2) + 1
self.add_effects(
blur_amt=self.saved_blur_amt,
saturation_amt=self.saved_saturation_amt,
dilate_amt=self.saved_dilate_amt,
threshold_limit_dist=self.saved_threshold_limit_dist,
lower_color_limit = self.saved_lower_color_limit,
upper_color_limit = self.saved_upper_color_limit,
)
def change_saturation_amt(self, val):
self.saved_saturation_amt = val
self.add_effects(
blur_amt=self.saved_blur_amt,
saturation_amt=self.saved_saturation_amt,
dilate_amt=self.saved_dilate_amt,
threshold_limit_dist=self.saved_threshold_limit_dist,
lower_color_limit = self.saved_lower_color_limit,
upper_color_limit = self.saved_upper_color_limit,
)
def change_dilate_amt(self, val):
self.saved_dilate_amt = (val*2) + 1
self.add_effects(
blur_amt=self.saved_blur_amt,
saturation_amt=self.saved_saturation_amt,
dilate_amt=self.saved_dilate_amt,
threshold_limit_dist=self.saved_threshold_limit_dist,
lower_color_limit = self.saved_lower_color_limit,
upper_color_limit = self.saved_upper_color_limit,
)
def change_threshold_limit_dist(self, val):
self.saved_threshold_limit_dist = val
self.add_effects(
blur_amt=self.saved_blur_amt,
saturation_amt=self.saved_saturation_amt,
dilate_amt=self.saved_dilate_amt,
threshold_limit_dist=self.saved_threshold_limit_dist,
lower_color_limit = self.saved_lower_color_limit,
upper_color_limit = self.saved_upper_color_limit,
)
def change_lower_color_limit(self, val):
self.saved_lower_color_limit = val
self.add_effects(
blur_amt=self.saved_blur_amt,
saturation_amt=self.saved_saturation_amt,
dilate_amt=self.saved_dilate_amt,
threshold_limit_dist=self.saved_threshold_limit_dist,
lower_color_limit = self.saved_lower_color_limit,
upper_color_limit = self.saved_upper_color_limit,
)
def change_upper_color_limit(self, val):
self.saved_lower_color_limit = val
self.add_effects(
blur_amt=self.saved_blur_amt,
saturation_amt=self.saved_saturation_amt,
dilate_amt=self.saved_dilate_amt,
threshold_limit_dist=self.saved_threshold_limit_dist,
lower_color_limit = self.saved_lower_color_limit,
upper_color_limit = self.saved_upper_color_limit,
)
def add_effects(
self,
blur_amt: int,
saturation_amt: int,
dilate_amt: int,
threshold_limit_dist: int,
lower_color_limit: int,
upper_color_limit: int,
):
frame_copy = frame.copy()
# Blur
frame_copy = cv2.blur(frame_copy, (blur_amt, blur_amt))
# Saturation
frame_copy = cv2.cvtColor(frame_copy, cv2.COLOR_BGR2HSV).astype("float32")
(h, s, v) = cv2.split(frame_copy)
s = s*(saturation_amt/50)
s = np.clip(s,0,255)
frame_copy = cv2.merge([h,s,v])
# Threshold color
lower_limit = np.array([lower_color_limit, (255-threshold_limit_dist), (255-threshold_limit_dist)])
upper_limit = np.array([upper_color_limit, 255, 255])
frame_copy = cv2.inRange(frame_copy, lower_limit, upper_limit)
# Dilate amount
kernel = np.ones((dilate_amt, dilate_amt), np.uint8)
frame_copy = cv2.dilate(frame_copy, kernel)
res = cv2.bitwise_and(frame,frame,mask = frame_copy)
cv2.imshow(windowName, frame_copy)
cv2.imshow('finish', res)
def __init__(self):
cv2.imshow(windowName, frame)
cv2.createTrackbar('blur_amt', windowName, 0, 100, self.change_blur_amt)
cv2.createTrackbar('saturation_amt', windowName, 0, 100, self.change_saturation_amt)
cv2.createTrackbar('dilate_amt', windowName, 0, 100, self.change_dilate_amt)
cv2.createTrackbar('threshold_limit_dist', windowName, 0, 255, self.change_threshold_limit_dist)
cv2.createTrackbar('lower_color_limit', windowName, 0, 360, self.change_lower_color_limit)
cv2.createTrackbar('upper_color_limit', windowName, 0, 360, self.change_upper_color_limit)
cv2.waitKey(0)
cv2.destroyAllWindows()
# ChangeEffects()
frame = cv2.imread('./vods/frame.png', cv2.IMREAD_COLOR)
windowName = 'vod'
frame_shape = frame.shape
mask = cv2.imread('./minimaps/split.png')
# mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
yoff = int(frame_shape[0]*0.095)
xoff = int(frame_shape[1]*0.09)
new_shape = (int(frame_shape[0]*0.88), int(frame_shape[1]*0.88))
mask = cv2.resize(mask, new_shape, interpolation=cv2.INTER_LINEAR)
yoff_max = min(yoff+mask.shape[0], frame_shape[0])
xoff_max = min(xoff+mask.shape[1], frame_shape[1])
mask_final = np.zeros(frame_shape, dtype="uint8")
mask_final[yoff:yoff_max, xoff:xoff_max] = mask[0:yoff_max-yoff, 0:xoff_max-xoff]
mask_gray = cv2.cvtColor(mask_final, cv2.COLOR_BGR2GRAY)
frame = cv2.bitwise_and(frame, frame, mask=mask_gray)
# cv2.imshow('mask_gray', mask_gray)
# cv2.imshow('frame_new', frame)
mask_final_blur = cv2.cvtColor(mask_final, cv2.COLOR_BGR2GRAY)
frame_new_blur = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('mask_gray_blur', mask_final_blur)
cv2.imshow('frame_new_blur', frame_new_blur)
for i in range(yoff, yoff_max):
for j in range(xoff, xoff_max):
if (frame_new_blur[i, j] == mask_final_blur[i, j]).any():
frame[i, j] = 0
else:
frame[i, j] = frame[i, j]
# if (frame_gray[yoff:yoff_max, xoff:xoff_max] == mask_final[0:yoff_max-yoff, 0:xoff_max-xoff]).all():
# frame[yoff:yoff_max, xoff:xoff_max] = 0
# else:
# frame[yoff:yoff_max, xoff:xoff_max] = frame[yoff:yoff_max, xoff:xoff_max]
cv2.imshow(windowName, frame)
cv2.waitKey(0)
cv2.destroyAllWindows()