Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/learnopencv
Path: blob/master/FunnyMirrors/FunnyMirrorsImages.py
3118 views
1
import cv2
2
import numpy as np
3
import math
4
from vcam import vcam,meshGen
5
6
paths = ["./data/chess.png","./data/im2.jpeg","./data/img3.jpg"]
7
8
9
for mode in range(8):
10
for i, path in enumerate(paths):
11
# Reading the input image
12
img = cv2.imread(path)
13
img = cv2.resize(img,(300,300))
14
H,W = img.shape[:2]
15
16
# Creating the virtual camera object
17
c1 = vcam(H=H,W=W)
18
19
# Creating the surface object
20
plane = meshGen(H,W)
21
22
# We generate a mirror where for each 3D point, its Z coordinate is defined as Z = F(X,Y)
23
24
if mode == 0:
25
plane.Z += 20*np.exp(-0.5*((plane.X*1.0/plane.W)/0.1)**2)/(0.1*np.sqrt(2*np.pi))
26
elif mode == 1:
27
plane.Z += 20*np.exp(-0.5*((plane.Y*1.0/plane.H)/0.1)**2)/(0.1*np.sqrt(2*np.pi))
28
elif mode == 2:
29
plane.Z -= 10*np.exp(-0.5*((plane.X*1.0/plane.W)/0.1)**2)/(0.1*np.sqrt(2*np.pi))
30
elif mode == 3:
31
plane.Z -= 10*np.exp(-0.5*((plane.Y*1.0/plane.W)/0.1)**2)/(0.1*np.sqrt(2*np.pi))
32
elif mode == 4:
33
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))
34
elif mode == 5:
35
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))
36
elif mode == 6:
37
plane.Z += 100*np.sqrt((plane.X*1.0/plane.W)**2+(plane.Y*1.0/plane.H)**2)
38
elif mode == 7:
39
plane.Z -= 100*np.sqrt((plane.X*1.0/plane.W)**2+(plane.Y*1.0/plane.H)**2)
40
else:
41
print("Wrong mode selected")
42
exit(-1)
43
44
# Extracting the generated 3D plane
45
pts3d = plane.getPlane()
46
47
# Projecting (Capturing) the plane in the virtual camera
48
pts2d = c1.project(pts3d)
49
50
# Deriving mapping functions for mesh based warping.
51
map_x,map_y = c1.getMaps(pts2d)
52
53
# Generating the output
54
output = cv2.remap(img,map_x,map_y,interpolation=cv2.INTER_LINEAR)
55
output = cv2.flip(output,1)
56
57
cv2.imshow("Funny Mirror",output)
58
cv2.imshow("Input and output",np.hstack((img,np.zeros((H,2,3),dtype=np.uint8),output)))
59
# Uncomment following line to save the outputs
60
# cv2.imwrite("Mirror-effect-%d-image-%d.jpg"%(mode+1,i+1),np.hstack((img,np.zeros((H,2,3),dtype=np.uint8),output)))
61
cv2.waitKey(0)
62