Innerhlab dieses Repositorys ist ein showcase ausgearbeitet, welcher live die Funktion des EVM Algorithmus darstellt.
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.

pyramid.py 3.7KB

3 weeks ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #..................................
  2. #........Visualisierung 2..........
  3. #..................................
  4. #...Eulerian Video Magnification...
  5. #..................................
  6. #.. Author: Galya Pavlova..........
  7. #..................................
  8. import cv2
  9. import numpy as np
  10. def create_gaussian_pyramid(image, levels):
  11. '''
  12. Creates a Gaussian pyramid for each image.
  13. :param image: An image, i.e video frame
  14. :param levels: The Gaussian pyramid level
  15. :return: Returns a pyramid of nr. levels images
  16. '''
  17. gauss = image.copy()
  18. gauss_pyr = [gauss]
  19. for level in range(1, levels):
  20. gauss = cv2.pyrDown(gauss)
  21. gauss_pyr.append(gauss)
  22. return gauss_pyr
  23. def gaussian_video(video_tensor, levels):
  24. '''
  25. For a given video sequence the function creates a video with
  26. the highest (specified by levels) Gaussian pyramid level
  27. :param video_tensor: Video sequence
  28. :param levels: Specifies the Gaussian pyramid levels
  29. :return: a video sequence where each frame is the downsampled of the original frame
  30. '''
  31. for i in range(0, video_tensor.shape[0]):
  32. frame = video_tensor[i]
  33. pyr = create_gaussian_pyramid(frame, levels)
  34. gaussian_frame = pyr[-1] # use only highest gaussian level
  35. if i == 0: # initialize one time
  36. vid_data = np.zeros((video_tensor.shape[0], gaussian_frame.shape[0], gaussian_frame.shape[1], 3))
  37. vid_data[i] = gaussian_frame
  38. return vid_data
  39. def create_laplacian_pyramid(image, levels):
  40. '''
  41. Builds a Laplace pyramid for an image, i.e. video frame
  42. :param image: Image, i.e. single video frame
  43. :param levels: Specifies the Laplace pyramid levels
  44. :return: Returns a pyramid of nr. levels images
  45. '''
  46. gauss_pyramid = create_gaussian_pyramid(image, levels)
  47. laplace_pyramid = []
  48. for i in range(levels-1):
  49. size = (gauss_pyramid[i].shape[1], gauss_pyramid[i].shape[0]) # reshape
  50. laplace_pyramid.append(gauss_pyramid[i]-cv2.pyrUp(gauss_pyramid[i+1], dstsize=size))
  51. laplace_pyramid.append(gauss_pyramid[-1]) # add last gauss pyramid level
  52. return laplace_pyramid
  53. def laplacian_video_pyramid(video_stack, levels):
  54. '''
  55. Creates a Laplacian pyramid for the whole video sequence
  56. :param video_stack: Video sequence
  57. :param levels: Specifies the Laplace pyramid levels
  58. :return: A two-dimensional array where the first index is used for the pyramid levels
  59. and the second for each video frame
  60. '''
  61. print('Build laplace pyramid')
  62. # "2 dimensional" array - first index for pyramid level, second for frames
  63. laplace_video_pyramid = [[0 for x in range(video_stack.shape[0])] for x in range(levels)]
  64. for i in range(video_stack.shape[0]):
  65. frame = video_stack[i]
  66. pyr = create_laplacian_pyramid(frame, levels)
  67. for n in range(levels):
  68. laplace_video_pyramid[n][i] = pyr[n]
  69. return laplace_video_pyramid
  70. def reconstruct(filtered_video, levels):
  71. '''
  72. Reconstructs a video sequence from the filtered Laplace video pyramid
  73. :param filtered_video: 2 dimensional video sequence - 1st. index pyramid levels, 2nd. - video frames
  74. :param levels: pyramid levels
  75. :return: video sequence
  76. '''
  77. print('Reconstruct video')
  78. final = np.empty(filtered_video[0].shape)
  79. for i in range(filtered_video[0].shape[0]): # iterate through frames
  80. up = filtered_video[-1][i] # highest level
  81. for k in range(levels-1, 0, -1): # going down to lowest level
  82. size = (filtered_video[k-1][i].shape[1], filtered_video[k-1][i].shape[0]) # reshape
  83. up = cv2.pyrUp(up, dstsize=size) + filtered_video[k-1][i]
  84. final[i] = up
  85. return final