You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pyramids.py 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import cv2
  2. import numpy as np
  3. # Build Gaussian image pyramid
  4. def build_gaussian_pyramid(img, levels):
  5. float_img = np.ndarray(shape=img.shape, dtype="float")
  6. float_img[:] = img
  7. pyramid = [float_img]
  8. for i in range(levels-1):
  9. float_img = cv2.pyrDown(float_img)
  10. pyramid.append(float_img)
  11. return pyramid
  12. # Build Laplacian image pyramid from Gaussian pyramid
  13. def build_laplacian_pyramid(img, levels):
  14. gaussian_pyramid = build_gaussian_pyramid(img, levels)
  15. laplacian_pyramid = []
  16. for i in range(levels-1):
  17. upsampled = cv2.pyrUp(gaussian_pyramid[i+1])
  18. (height, width, depth) = upsampled.shape
  19. gaussian_pyramid[i] = cv2.resize(gaussian_pyramid[i], (height, width))
  20. diff = cv2.subtract(gaussian_pyramid[i],upsampled)
  21. laplacian_pyramid.append(diff)
  22. laplacian_pyramid.append(gaussian_pyramid[-1])
  23. return laplacian_pyramid
  24. # Build video pyramid by building Laplacian pyramid for each frame
  25. def build_video_pyramid(frames):
  26. lap_video = []
  27. for i, frame in enumerate(frames):
  28. pyramid = build_laplacian_pyramid(frame, 3)
  29. for j in range(3):
  30. if i == 0:
  31. lap_video.append(np.zeros((len(frames), pyramid[j].shape[0], pyramid[j].shape[1], 3)))
  32. lap_video[j][i] = pyramid[j]
  33. return lap_video
  34. # Collapse video pyramid by collapsing each frame's Laplacian pyramid
  35. def collapse_laplacian_video_pyramid(video, frame_ct):
  36. collapsed_video = []
  37. for i in range(frame_ct):
  38. prev_frame = video[-1][i]
  39. for level in range(len(video) - 1, 0, -1):
  40. pyr_up_frame = cv2.pyrUp(prev_frame)
  41. (height, width, depth) = pyr_up_frame.shape
  42. prev_level_frame = video[level - 1][i]
  43. prev_level_frame = cv2.resize(prev_level_frame, (height, width))
  44. prev_frame = pyr_up_frame + prev_level_frame
  45. # Normalize pixel values
  46. min_val = min(0.0, prev_frame.min())
  47. prev_frame = prev_frame + min_val
  48. max_val = max(1.0, prev_frame.max())
  49. prev_frame = prev_frame / max_val
  50. prev_frame = prev_frame * 255
  51. prev_frame = cv2.convertScaleAbs(prev_frame)
  52. collapsed_video.append(prev_frame)
  53. return collapsed_video