2 import matplotlib.pyplot
as plt
6 from concurrent.futures
import ProcessPoolExecutor
15 @njit(nogil=
True, cache=
True)
17 return (np.pi * 2) / wvl
21 def h_frsn(pixel_pitch_x, pixel_pitch_y, nx, ny, zz, wvl):
22 re = np.zeros((ny, nx))
23 im = np.zeros((ny, nx))
24 for i
in np.arange(nx):
25 for j
in np.arange(ny):
26 x = (i - nx / 2) * pixel_pitch_x
27 y = (j - ny / 2) * pixel_pitch_y
28 re[j, i] = np.cos((np.pi / (wvl * zz)) * (x * x + y * y))
29 im[j, i] = np.sin((np.pi / (wvl * zz)) * (x * x + y * y))
35 """ extract alpha map """ 36 shape = depthmap.shape
39 amap = np.zeros((hh, ww))
40 for i
in np.arange(ww):
41 for j
in np.arange(hh):
42 if depthmap[j, i] == n:
48 def refwave(wvl, wr, hr, z, pp, thetaX, thetaY):
49 a = np.zeros((hr, wr))
50 b = np.zeros((hr, wr))
51 for i
in np.arange(hr):
52 for j
in np.arange(wr):
54 y = -(i - hr / 2) * pp
55 a[i, j] = np.cos(
k(wvl) * (x * np.sin(thetaX) + y * np.sin(thetaY)))
56 b[i, j] = np.sin(
k(wvl) * (x * np.sin(thetaX) + y * np.sin(thetaY)))
57 return (a / z) + 1j * (b / z)
62 Get Fringe pattern by 2D Depth map image and RGB color image 66 f : propagation length 67 angle : phase shift angle 68 Red, Green, Blue : wavelength 69 scale : scaling factor 76 Depth quantization (깊이 계조) 80 UD = -(far_depth - near_depth) / 256 83 def __init__(self, RGBimg, Depthimg, f=1, angleX=0, angleY=0,
84 Red=639*nm, Green=525*nm, Blue=463*nm, SLM_width=3840, multicore=True,
85 SLM_height=2160, scale=0.03, pixel_pitch=3.6*um, zeropadding=3840,
86 field_length=1000e-3, near_depth=800e-3, far_depth=1200e-3, DepthQuantization=256):
88 self.
imagein = np.asarray(Image.open(RGBimg))
89 self.
depthimg = np.asarray(Image.open(Depthimg))
100 self.
UD = -(far_depth - near_depth) / 256
101 self.
DQ = DepthQuantization
107 self.
num_cpu = multiprocessing.cpu_count() // 2
114 """RGB 파장에 맞게 원본 이미지를 리사이징 + zero padding""" 115 w_n = int(self.
w * (self.
wvl_B / wvl))
116 h_n = int(self.
h * (self.
wvl_B / wvl))
117 img_new = np.zeros((self.
h * 2, self.
w * 2))
118 im = Image.fromarray(img)
119 im = im.resize((w_n, h_n), Image.BILINEAR)
121 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
124 def Prop(self, color, image, amap, n):
127 elif color ==
'blue':
133 imgs = np.flip(imgs, axis=0)
134 zzz = self.
UD * n + self.
zz 135 phase = np.random.random((2 * self.
h, 2 * self.
w)) * 2 * np.pi
136 ph = np.exp(1j * phase)
138 ps = self.
scale / (2 * self.
w)
139 ch2 = ph *
h_frsn(ps, ps, self.
w + self.
w, self.
h + self.
h, zzz, wvl)
140 CH1 = np.fft.fftshift(np.fft.fft2(np.fft.fftshift(ch2)))
141 result = CH1 *
h_frsn(self.
pp, self.
pp, self.
w + self.
w, self.
h + self.
h, zzz, wvl)
142 result = result[self.
h // 2: (3 * self.
h) // 2, self.
w // 2: (3 * self.
w) // 2]
150 return self.
Prop(
'red', self.
img_R, amap, n)
156 return self.
Prop(
'green', self.
img_G, amap, n)
162 return self.
Prop(
'blue', self.
img_B, amap, n)
167 elif color ==
'blue':
171 H = np.zeros((self.
h, self.
w), dtype=
'complex128')
174 with ProcessPoolExecutor(self.
num_cpu)
as ex:
175 cache = [result
for result
in ex.map(fun, list(n))]
176 cache = np.asarray(cache)
177 print(n,
' depth done')
178 for j
in range(len(n)):
184 return np.fft.fftshift(np.fft.fft2(np.fft.fftshift(f)))
187 return np.fft.fftshift(np.fft.ifft2(np.fft.fftshift(f)))
190 """single side band encoding""" 191 height, width = ch.shape
192 a = np.zeros((height, width), dtype=
'complex128')
194 CH = CH[height // 4: (height * 3) // 4, :]
195 a[0:height // 2, :] = CH
196 a[height // 2:, :] = np.conj(CH)
202 arrin = np.copy(np.imag(arr))
204 arrin = np.copy(np.real(arr))
205 elif type ==
'angle':
206 arrin = np.copy(np.angle(arr))
207 elif type ==
'amplitude':
208 arrin = np.copy(np.abs(arr))
212 arrin -= np.min(arrin)
214 arrin = arrin / np.max(arrin)
220 img = np.zeros((h, w, 3))
224 plt.imsave(fname, img)
228 """Get Single channel image""" 230 phase = fname +
'_IM.bmp' 231 plt.imsave(phase, im, cmap=
'gray')
233 real = fname +
'_RE.bmp' 234 plt.imsave(real, re, cmap=
'gray')
def normalize(self, arr, type='angle')
def __init__(self, RGBimg, Depthimg, f=1, angleX=0, angleY=0, Red=639 *nm, Green=525 *nm, Blue=463 *nm, SLM_width=3840, multicore=True, SLM_height=2160, scale=0.03, pixel_pitch=3.6 *um, zeropadding=3840, field_length=1000e-3, near_depth=800e-3, far_depth=1200e-3, DepthQuantization=256)
def refwave(wvl, wr, hr, z, pp, thetaX, thetaY)
def Prop(self, color, image, amap, n)
def getMonoImage(self, ch, fname)
def h_frsn(pixel_pitch_x, pixel_pitch_y, nx, ny, zz, wvl)
def resizeimg(self, wvl, img)
def alphamap(depthmap, n)
def getRGBImage(self, R, G, B, fname, type='angle')
def parallelCal(self, color)