-
Notifications
You must be signed in to change notification settings - Fork 1
/
VideoThresholder.py
52 lines (36 loc) · 1.23 KB
/
VideoThresholder.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
import cv2
import os.path
import sys
def InvertColor(img):
return 255 - img
def main():
fullPath = sys.argv[1]
_, tail = os.path.split(fullPath)
videoName = os.path.splitext(tail)[0]
# Remove old output file if it exists.
outputPath = 'Output/' + videoName + '.mp4'
if os.path.isfile(outputPath):
os.remove(outputPath)
print("Processing: " + outputPath)
cap = cv2.VideoCapture(fullPath)
fps = cap.get(cv2.CAP_PROP_FPS)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
ret, frame = cap.read()
height, width, channel = frame.shape
out = cv2.VideoWriter(outputPath, fourcc, fps, (width, height))
while(cap.isOpened()):
ret, frame = cap.read()
if ret == False:
break
frame = InvertColor(frame)
b,g,r = cv2.split(frame)
ret, bThresh = cv2.threshold(b, 50, 255, cv2.THRESH_TOZERO)
ret, gThresh = cv2.threshold(g, 50, 255, cv2.THRESH_TOZERO)
ret, rThresh = cv2.threshold(r, 50, 255, cv2.THRESH_TOZERO)
frame = cv2.merge((bThresh, gThresh, rThresh))
frame = InvertColor(frame)
out.write(frame)
cap.release()
print("Completed!")
if __name__ == '__main__':
main()