Path: blob/master/FunnyMirrors/FunnyMirrorsVideo.py
3118 views
import cv21import numpy as np2import math3from vcam import vcam,meshGen4import sys567cap = cv2.VideoCapture(sys.argv[1])8ret, img = cap.read()910H,W = img.shape[:2]11fps = 301213# Creating the virtual camera object14c1 = vcam(H=H,W=W)1516# Creating the surface object17plane = meshGen(H,W)1819mode = int(sys.argv[2])2021# We generate a mirror where for each 3D point, its Z coordinate is defined as Z = F(X,Y)22if mode == 0:23plane.Z += 20*np.exp(-0.5*((plane.X*1.0/plane.W)/0.1)**2)/(0.1*np.sqrt(2*np.pi))24elif mode == 1:25plane.Z += 20*np.exp(-0.5*((plane.Y*1.0/plane.H)/0.1)**2)/(0.1*np.sqrt(2*np.pi))26elif mode == 2:27plane.Z -= 10*np.exp(-0.5*((plane.X*1.0/plane.W)/0.1)**2)/(0.1*np.sqrt(2*np.pi))28elif mode == 3:29plane.Z -= 10*np.exp(-0.5*((plane.Y*1.0/plane.W)/0.1)**2)/(0.1*np.sqrt(2*np.pi))30elif mode == 4:31plane.Z += 20*np.sin(2*np.pi*((plane.X-plane.W/4.0)/plane.W)) + 20*np.sin(2*np.pi*((plane.Y-plane.H/4.0)/plane.H))32elif mode == 5:33plane.Z -= 20*np.sin(2*np.pi*((plane.X-plane.W/4.0)/plane.W)) - 20*np.sin(2*np.pi*((plane.Y-plane.H/4.0)/plane.H))34elif mode == 6:35plane.Z += 100*np.sqrt((plane.X*1.0/plane.W)**2+(plane.Y*1.0/plane.H)**2)36elif mode == 7:37plane.Z -= 100*np.sqrt((plane.X*1.0/plane.W)**2+(plane.Y*1.0/plane.H)**2)38else:39print("Wrong mode selected")40exit(-1)4142# Extracting the generated 3D plane43pts3d = plane.getPlane()4445# Projecting (Capturing) the plane in the virtual camera46pts2d = c1.project(pts3d)4748# Deriving mapping functions for mesh based warping.49map_x,map_y = c1.getMaps(pts2d)5051ret, img = cap.read()5253while 1:54ret, img = cap.read()55if ret:56output = cv2.remap(img,map_x,map_y,interpolation=cv2.INTER_LINEAR,borderMode=4)57output = cv2.flip(output,1)58out1 = np.hstack((img,output))59out1 = cv2.resize(out1,(700,350))60cv2.imshow("output",out1)61if cv2.waitKey(1)&0xFF == 27:62break63else:64break6566