-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubmission_chroma.py
184 lines (151 loc) · 6.24 KB
/
submission_chroma.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
172
173
174
175
176
177
178
179
180
181
182
183
184
# Enter your code here
import cv2
import numpy as np
points = []
frame = []
frameHSV = []
trackTolerance = 0
trackSoftness = 0
trackDefringe = 50
isColor = False
isBackground = False
def setColor(action, x, y, flags, userdata):
global points, isColor, frameHSV, pixelColorHSV
if action == cv2.EVENT_LBUTTONDOWN and not isColor:
isColor = True
points = (x,y)
pixelColorHSV = tuple(frameHSV[y,x,:])
newImage = removeBackground()
newImage = addAnnotation(newImage)
cv2.imshow(windowName, newImage)
def removeBackground():
global trackTolerance, trackSoftness, trackDefringe, isBackground, frame, frameHSV, bg
# Modify brightness of green channel (Defringe)
B, G, R = cv2.split(frame)
brightnessOffset = 2 * trackDefringe - 100
# Apply defringing effect primarily to the green channel
gModified = np.uint8(np.clip((np.int32(G) + np.ones_like(G, dtype="int32") * brightnessOffset), 0, 255))
# Merge modified green channel with original blue and red channels
frameModified = cv2.merge((B, gModified, R))
frameModifiedHSV = cv2.cvtColor(frameModified, cv2.COLOR_BGR2HSV)
# Remove color background in the Hue channel (Tolerance)
hMod, sMod, vMod = cv2.split(frameModifiedHSV)
colorValue = pixelColorHSV[0]
upperH = np.array(np.clip(colorValue + trackTolerance, 0, 180), dtype="uint8")
lowerH = np.array(np.clip(colorValue - trackTolerance, 0, 180), dtype="uint8")
mask = cv2.inRange(hMod, lowerH, upperH)
mask = 255 - mask
# Apply Gaussian blur for softness
if trackSoftness > 0:
maskBlur = cv2.blur(mask, (10*trackSoftness, 10*trackSoftness), (-1,-1))
maskBlur = np.uint8(np.round((maskBlur/255.0)*(mask/255.0)*255))
else:
maskBlur = mask
# Apply mask to modify brightness of V channel
newV = np.uint8(np.round((vMod/255.0 * maskBlur/255.0) * 255))
imageWithoutBg = cv2.merge((hMod, sMod, newV))
imageWithoutBg = cv2.cvtColor(imageWithoutBg, cv2.COLOR_HSV2BGR)
# Resize background image
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
resized_bg = cv2.resize(bg, (frame_width, frame_height))
# Add background
if isBackground:
maskInv = 255 - mask
maskInv3Ch = np.ones_like(resized_bg)
for i in range(3):
maskInv3Ch[:,:,i] = maskInv
bgCut = np.uint8(np.round((resized_bg/255.0) * (maskInv3Ch/255.0) * 255))
imageWithNewBg = cv2.add(imageWithoutBg, bgCut)
output = imageWithNewBg
else:
output = imageWithoutBg
return output
def getParameter(*args):
global trackTolerance, trackSoftness, trackDefringe
trackTolerance = cv2.getTrackbarPos("Tolerance", windowName)
trackSoftness = cv2.getTrackbarPos("Softness", windowName)
trackDefringe = cv2.getTrackbarPos("Defringe", windowName)
if isColor:
newImage = removeBackground()
newImage = addAnnotation(newImage)
cv2.imshow(windowName, newImage)
def activateBackground(*args):
global isBackground
if isColor:
isBackground = cv2.getTrackbarPos("Background", windowName)
newImage = removeBackground()
newImage = addAnnotation(newImage)
cv2.imshow(windowName, newImage)
def addAnnotation(image):
imageCopied = image.copy()
cv2.putText(imageCopied, "Click to remove green scene", (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (255,255,255), 4)
cv2.putText(imageCopied, "esc: exit", (10, 90), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (255,255,255), 4)
cv2.putText(imageCopied, "s: save video", (10, 130), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (255,255,255), 4)
return imageCopied
def saveVideo(*args):
global cap, frame, frameHSV
if cap.isOpened():
fps = round(cap.get(cv2.CAP_PROP_FPS))
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
numberFrames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
ret = cap.set(cv2.CAP_PROP_POS_FRAMES,0)
out = cv2.VideoWriter("output.mp4", cv2.VideoWriter_fourcc('M','J','P','G'), fps, (frame_width,frame_height))
print("VideoWriter started")
while(cap.isOpened()):
ret, frame = cap.read()
if ret:
frameHSV = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
frameBg = removeBackground()
out.write(frameBg)
frameBg = addAnnotation(frameBg) # Use addAnnotation here
cv2.imshow(windowName, frameBg)
k = cv2.waitKey(10)
if k == ord('a'):
frameBg = removeBackground()
frameBg = addAnnotationAborted(frameBg)
cv2.imshow(windowName, frameBg)
print("VideoWriter aborted")
break
else:
break
frameBg = removeBackground()
frameBg = addAnnotationFinished(frameBg)
cv2.imshow(windowName, frameBg)
print("VideoWriter finished")
out.release()
# Read video and background
cap = cv2.VideoCapture("greenscreen-demo.mp4")
bg = cv2.imread("background.jpg", cv2.IMREAD_COLOR)
# Check for video and background
if not cap.isOpened():
print("Error opening video!")
elif bg is None:
print("Error opening background!")
else:
print("Video and background loaded successfully!")
# Read frame
_, frame = cap.read()
frameCopied = addAnnotation(frame)
# Get frame height/width
frameHeight, frameWidth = frame.shape[:2]
# Create window
windowName = "Display Frame"
cv2.namedWindow(windowName, cv2.WINDOW_NORMAL)
cv2.imshow(windowName, frameCopied)
cv2.resizeWindow(windowName, frameWidth, frameHeight)
frameHSV = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
cv2.setMouseCallback(windowName, setColor)
cv2.createTrackbar("Tolerance", windowName, 0, 100, getParameter)
cv2.createTrackbar("Softness", windowName, 0, 100, getParameter)
cv2.createTrackbar("Defringe", windowName, 50, 100, getParameter)
cv2.createTrackbar("Background", windowName, 0, 1, activateBackground)
# Main loop
k = 0
while k != 27:
k = cv2.waitKey(20) & 0xFF
if k == ord('s'):
saveVideo()
cap.release()
cv2.destroyAllWindows()