-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsearch.py
142 lines (123 loc) · 5.65 KB
/
search.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
from numpy.distutils.system_info import f2py_info
__author__ = 'davidsiecinski'
from abc import ABCMeta, abstractmethod
from scipy import interpolate
from scipy import ndimage
import numpy
class Search:
__metaclass__ = ABCMeta
def __init__(self, current_picture, referenced_picture, n=2, p=2,useIntrpolation=True):
self.current_picture = current_picture
self.referenced_picture = referenced_picture
self.n = n # size of macroblock
self.p = p # defines searched area [-p,p]
self.x = 0 # curent coordinate of bottom left corner of macroblock
self.y = 0 # curent coordinate of bottom left corner of macroblock
self.useIntrpolation=useIntrpolation
if useIntrpolation:
self.current_picture_interpolated = self.imageInterpolation(self.current_picture)
self.referenced_picture_interpolated = self.imageInterpolation(self.referenced_picture)
self.p = p*2
self.n = n*2
self.numOfcomparedMacroblocks=0
def setCurrentPicture(self, current_picture):
self.picture = current_picture
def setReferencedPicture(self, referenced_picture):
self.referenced_picture = referenced_picture
# remember that y is reversed
# use __makroBlock__ to get value
def __position___(self, i, j,):
y = self.y + i
x = self.x + j
return y, x
# macroblock is defined by top left corner and size self.N
# returns a cut part of picture
def __makroBlock__(self, i, j, isCurrent=True, isInterpolated=False):
y, x = self.__position___(i, j)
if isCurrent:
# print "ref_pict[",y,"]","[",x,"]= ",list(reversed(self.current_picture))[y][x]
# print "row",list(reversed(self.current_picture))[y]
if isInterpolated:
return list(reversed(self.current_picture_interpolated))[y][x]
else:
return list(reversed(self.current_picture))[y][x]
else:
if isInterpolated:
return list(reversed(self.referenced_picture_interpolated))[y][x]
else:
return list(reversed(self.referenced_picture))[y][x]
@abstractmethod
def motionVector(self):
print "Vector motion"
def motionEstimation(self):
current_picture=None
if self.useIntrpolation:
current_picture=self.current_picture_interpolated
else :
current_picture=self.current_picture
num_of_macroblocs_in_y = len(current_picture) / self.n
num_of_macroblocs_in_x = len(current_picture[0]) / self.n
result = []
for y in range(num_of_macroblocs_in_y):
row = []
for x in range(num_of_macroblocs_in_x):
self.x = x * self.n
self.y = y * self.n
row.append(self.motionVector( self.useIntrpolation))
result.append(row)
return result
def imageInterpolation(self,image):
y=range(len(image))
x=range(len(image[0]))
f = interpolate.interp2d(x, y, image)
xx= [x * 0.5 for x in range(2*len(image[0]))]
yy=[x * 0.5 for x in range(2*len(image))]
interpolated_image=f(xx,yy).tolist()
return interpolated_image
def imageDownScaling(self,interpolated_image):
np_array=numpy.array(interpolated_image)
np_array_small=ndimage.interpolation.zoom(np_array,.5,order=5)
return np_array_small.tolist()
def createCompressedImage(self):
current_picture=None
if self.useIntrpolation:
current_picture=self.current_picture_interpolated
else :
current_picture=self.current_picture
num_of_macroblocs_in_y = len(current_picture) / self.n
num_of_macroblocs_in_x = len(current_picture[0]) / self.n
mE = self.motionEstimation()
compressedImage = [0] * len(current_picture)
for i in range(len(compressedImage)):
compressedImage[i] = [0] * len(current_picture[0])
try:
for y_ in range(num_of_macroblocs_in_y):
for x_ in range(num_of_macroblocs_in_x):
x = x_ * self.n
y = y_ * self.n
for n1 in range(self.n):
for m1 in range(self.n):
offset_y = mE[y_][x_][0]
offset_x = mE[y_][x_][1]
ey = y + n1 + offset_y
ex = x + m1 + offset_x
if ey < 0 or len(current_picture) <= ey or ex < 0 or len(current_picture[0]) <= ex:
print "skipped: ey= ",ey," ex= ",ex," len(current_picture) ",len(current_picture)," len(current_picture[0])",len(current_picture[0])
continue
cy = y + n1
cx = x + m1
compressedImage[ey][ex] = current_picture[cy][cx]
except IndexError as e:
print "Error: ", e.message, e.args
print "compressedImage size: [", len(compressedImage), "][", y + n1 + offset_y * self.n, "]"
print "value: [", y + n1 + offset_y * self.n, "][", x + m1 + offset_x * self.n, "]"
print "y=", y, " n1=", n1, " offset_y", offset_y, " n=", self.n, " x=", x, " m1=", m1, " offset_x=", offset_x
print "current_picture[", y + n1, "][", x + m1, "]= ", current_picture[y + n1][x + m1]
raise e
exit()
except TypeError as e:
print e.message
if self.useIntrpolation:
return self.imageDownScaling(compressedImage)
else:
return compressedImage