FunnyMirrorsVideo.py 1.8 KB

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