import cv2
import numpy as np
import scipy.fftpack as fftpack
# Build Gaussian Pyramid
def build_gaussian_pyramid(src, level=3):
s = src.copy()
pyramid = [s]
for i in range(level):
s = cv2.pyrDown(s)
pyramid.append(s)
return pyramid
# load video from file
def load_video(video_filename):
cap = cv2.VideoCapture(video_filename)
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
width, height = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))
video_tensor = np.zeros((frame_count, height, width, 3), dtype='float')
x = 0
while cap.isOpened():
ret, frame = cap.read()
if ret is True:
video_tensor[x] = frame
x += 1
else:
break
return video_tensor, fps
# apply temporal ideal bandpass filter to gaussian video
def temporal_ideal_filter(tensor, low, high, fps, axis=0):
fft = fftpack.fft(tensor, axis=axis)
frequencies = fftpack.fftfreq(tensor.shape[0], d=1.0 / fps)
bound_low = (np.abs(frequencies - low)).argmin()
bound_high = (np.abs(frequencies - high)).argmin()
fft[:bound_low] = 0
fft[bound_high:-bound_high] = 0
fft[-bound_low:] = 0
iff = fftpack.ifft(fft, axis=axis)
return np.abs(iff)
# build gaussian pyramid for video
def gaussian_video(video_tensor, levels=3):
for i in range(0, video_tensor.shape[0]):
frame = video_tensor[i]
pyr = build_gaussian_pyramid(frame, level=levels)
gaussian_frame = pyr[-1]
if i == 0:
vid_data = np.zeros((video_tensor.shape[0], gaussian_frame.shape[0], gaussian_frame.shape[1], 3))
vid_data[i] = gaussian_frame
return vid_data
# amplify the video
def amplify_video(gaussian_vid, amplification=50):
return gaussian_vid * amplification
# reconstract video from original video and gaussian video
def reconstract_video(amp_video, origin_video, levels=3):
final_video = np.zeros(origin_video.shape)
for i in range(0, amp_video.shape[0]):
img = amp_video[i]
for x in range(levels):
img = cv2.pyrUp(img)
img = img + origin_video[i]
final_video[i] = img
return final_video
# save video to files
def save_video(video_tensor):
fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')
[height, width] = video_tensor[0].shape[0:2]
writer = cv2.VideoWriter("out.avi", fourcc, 30, (width, height), 1)
for i in range(0, video_tensor.shape[0]):
writer.write(cv2.convertScaleAbs(video_tensor[i]))
writer.release()
# magnify color
def magnify_color(video_name, low, high, levels=3, amplification=20):
t, f = load_video(video_name)
gau_video = gaussian_video(t, levels=levels)
filtered_tensor = temporal_ideal_filter(gau_video, low, high, f)
amplified_video = amplify_video(filtered_tensor, amplification=amplification)
final = reconstract_video(amplified_video, t, levels=3)
save_video(final)
if __name__ == "__main__":
magnify_color("baby.mp4", 0.4, 3)
opencv信号放大
最新推荐文章于 2025-07-01 09:43:33 发布