Path: blob/master/FunnyMirrors/FunnyMirrorsImages.py
3118 views
import cv21import numpy as np2import math3from vcam import vcam,meshGen45paths = ["./data/chess.png","./data/im2.jpeg","./data/img3.jpg"]678for mode in range(8):9for i, path in enumerate(paths):10# Reading the input image11img = cv2.imread(path)12img = cv2.resize(img,(300,300))13H,W = img.shape[:2]1415# Creating the virtual camera object16c1 = vcam(H=H,W=W)1718# Creating the surface object19plane = meshGen(H,W)2021# We generate a mirror where for each 3D point, its Z coordinate is defined as Z = F(X,Y)2223if mode == 0:24plane.Z += 20*np.exp(-0.5*((plane.X*1.0/plane.W)/0.1)**2)/(0.1*np.sqrt(2*np.pi))25elif mode == 1:26plane.Z += 20*np.exp(-0.5*((plane.Y*1.0/plane.H)/0.1)**2)/(0.1*np.sqrt(2*np.pi))27elif mode == 2:28plane.Z -= 10*np.exp(-0.5*((plane.X*1.0/plane.W)/0.1)**2)/(0.1*np.sqrt(2*np.pi))29elif mode == 3:30plane.Z -= 10*np.exp(-0.5*((plane.Y*1.0/plane.W)/0.1)**2)/(0.1*np.sqrt(2*np.pi))31elif mode == 4:32plane.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))33elif mode == 5:34plane.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))35elif mode == 6:36plane.Z += 100*np.sqrt((plane.X*1.0/plane.W)**2+(plane.Y*1.0/plane.H)**2)37elif mode == 7:38plane.Z -= 100*np.sqrt((plane.X*1.0/plane.W)**2+(plane.Y*1.0/plane.H)**2)39else:40print("Wrong mode selected")41exit(-1)4243# Extracting the generated 3D plane44pts3d = plane.getPlane()4546# Projecting (Capturing) the plane in the virtual camera47pts2d = c1.project(pts3d)4849# Deriving mapping functions for mesh based warping.50map_x,map_y = c1.getMaps(pts2d)5152# Generating the output53output = cv2.remap(img,map_x,map_y,interpolation=cv2.INTER_LINEAR)54output = cv2.flip(output,1)5556cv2.imshow("Funny Mirror",output)57cv2.imshow("Input and output",np.hstack((img,np.zeros((H,2,3),dtype=np.uint8),output)))58# Uncomment following line to save the outputs59# cv2.imwrite("Mirror-effect-%d-image-%d.jpg"%(mode+1,i+1),np.hstack((img,np.zeros((H,2,3),dtype=np.uint8),output)))60cv2.waitKey(0)6162