-
Notifications
You must be signed in to change notification settings - Fork 0
/
one.py
84 lines (65 loc) · 2.28 KB
/
one.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
"""One pixel camera simulation"""
import math
import matplotlib.pyplot as plt
import numpy as np
import scipy.misc
import skimage.exposure
from skimage.measure import compare_ssim as ssim
def base_convert(number, base):
"""Convert number to a numerical base"""
result = []
if number == 0:
return [0]
while number > 0:
result.insert(0, number % base)
number = number // base
return result
def hadamard(size, vector):
"""Return a hadamard matrix"""
# size is 2 power to order
order = int(math.log(size, 2))
vector = vector[::-1]
v_m = [[1, 1, 1, -1], [1, 1, -1, 1], [1, -1, 1, 1], [-1, 1, 1, 1]]
h_m = np.array([[1]])
for i in xrange(0, order):
h_m1 = np.concatenate((v_m[vector[i]][0] * h_m,
v_m[vector[i]][2] * h_m))
h_m2 = np.concatenate((v_m[vector[i]][1] * h_m,
v_m[vector[i]][3] * h_m))
h_m = np.concatenate((h_m1, h_m2), 1)
return h_m
def generate_hadamard(size, number):
"""Generate a n hadamard matrix """
matrix_vector = []
for i in xrange(0, number):
matrix_code = base_convert(i, 4)
padding = np.zeros(7 - len(matrix_code), dtype="int")
matrix_code = np.concatenate((padding, matrix_code))
hadamard_matrix = hadamard(size, matrix_code)
matrix_vector.append(hadamard_matrix)
return matrix_vector
TEST_IMAGE = scipy.misc.face()
TEST_IMAGE = TEST_IMAGE[:, :, 1]
TEST_IMAGE = scipy.misc.imresize(TEST_IMAGE, [64, 64])
TEST_IMAGE = TEST_IMAGE.astype("float64")
nn = 4**7
M = 4**7
HADAMARD_MASKS = generate_hadamard(TEST_IMAGE.shape[0], nn)
np.random.shuffle(HADAMARD_MASKS)
result = np.ones(TEST_IMAGE.shape)
for i in range(0, M):
mask = HADAMARD_MASKS[i] == 1
mask = mask * 1
masked = TEST_IMAGE * HADAMARD_MASKS[i]
intensity = masked.sum(dtype="float64")
pixels = HADAMARD_MASKS[i].sum(dtype="float64")
result += intensity * HADAMARD_MASKS[i]
result = skimage.exposure.rescale_intensity(result, out_range=(0, 255))
print ssim(result, TEST_IMAGE)
fig = plt.figure()
fig.add_subplot(1, 2, 1)
plt.gray()
plt.imshow(TEST_IMAGE)
fig.add_subplot(1, 2, 2)
plt.imshow(result)
plt.show()