Repository navigation
Expand file tree
/
Copy pathSistemaEncriptacion_simplus_fcns.py
More file actions
164 lines (134 loc) · 4.78 KB
/
Copy pathSistemaEncriptacion_simplus_fcns.py
File metadata and controls
164 lines (134 loc) · 4.78 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
"""
Created by José Hernán Ortiz Ocampo for the course Optics Instrumentation,
university EAFIT, 2019-2
Module with the functions corresponding to SistemaEncriptacion_simplus_main
Has the functions:
FT
iFT
padding
encryptation
desencryptation
"""
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
def FT(u):
"""
Calculates de 2d fft of u, with the corresponding shifts
"""
U = np.fft.ifftshift(np.fft.fft2(np.fft.fftshift(u)))
return U
def iFT(u):
"""
Calculates de 2d ifft of u, with the corresponding shifts
"""
U = np.fft.ifftshift(np.fft.ifft2(np.fft.fftshift(u)))
return U
def padding(image,n,m,M):
"""
returns numpy MxM matrix with image with size 2*w (square) in the
middle of the matrix, and 0s elsewhere
Inputs:
ui = original matrix, size MxM
image = path to image to joint
L = total size of the image
w = half width of the image to be assembled
"""
im = Image.open(image).convert('L')
u = np.zeros((M,M), dtype=complex)
im_array = np.array(im)
im_array = im_array/255
u[int(M/2-m/2):int(M/2-m/2) + m , int(M/2-n/2):int(M/2-n/2) + n ] = im_array
return u
#%% Encrypting function
def encryptation(image,wi, f1, f2, lambda_ , maskpath, px, doublemask = False) :
"""
Encryption function,
Inputs:
image : path to the image to be encrypted
wi : size of the image to be encrypted ( if rectangular, use the biggest side)
f1 : focal 1 of the 4f encrypting system
f2: focal 2 of the 4f encrypting system
lambda_ : wavelenght used in the system
maskpath : path to the mask image
px : pixel size of the mask
doublemask : activate for double random mask encryption
Outputs:
uo = encrypted wave
um_phase = random phase introduced
xo = exit coordinates
"""
mask = Image.open(maskpath).convert('L')
n,m = mask.size
mask = np.array(mask)
M = max(n,m)
mask_array = np.zeros((M, M))
mask_array[int(M/2-m/2):int(M/2-m/2) + m , int(M/2-n/2):int(M/2-n/2) + n ] = mask
mask_size = px*M
xm= np.array([-mask_size/2, mask_size/2])
fx = xm/(lambda_*f1)
im = Image.open(image)
n,m = im.size
xi = np.array([-wi/2, wi/2])
xo = f1/f2 * xi #the minus sign goes to the inverted image
if doublemask:
ui = padding(image,n,m,M) * np.exp(1j*np.random.rand(M,M) * 2*np.pi)
else:
ui = padding(image,n,m,M)
u3 = FT(ui)
um_phase = mask/255 * 2*np.pi
um = np.exp(1j*um_phase) # only phase mask
u4 = u3*um # mask imposition to the wave
uo = iFT(u4)
uo = uo[::-1, ::-1]
# Source wave
plt.figure()
uiplot = np.abs(ui[int(M/2-m/2):int(M/2-m/2) + m , int(M/2-n/2):int(M/2-n/2) + n ]**2)
plt.imshow(uiplot,extent = [xi[0], xi[-1], xi[0], xi[-1]], cmap = 'gray')
plt.title('Input image')
plt.xlabel('x(mm)')
plt.ylabel('y(mm)')
#randon phase mask
plt.figure()
plt.imshow(np.angle(um),extent = [fx[0], fx[-1], fx[0], fx[-1]], cmap = 'gray')
plt.title(' Random phase mask' )
plt.xlabel('fx(cyc/mm)')
plt.ylabel('fy(cyc/mm)')
plt.figure()
plt.imshow(np.angle(um),extent = [xm[0], xm[-1], xm[0], xm[-1]], cmap = 'gray')
plt.title(' Random phase mask' )
plt.xlabel('x(mm)')
plt.ylabel('y(mm)')
# Output plane
plt.figure()
uoplot = np.abs(uo[int(M/2-m/2):int(M/2-m/2) + m , int(M/2-n/2):int(M/2-n/2) + n ]**2)
plt.imshow(uoplot,extent = [xo[0], xo[-1], xo[0], xo[-1]], cmap = 'gray')
plt.title('Encrypted image')
plt.xlabel('x(mm)')
plt.ylabel('y(mm)')
return uo, um_phase, xo
#%%Decryption function
def desencryptation(uo,um_phase,n,m, xo, f3, f4):
"""
Encryption function,
Inputs:
uo = encrypted wave
um_phase = encryption mask used,
n,m = number of pixels of the original image,
xo = coordinate position of the encrypted image
f3, f4 = focal of the decrytion 4f system
Outputs:
uo_deconv = decryted image
"""
M,M = uo.shape
Fuo = iFT(uo)
Fuo_deconv = Fuo * np.exp(-1j*um_phase)
uo_deconv = iFT(Fuo_deconv)
x_deconv = f3/f4 * xo
plt.figure()
uo_deconvplot = np.abs(uo_deconv[int(M/2-m/2):int(M/2-m/2) + m , int(M/2-n/2):int(M/2-n/2) + n ]**2)
plt.imshow(uo_deconvplot,extent = [x_deconv[0], x_deconv[-1], x_deconv[0], x_deconv[-1]], cmap = 'gray')
plt.title('Decrypted image')
plt.xlabel('x(mm)')
plt.ylabel('y(mm)')
return uo_deconv