laplacian_pyramid_blending.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import cv2
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. import matplotlib.image as mpimg
  5. # Read two images
  6. A = cv2.imread("images/man.jpg")
  7. B = cv2.imread("images/woman.jpg")
  8. # Convert to float
  9. A = np.float32(A) / 255.0
  10. B = np.float32(B) / 255.0
  11. # Create a rough mask around man face in A.
  12. mask = np.zeros(A.shape,A.dtype)
  13. polygon = np.array([[164,226], [209,225], [238,188], [252,133], [248,75], [240,29], [192,15], [150,15], [100,70], [106,133], [123,194] ], np.int32)
  14. cv2.fillPoly(mask, [polygon], (255, 255, 255))
  15. # Convert the mask to float
  16. mask = np.float32(mask) / 255.0
  17. # Multiply with float < 1.0 to take weighted average of man and woman's face
  18. mask = mask * 0.7 # 0.7 for man, 0.3 for woman
  19. # Resizing to multiples of 2^(levels in pyramid), thus 32 in our case
  20. A = cv2.resize(A,(384,352))
  21. # B and mask should have same size as A for multiplication and addition operations later
  22. B = cv2.resize(B,(A.shape[1],A.shape[0]))
  23. mask = cv2.resize(mask,(A.shape[1],A.shape[0]))
  24. # Start with original images (base of pyramids)
  25. guassianA = A.copy()
  26. guassianB = B.copy()
  27. guassianMask = mask.copy()
  28. # combined laplacian pyramids of both images
  29. combinedLaplacianPyramids = []
  30. # Number of levels in pyramids, try with different values, Be careful with image sizes
  31. maxIterations = 5
  32. for i in range(maxIterations):
  33. # compute laplacian pyramids for both images
  34. laplacianA = cv2.subtract(guassianA, cv2.pyrUp(cv2.pyrDown(guassianA)))
  35. laplacianB = cv2.subtract(guassianB, cv2.pyrUp(cv2.pyrDown(guassianB)))
  36. # Combine both laplacian pyramids, taking weighted average with guassian pyramid of mask
  37. combinedLaplacian = guassianMask * laplacianA + (1.0 - guassianMask) * laplacianB
  38. # add combinedLaplacian in the beginning of the list of combined laplacian pyramids
  39. combinedLaplacianPyramids.insert(0,combinedLaplacian)
  40. # Update guassian pyramids for next iteration
  41. guassianA = cv2.pyrDown(guassianA)
  42. guassianB = cv2.pyrDown(guassianB)
  43. guassianMask = cv2.pyrDown(guassianMask)
  44. # Add last combination of laplacian pyramids (top level of pyramids)
  45. lastCombined = guassianMask * guassianA + (1.0 - guassianMask) * guassianB
  46. combinedLaplacianPyramids.insert(0,lastCombined)
  47. # reconstructing image
  48. blendedImage = combinedLaplacianPyramids[0]
  49. for i in xrange(1,len(combinedLaplacianPyramids)):
  50. # upSample and add to next level
  51. blendedImage = cv2.pyrUp(blendedImage)
  52. blendedImage = cv2.add(blendedImage, combinedLaplacianPyramids[i])
  53. cv2.imshow('Blended',blendedImage)
  54. # direct blending both images for comparison
  55. directCombination = mask * A + (1.0 - mask) * B
  56. cv2.imshow('Direct combination',directCombination)
  57. cv2.waitKey(0)