-
Notifications
You must be signed in to change notification settings - Fork 24
/
utils.py
152 lines (129 loc) · 4.37 KB
/
utils.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
143
144
145
146
147
148
149
150
151
152
# -*- coding: utf-8 -*-
"""
Created on Sat Dec 10 16:32:39 2016
@author: Rachid & Chaima
"""
import pdb
import numpy as np
import scipy.signal as sig
def kernel_1D(n, a=0.6):
"""Kernel function in 1 dimension"""
kernel = [.0625, .25, .375, .25, .0625]
# if n == 0:
# return a
# elif n == -1 or n == 1:
# return 1./4
# elif n == -2 or n == 2:
# return 1./4 - float(a)/2
# else:
# return 0
return kernel[n]
def kernel_old(m, n, a=0.6):
"""Returns the value of the kernel at position w and n"""
return kernel_1D(m, a)*kernel_1D(n, a)
def get_kernel(a=0.6):
kernel = np.zeros((5,5))
for i in range(5):
for j in range(5):
kernel[i, j] = kernel_1D(i, a)*kernel_1D(j, a)
return kernel
def Reduce_old(image, n, a=0.6):
"""Reduce function for Pyramids"""
try:
if n == 0:
return image
else:
image = Reduce(image, n-1, a)
[R, C] = [image.shape[0], image.shape[1]]
image_extended = np.zeros((R+4, C+4))
image_extended[2:R+2, 2:C+2] = image
try:
image_reduced = np.zeros((R/2, C/2))
except Exception as e:
print "Dimension Error"
print e
for i in range(R/2):
for j in range(C/2):
for m in range(-2, 3):
for n in range(-2, 3):
image_reduced[i, j] += kernel_old(m, n) * image_extended[2 * i + m + 2, 2 * j + n + 2]
return image_reduced
except Exception as e:
print "Dimension Error"
print e
def weighted_sum(image, i, j, a):
weighted_sum = 0
for m in range(-2, 3):
for n in range(-2, 3):
pixel_i = float(i - m) / 2
pixel_j = float(j - n) / 2
if pixel_i.is_integer() and pixel_j.is_integer():
weighted_sum += kernel_old(m, n, a) * image[pixel_i, pixel_j]
return 4 * weighted_sum
def Expand_old(image, n, a=0.6):
"""Expand function for Pyramids"""
try:
if n == 0:
return image
else:
image = Expand(image, n-1, a)
[R, C] = image.shape
image_extended = np.zeros((R+4, C+4))
image_extended[2:R+2, 2:C+2] = image
new_floor = np.zeros((2 * R, 2 * C))
for i in range(2 * R):
for j in range(2 * C):
new_floor[i, j] = weighted_sum(image_extended, i+2, j+2, a)
new_floor = (new_floor - np.min(new_floor))
new_floor = new_floor / np.max(new_floor)
return new_floor
except Exception as e:
print "Dimension error"
print e
def Reduce1(image, a=0.6):
kernel = get_kernel(a)
shape = image.shape
if len(shape) == 3:
image_reduced = np.zeros((shape[0]/2, shape[1]/2, 3))
for canal in range(3):
canal_reduced = sig.convolve2d(image[:, :, canal], kernel, 'same')
image_reduced[:, :, canal] = canal_reduced[::2, ::2]
else:
image_reduced = sig.convolve2d(image, kernel, 'same')[::2, ::2]
return image_reduced
def Reduce(image, n, a=0.6):
"""Reduce function for Pyramids"""
try:
if n == 0:
return image
else:
image = Reduce(image, n-1, a)
return Reduce1(image, a)
except Exception as e:
print "Dimension Error"
print e
def Expand1(image, a=0.6):
kernel = get_kernel(a)
shape = image.shape
if len(shape) == 3:
image_to_expand = np.zeros((2*shape[0], 2*shape[1], 3))
image_expanded = np.zeros(image_to_expand.shape)
for canal in range(3):
image_to_expand[::2, ::2, canal] = image[:, :, canal]
image_expanded[:, :, canal] = sig.convolve2d(image_to_expand[:, :, canal], 4*kernel, 'same')
else:
image_to_expand = np.zeros((2 * shape[0], 2 * shape[1]))
image_to_expand[::2, ::2] = image
image_expanded = sig.convolve2d(image_to_expand[:, :], 4*kernel, 'same')
return image_expanded
def Expand(image, n, a=0.6):
"""Expand function for Pyramids"""
try:
if n == 0:
return image
else:
image = Expand(image, n-1, a)
return Expand1(image, a)
except Exception as e:
print "Dimension error"
print e