-
Notifications
You must be signed in to change notification settings - Fork 11
/
pyramid.py
69 lines (53 loc) · 2.24 KB
/
pyramid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import numpy
import cv2
# noinspection PyUnresolvedReferences
import time
# noinspection PyUnresolvedReferences
import numpy as np
def create_gaussian_image_pyramid(image, pyramid_levels):
gauss_copy = numpy.ndarray(shape=image.shape, dtype="float")
gauss_copy[:] = image
img_pyramid = [gauss_copy]
for pyramid_level in range(1, pyramid_levels):
gauss_copy = cv2.pyrDown(gauss_copy)
img_pyramid.append(gauss_copy)
return img_pyramid
def create_laplacian_image_pyramid(image, pyramid_levels):
gauss_pyramid = create_gaussian_image_pyramid(image, pyramid_levels)
laplacian_pyramid = []
for i in range(pyramid_levels - 1):
laplacian_pyramid.append(
(gauss_pyramid[i] - cv2.pyrUp(gauss_pyramid[i + 1],
dstsize=(gauss_pyramid[i].shape[1], gauss_pyramid[i].shape[0]))))
laplacian_pyramid.append(gauss_pyramid[-1])
return laplacian_pyramid
def create_laplacian_video_pyramid(video, pyramid_levels):
return _create_pyramid(video, pyramid_levels, create_laplacian_image_pyramid)
def _create_pyramid(video, pyramid_levels, pyramid_fn):
vid_pyramid = []
# frame_count, height, width, colors = video.shape
for frame_number, frame in enumerate(video):
frame_pyramid = pyramid_fn(frame, pyramid_levels)
for pyramid_level, pyramid_sub_frame in enumerate(frame_pyramid):
if frame_number == 0:
vid_pyramid.append(
numpy.zeros((video.shape[0], pyramid_sub_frame.shape[0], pyramid_sub_frame.shape[1]),
dtype="float"))
vid_pyramid[pyramid_level][frame_number] = pyramid_sub_frame
return vid_pyramid
def collapse_laplacian_pyramid(image_pyramid):
img = image_pyramid.pop()
while image_pyramid:
size = (image_pyramid[-1].shape[1], image_pyramid[-1].shape[0])
img = cv2.pyrUp(img, dstsize=size) + (image_pyramid.pop() - 0)
return img
def collapse_laplacian_video_pyramid(pyramid):
i = 0
while True:
try:
img_pyramid = [vid[i] for vid in pyramid]
pyramid[0][i] = collapse_laplacian_pyramid(img_pyramid)
i += 1
except IndexError:
break
return pyramid[0]