Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/learnopencv
Path: blob/master/FunnyMirrors/FunnyMirrorsVideo.py
3118 views
1
import cv2
2
import numpy as np
3
import math
4
from vcam import vcam,meshGen
5
import sys
6
7
8
cap = cv2.VideoCapture(sys.argv[1])
9
ret, img = cap.read()
10
11
H,W = img.shape[:2]
12
fps = 30
13
14
# Creating the virtual camera object
15
c1 = vcam(H=H,W=W)
16
17
# Creating the surface object
18
plane = meshGen(H,W)
19
20
mode = int(sys.argv[2])
21
22
# We generate a mirror where for each 3D point, its Z coordinate is defined as Z = F(X,Y)
23
if mode == 0:
24
plane.Z += 20*np.exp(-0.5*((plane.X*1.0/plane.W)/0.1)**2)/(0.1*np.sqrt(2*np.pi))
25
elif mode == 1:
26
plane.Z += 20*np.exp(-0.5*((plane.Y*1.0/plane.H)/0.1)**2)/(0.1*np.sqrt(2*np.pi))
27
elif mode == 2:
28
plane.Z -= 10*np.exp(-0.5*((plane.X*1.0/plane.W)/0.1)**2)/(0.1*np.sqrt(2*np.pi))
29
elif mode == 3:
30
plane.Z -= 10*np.exp(-0.5*((plane.Y*1.0/plane.W)/0.1)**2)/(0.1*np.sqrt(2*np.pi))
31
elif mode == 4:
32
plane.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))
33
elif mode == 5:
34
plane.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))
35
elif mode == 6:
36
plane.Z += 100*np.sqrt((plane.X*1.0/plane.W)**2+(plane.Y*1.0/plane.H)**2)
37
elif mode == 7:
38
plane.Z -= 100*np.sqrt((plane.X*1.0/plane.W)**2+(plane.Y*1.0/plane.H)**2)
39
else:
40
print("Wrong mode selected")
41
exit(-1)
42
43
# Extracting the generated 3D plane
44
pts3d = plane.getPlane()
45
46
# Projecting (Capturing) the plane in the virtual camera
47
pts2d = c1.project(pts3d)
48
49
# Deriving mapping functions for mesh based warping.
50
map_x,map_y = c1.getMaps(pts2d)
51
52
ret, img = cap.read()
53
54
while 1:
55
ret, img = cap.read()
56
if ret:
57
output = cv2.remap(img,map_x,map_y,interpolation=cv2.INTER_LINEAR,borderMode=4)
58
output = cv2.flip(output,1)
59
out1 = np.hstack((img,output))
60
out1 = cv2.resize(out1,(700,350))
61
cv2.imshow("output",out1)
62
if cv2.waitKey(1)&0xFF == 27:
63
break
64
else:
65
break
66