-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrambling.py
58 lines (46 loc) · 1.9 KB
/
scrambling.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
import cv2
import numpy as np
import scipy.fftpack
def scramble(f):
cap=cv2.VideoCapture(f)
perm = None
noise = None
fourcc = cv2.cv.CV_FOURCC(*'PIM1')
out_fname = '.'.join(f.split('.')[:-1])+'_scrambled.mpg'
#print out_fname
out = None
i=0
ret, frame = cap.read()
perm = np.random.permutation(np.arange(frame[:,:,0].size))
noise = np.random.vonmises(mu=0,kappa=.05,size=frame.shape[:2])
out = cv2.VideoWriter(out_fname, fourcc, cap.get(cv2.cv.CV_CAP_PROP_FPS), frame.shape[:2][::-1])
while(ret):
print i
i += 1
# if i>10:
# break
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2LAB).astype(np.float)/255.
# return hsv
ft = [scipy.fftpack.fft2(hsv[...,c]) for c in range(3)]
# scph = [np.angle(ft[c]).ravel()[perm].reshape(ft[c].shape) for c in range(3)]
# scph = [np.angle(ft[c])+np.angle(ft[c]).ravel()[perm].reshape(ft[c].shape) for c in range(3)]
# scph = [np.angle(ft[c]) for c in range(3)]
scph = [np.mod(np.angle(ft[c])+noise+3*np.pi,2*np.pi)-np.pi for c in range(3)]
ftsc = [np.abs(ft[c])*np.exp(scph[c]*1j) for c in range(3)]
ift = [scipy.fftpack.ifft2(ftsc[c]) for c in range(3)]
# print [(np.abs(c[...,np.newaxis]).min(),np.abs(c[...,np.newaxis]).max()) for c in ift]
oframe = (np.dstack([np.abs(c[...,np.newaxis]) for c in ift]))
maxs = np.apply_over_axes(np.max,oframe,[0,1])
# print maxs.ravel()
oframe[...,maxs.ravel()>=1] /= maxs[maxs>=1]
# print oframe.max()
bgr = cv2.cvtColor((oframe*255).astype(np.uint8), cv2.COLOR_LAB2BGR)
# print frame.shape,frame.dtype
# print bgr.shape, bgr.dtype
out.write(bgr)
# out.write(frame)
del hsv, ft, ftsc, ift, oframe, bgr
ret, frame = cap.read()
cap.release()
del out
cv2.destroyAllWindows()