I am currently working on a school project where I am trying to simulate an LSD trip using a webcam and video processing effects. I am using python and opencv to accomplish this, but I am having trouble figuring out how to create/apply a certain effect that acts like a "drift/morph/melt/flow" to the webcam footage.
I have attached an example of the effect I am trying to achieve. It looks like the image is slowly melting and distorting, almost as if it is being pulled in multiple directions at once.
-
import numpy as np import cv2 from PIL import Image img = cv2.imread("bluecar_sm.jpg") # get dimensions h, w = img.shape[:2] # set wavelength wave_x = 2*w wave_y = h # set amount, number of frames and delay amount_x = 10 amount_y = 5 num_frames = 100 delay = 50 border_color = (128,128,128) # create X and Y ramps x = np.arange(w, dtype=np.float32) y = np.arange(h, dtype=np.float32) frames = [] # loop and change phase for i in range(0,num_frames): # compute phase to increment over 360 degree for number of frames specified so makes full cycle phase_x = i*360/num_frames phase_y = phase_x # create sinusoids in X and Y, add to ramps and tile out to fill to size of image x_sin = amount_x * np.sin(2 * np.pi * (x/wave_x phase_x/360)) x map_x = np.tile(x_sin, (h,1)) y_sin = amount_y * np.sin(2 * np.pi * (y/wave_y phase_y/360)) y map_y = np.tile(y_sin, (w,1)).transpose() # do the warping using remap result = cv2.remap(img.copy(), map_x, map_y, cv2.INTER_CUBIC, borderMode = cv2.BORDER_CONSTANT, borderValue=border_color) # show result cv2.imshow('result', result) cv2.waitKey(delay) # convert to PIL format and save frames result = cv2.cvtColor(result, cv2.COLOR_BGR2RGB) pil_result = Image.fromarray(result) frames.append(pil_result) # write animated gif from frames using PIL frames[0].save('bluecar_sm_animation.gif',save_all=True, append_images=frames[1:], optimize=False, duration=delay, loop=0)