|
import numpy as np |
|
import cv2 |
|
|
|
import scipy |
|
|
|
|
|
def gaussian_video(video, pyramid_levels): |
|
"""Create a gaussian representation of a video""" |
|
vid_data = None |
|
for x in range(0, video.shape[0]): |
|
frame = video[x] |
|
gauss_copy = np.ndarray(shape=frame.shape, dtype="float") |
|
gauss_copy[:] = frame |
|
for i in range(pyramid_levels): |
|
gauss_copy = gausPyrDown(gauss_copy) |
|
if x == 0: |
|
vid_data = np.zeros((video.shape[0], gauss_copy.shape[0], gauss_copy.shape[1], 3)) |
|
vid_data[x] = gauss_copy |
|
return vid_data |
|
|
|
def gausPyrDown(frame,sz=5,sigma=1): |
|
height, width, channel = frame.shape |
|
convFrame = np.zeros(shape=(height, width,channel), dtype="float") |
|
kernel = gausKernel(sz=sz,sigma=sigma) |
|
for channel_i in range(channel): |
|
convFrame[:,:,channel_i] = scipy.ndimage.convolve(frame[:,:,channel_i],kernel) |
|
downFrame = convFrame[::2,::2,:] |
|
return downFrame |
|
|
|
def gausPyrUp(frame,sz=5,sigma=1): |
|
height, width, channel = frame.shape |
|
upFrame = np.zeros(shape=(2*height, 2*width,channel), dtype="float") |
|
kernel = gausKernel(sz=sz,sigma=sigma) |
|
for channel_i in range(channel): |
|
upFrame[::2,::2,channel_i] = frame[:,:,channel_i] |
|
upFrame[:,:,channel_i] = scipy.ndimage.convolve(upFrame[:,:,channel_i],kernel*4) |
|
return upFrame |
|
|
|
|
|
def gausKernel(sz = 5,sigma=1): |
|
kernel = np.zeros((sz,sz)) |
|
|
|
sz_1 = int(sz/2) |
|
for x in (np.arange(sz)-sz_1): |
|
for y in (np.arange(sz)-sz_1): |
|
ix,iy = x+sz_1,y+sz_1 |
|
kernel[ix,iy] = np.exp(-(x**2+y**2)/(2.0*sigma**2)) |
|
kernel = kernel / np.sum(kernel) |
|
|
|
return kernel |
|
|
|
|
|
def temporal_bandpass_filter(data, fps, freq_min=0.833, freq_max=1, axis=0): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data_process = np.zeros(data.shape) |
|
sample_interval = 1.0/fps |
|
for x in range(data.shape[1]): |
|
for y in range(data.shape[2]): |
|
for z in range(data.shape[3]): |
|
|
|
data_process[:,x,y,z] = bandpass_filter(data[:,x,y,z], sample_interval, freq_min, freq_max) |
|
return data_process |
|
|
|
|
|
|
|
|
|
def bandpass_filter(x, sample_interval, freq_min, freq_max): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
X = np.fft.fft(x) |
|
frequencies = np.fft.fftfreq(len(x), d=sample_interval) |
|
bound_low = (np.abs(frequencies - freq_min)).argmin() |
|
bound_high = (np.abs(frequencies - freq_max)).argmin() |
|
X[:bound_low] = 0 |
|
X[bound_high:-bound_high] = 0 |
|
X[-bound_low:] = 0 |
|
band_pass_signal = np.abs(np.fft.ifft(X, axis=0)) |
|
|
|
return band_pass_signal |
|
|
|
|
|
|
|
def convertScaleAbs(frame): |
|
outFrame = np.ndarray(shape=frame.shape, dtype="uint8") |
|
for channel_i in range(3): |
|
outFrame[:,:,channel_i] = np.clip(np.abs(frame[:,:,channel_i]),0,255).astype(np.uint8).copy() |
|
return outFrame |
|
|
|
|
|
def combine_pyramid_and_save(g_video, orig_video, enlarge_multiple, fps): |
|
"""Combine a gaussian video representation with the original""" |
|
|
|
width, height = orig_video.shape[2], orig_video.shape[1] |
|
mag_data = np.zeros(orig_video.shape, dtype='uint8') |
|
for x in range(0, g_video.shape[0]): |
|
img = np.ndarray(shape=g_video[x].shape, dtype='float') |
|
img[:] = g_video[x] |
|
for i in range(enlarge_multiple): |
|
img = gausPyrUp(img) |
|
img[:height, :width] = img[:height, :width] + orig_video[x] |
|
res = convertScaleAbs(img[:height, :width]) |
|
mag_data[x] = res |
|
return mag_data |
|
|
|
|
|
|