-
Notifications
You must be signed in to change notification settings - Fork 40
/
phasebasedMoMag.py
164 lines (122 loc) · 4.67 KB
/
phasebasedMoMag.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
from perceptual.filterbank import *
import cv2
# determine what OpenCV version we are using
try:
import cv2.cv as cv
USE_CV2 = True
except ImportError:
# OpenCV 3.x does not have cv2.cv submodule
USE_CV2 = False
import sys
import numpy as np
from pyr2arr import Pyramid2arr
from temporal_filters import IdealFilterWindowed, ButterBandpassFilter
def phaseBasedMagnify(vidFname, vidFnameOut, maxFrames, windowSize, factor, fpsForBandPass, lowFreq, highFreq):
# initialize the steerable complex pyramid
steer = Steerable(5)
pyArr = Pyramid2arr(steer)
print "Reading:", vidFname,
# get vid properties
vidReader = cv2.VideoCapture(vidFname)
if USE_CV2:
# OpenCV 2.x interface
vidFrames = int(vidReader.get(cv.CV_CAP_PROP_FRAME_COUNT))
width = int(vidReader.get(cv.CV_CAP_PROP_FRAME_WIDTH))
height = int(vidReader.get(cv.CV_CAP_PROP_FRAME_HEIGHT))
fps = int(vidReader.get(cv.CV_CAP_PROP_FPS))
func_fourcc = cv.CV_FOURCC
else:
# OpenCV 3.x interface
vidFrames = int(vidReader.get(cv2.CAP_PROP_FRAME_COUNT))
width = int(vidReader.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(vidReader.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(vidReader.get(cv2.CAP_PROP_FPS))
func_fourcc = cv2.VideoWriter_fourcc
if np.isnan(fps):
fps = 30
print ' %d frames' % vidFrames,
print ' (%d x %d)' % (width, height),
print ' FPS:%d' % fps
# video Writer
fourcc = func_fourcc('M', 'J', 'P', 'G')
vidWriter = cv2.VideoWriter(vidFnameOut, fourcc, int(fps), (width,height), 1)
print 'Writing:', vidFnameOut
# how many frames
nrFrames = min(vidFrames, maxFrames)
# read video
#print steer.height, steer.nbands
# setup temporal filter
filter = IdealFilterWindowed(windowSize, lowFreq, highFreq, fps=fpsForBandPass, outfun=lambda x: x[0])
#filter = ButterBandpassFilter(1, lowFreq, highFreq, fps=fpsForBandPass)
print 'FrameNr:',
for frameNr in range( nrFrames + windowSize ):
print frameNr,
sys.stdout.flush()
if frameNr < nrFrames:
# read frame
_, im = vidReader.read()
if im is None:
# if unexpected, quit
break
# convert to gray image
if len(im.shape) > 2:
grayIm = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY)
else:
# already a grayscale image?
grayIm = im
# get coeffs for pyramid
coeff = steer.buildSCFpyr(grayIm)
# add image pyramid to video array
# NOTE: on first frame, this will init rotating array to store the pyramid coeffs
arr = pyArr.p2a(coeff)
phases = np.angle(arr)
# add to temporal filter
filter.update([phases])
# try to get filtered output to continue
try:
filteredPhases = filter.next()
except StopIteration:
continue
print '*',
# motion magnification
magnifiedPhases = (phases - filteredPhases) + filteredPhases*factor
# create new array
newArr = np.abs(arr) * np.exp(magnifiedPhases * 1j)
# create pyramid coeffs
newCoeff = pyArr.a2p(newArr)
# reconstruct pyramid
out = steer.reconSCFpyr(newCoeff)
# clip values out of range
out[out>255] = 255
out[out<0] = 0
# make a RGB image
rgbIm = np.empty( (out.shape[0], out.shape[1], 3 ) )
rgbIm[:,:,0] = out
rgbIm[:,:,1] = out
rgbIm[:,:,2] = out
#write to disk
res = cv2.convertScaleAbs(rgbIm)
vidWriter.write(res)
# free the video reader/writer
vidReader.release()
vidWriter.release()
################# main script
#vidFname = 'media/baby.mp4';
#vidFname = 'media/WIN_20151208_17_11_27_Pro.mp4.normalized.avi'
#vidFname = 'media/embryos01_30s.mp4'
vidFname = 'media/guitar.mp4'
# maximum nr of frames to process
maxFrames = 60000
# the size of the sliding window
windowSize = 30
# the magnifaction factor
factor = 20
# the fps used for the bandpass
fpsForBandPass = 600 # use -1 for input video fps
# low ideal filter
lowFreq = 72
# high ideal filter
highFreq = 92
# output video filename
vidFnameOut = vidFname + '-Mag%dIdeal-lo%d-hi%d.avi' % (factor, lowFreq, highFreq)
phaseBasedMagnify(vidFname, vidFnameOut, maxFrames, windowSize, factor, fpsForBandPass, lowFreq, highFreq)