FunnyMirrorsImages.py 2.1 KB

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