2 import matplotlib.pyplot
as plt
14 return (np.pi * 2) / wvl
19 deltax = 1 / (w * pp * 2)
20 deltay = 1 / ((h + w) * pp)
21 a = np.zeros((w + h, w*2))
22 b = np.zeros((w + h, w*2))
23 delx = 1 / (np.sqrt((2 * deltax * z)**2 + 1) * wvl)
24 dely = 1 / (np.sqrt((2 * deltay * z)**2 + 1) * wvl)
25 for i
in np.arange(w*2):
26 for j
in np.arange(w + h):
27 fx = ((i - w) * deltax)
28 fy = -((j - (w + h)/2) * deltay)
29 if -delx < fx < delx
and -dely < fy < dely:
30 a[j, i] = np.cos(2 * np.pi * z * np.sqrt((1/wvl)**2 - fx * fx - fy * fy))
31 b[j, i] = np.sin(2 * np.pi * z * np.sqrt((1/wvl)**2 - fx * fx - fy * fy))
32 print(z,
'kernel ready')
37 def h_frsn(pixel_pitch_x, pixel_pitch_y, nx, ny, zz, wvl):
38 re = np.zeros((ny, nx))
39 im = np.zeros((ny, nx))
40 for i
in np.arange(nx):
41 for j
in np.arange(ny):
42 x = (i - nx / 2) * pixel_pitch_x
43 y = -(j - ny / 2) * pixel_pitch_y
44 re[j, i] = np.cos((np.pi / (wvl * zz)) * (x * x + y * y))
45 im[j, i] = np.sin((np.pi / (wvl * zz)) * (x * x + y * y))
50 def refwave(wvl, wr, hr, z, pp, thetaX, thetaY):
51 a = np.zeros((hr, wr))
52 b = np.zeros((hr, wr))
53 for i
in np.arange(hr):
54 for j
in np.arange(wr):
56 y = -(i - hr / 2) * pp
57 a[i, j] = np.cos(
k(wvl) * (x * np.sin(thetaX) + y * np.sin(thetaY)))
58 b[i, j] = np.sin(
k(wvl) * (x * np.sin(thetaX) + y * np.sin(thetaY)))
59 return (a / z) + 1j * (b / z)
64 Get Fringe pattern by 2D image 67 f : propagation length 68 angle : phase shift angle 69 Red, Green, Blue : wavelength 70 scale : scaling factor 73 def __init__(self, imgpath, propagation_distance=1, angleX=0, angleY=0, Red=639*nm, Green=525*nm, Blue=463*nm,
74 SLM_width=3840, SLM_height=2160, scale=0.03, pixel_pitch=3.6*um):
75 self.
zz = propagation_distance
76 self.
imagein = np.asarray(Image.open(imgpath))
77 self.
thetaX = angleX * (np.pi / 180)
78 self.
thetaY = angleY * (np.pi / 180)
88 im = Image.fromarray(img)
89 im = im.resize((self.
w, self.
h), Image.BILINEAR)
93 img_new = np.zeros((hh, ww))
94 img_new[(hh-self.
h)//2:(hh+self.
h)//2, (ww-self.
w)//2:(ww+self.
w)//2] = im
98 """RGB 파장에 맞게 원본 이미지를 리사이징 + zero padding""" 99 w_n = int(self.
w * (self.
wvl_B / wvl))
100 h_n = int(self.
h * (self.
wvl_B / wvl))
101 img_new = np.zeros((self.
h*2, self.
w*2))
102 im = Image.fromarray(img)
103 im = im.resize((w_n, h_n), Image.BILINEAR)
106 img_new[(self.
h*2 - h_n) // 2:(self.
h*2 + h_n) // 2, (self.
w*2 - w_n) // 2:(self.
w*2 + w_n) // 2] = im
114 elif color ==
'blue':
121 image = np.flip(image, axis=0)
122 ps = self.
scale / (2*self.
w)
123 phase = np.random.rand(2*self.
h, 2*self.
w) * 2 * np.pi
124 ph = np.exp(1j*phase)
126 ch2 = ph *
h_frsn(ps, ps, self.
w * 2, self.
h * 2, self.
zz, wvl)
127 CH1 = np.fft.fftshift(np.fft.fft2(np.fft.fftshift(ch2)))
128 result = CH1 *
h_frsn(self.
pp, self.
pp, self.
w * 2, self.
h * 2, self.
zz, wvl)
129 result = result[self.
h // 2: (3*self.
h) // 2, self.
w // 2: (3*self.
w) // 2]
138 elif color ==
'blue':
144 phase = np.random.rand(self.
w + self.
h, 2 * self.
w) * 2 * np.pi
145 phase = np.exp(1j * phase)
149 result = self.
ifft(CH)
150 result = result[self.
w // 2: self.
w//2 + self.
h, self.
w//2: (3 * self.
w)//2]
157 return np.fft.fftshift(np.fft.fft2(np.fft.fftshift(f)))
160 return np.fft.fftshift(np.fft.ifft2(np.fft.fftshift(f)))
163 """single side band encoding""" 164 height, width = ch.shape
165 a = np.zeros((height, width), dtype=
'complex128')
167 CH = CH[height // 4: (height * 3) // 4, :]
168 a[0:height // 2, :] = CH
169 a[height // 2:, :] = np.conj(CH)
175 arrin = np.copy(np.imag(arr))
177 arrin = np.copy(np.real(arr))
178 elif type ==
'angle':
179 arrin = np.copy(np.angle(arr))
180 elif type ==
'amplitude':
181 arrin = np.copy(np.abs(arr))
185 arrin -= np.min(arrin)
187 arrin = arrin / np.max(arrin)
193 img = np.zeros((h, w, 3))
197 plt.imsave(fname, img)
201 """Get Single channel image""" 203 phase = fname +
'_IM.bmp' 204 plt.imsave(phase, im, cmap=
'gray')
206 real = fname +
'_RE.bmp' 207 plt.imsave(real, re, cmap=
'gray')
def getMonoImage(self, ch, fname)
def zeropadding(self, img, nzpw)
def h_frsn(pixel_pitch_x, pixel_pitch_y, nx, ny, zz, wvl)
def resizeimg(self, wvl, img)
def normalize(self, arr, type='angle')
def asm_kernel(wvl, z, w, h, pp)
def __init__(self, imgpath, propagation_distance=1, angleX=0, angleY=0, Red=639 *nm, Green=525 *nm, Blue=463 *nm, SLM_width=3840, SLM_height=2160, scale=0.03, pixel_pitch=3.6 *um)
def refwave(wvl, wr, hr, z, pp, thetaX, thetaY)
def getRGBImage(self, R, G, B, fname, type='phase')