forked from ImSwitch/ImSwitch
-
Notifications
You must be signed in to change notification settings - Fork 16
Open
Description
Hi, @beniroquai !
I am trying to solve the DPC problem in ImSwitch script, here's the code :)
import tifffile
import time
import matplotlib.pyplot as plt
import numpy as np
mainWindow.setCurrentModule('imcontrol')
def fft2c(x):
axes = (-2, -1)
return np.fft.fftshift(
np.fft.fft2(np.fft.ifftshift(x, axes=axes), norm="ortho"), axes=axes
)
def ifft2c(X):
axes = (-2, -1)
return np.fft.fftshift(
np.fft.ifft2(np.fft.ifftshift(X, axes=axes), norm="ortho"), axes=axes
)
tWait = 2
Path = r'C:\Users\T490\Documents\GitHub\ImSwitch\imswitch\ImSwitch\recordings\DPC'
#Top pattern
api.imcontrol.setAllLEDOff()
time.sleep(.2)
topPattern = [{"id":8, "r":0, "g":1, "b":0},
{"id":9, "r":0, "g":1, "b":0},
{"id":10, "r":0, "g":1, "b":0},
{"id":11, "r":0, "g":1, "b":0},
{"id":12, "r":0, "g":0, "b":0},
{"id":13, "r":0, "g":1, "b":0},
{"id":14, "r":0, "g":1, "b":0},
{"id":15, "r":0, "g":0, "b":0}
]
api.imcontrol.setSpecial(topPattern, 255, True)
time.sleep(tWait)
mImage = api.imcontrol.snapImage(True, False)
tifffile.imsave(Path+r'\toppattern.tif', mImage)
#Bottom pattern
api.imcontrol.setAllLEDOff()
time.sleep(0.2)
bottomPattern= [{"id":0, "r":0, "g":0, "b":0},
{"id":1, "r":0, "g":1, "b":0},
{"id":2, "r":0, "g":1, "b":0},
{"id":3, "r":0, "g":0, "b":0},
{"id":4, "r":0, "g":1, "b":0},
{"id":5, "r":0, "g":1, "b":0},
{"id":6, "r":0, "g":1, "b":0},
{"id":7, "r":0, "g":1, "b":0}
]
api.imcontrol.setSpecial(bottomPattern, 255, True)
time.sleep(tWait)
mImage = api.imcontrol.snapImage(True, False)
tifffile.imsave(Path + r'\bottompattern.tif', mImage)
#right pattern
api.imcontrol.setAllLEDOff()
time.sleep(.1)
rightPattern = [{"id":2, "r":0, "g":1, "b":0},
{"id":3, "r":0, "g":0, "b":0},
{"id":4, "r":0, "g":1, "b":0},
{"id":5, "r":0, "g":1, "b":0},
{"id":11, "r":0, "g":1, "b":0},
{"id":10, "r":0, "g":1, "b":0},
{"id":12, "r":0, "g":0, "b":0},
{"id":13, "r":0, "g":1, "b":0}
]
api.imcontrol.setSpecial(rightPattern, 255, True)
time.sleep(tWait)
mImage = api.imcontrol.snapImage(True, False)
tifffile.imsave(Path+r'\rightpattern.tif', mImage)
#left pattern
api.imcontrol.setAllLEDOff()
time.sleep(.1)
leftPattern = [{"id":0, "r":0, "g":0, "b":0},
{"id":1, "r":0, "g":1, "b":0},
{"id":6, "r":0, "g":1, "b":0},
{"id":7, "r":0, "g":1, "b":0},
{"id":8, "r":0, "g":1, "b":0},
{"id":9, "r":0, "g":1, "b":0},
{"id":14, "r":0, "g":1, "b":0},
{"id":15, "r":0, "g":0, "b":0}
]
api.imcontrol.setSpecial(leftPattern, 255, True)
time.sleep(tWait)
mImage = api.imcontrol.snapImage(True, False)
tifffile.imsave(Path+r'\leftpattern.tif', mImage)
#Brightfield pattern
api.imcontrol.setAllLEDOff()
time.sleep(.1)
rightPattern = [{"id":2, "r":0, "g":1, "b":0},
{"id":3, "r":0, "g":0, "b":0},
{"id":4, "r":0, "g":1, "b":0},
{"id":5, "r":0, "g":1, "b":0},
{"id":11, "r":0, "g":1, "b":0},
{"id":10, "r":0, "g":1, "b":0},
{"id":12, "r":0, "g":0, "b":0},
{"id":13, "r":0, "g":1, "b":0}
]
api.imcontrol.setSpecial(rightPattern, 255, True)
leftPattern = [{"id":0, "r":0, "g":0, "b":0},
{"id":1, "r":0, "g":1, "b":0},
{"id":6, "r":0, "g":1, "b":0},
{"id":7, "r":0, "g":1, "b":0},
{"id":8, "r":0, "g":1, "b":0},
{"id":9, "r":0, "g":1, "b":0},
{"id":14, "r":0, "g":1, "b":0},
{"id":15, "r":0, "g":0, "b":0}
]
api.imcontrol.setSpecial(leftPattern, 255, True)
time.sleep(tWait)
#Read Intensity images for the four patterns
#I_Bf = tiff.imread(str(path_name + '/Wide_field.tiff'))
I_bottom = tifffile.imread(str(Path + '/bottompattern.tif'))
I_top = tifffile.imread(str(Path + '/toppattern.tif'))
I_left = tifffile.imread(str(Path + '/leftpattern.tif'))
I_right = tifffile.imread(str(Path + '/rightpattern.tif'))
m = I_bottom.shape[0]
n = I_bottom.shape[1]
scaling_x = n/2
scaling_y = m/2
x = np.linspace(-n/2,n/2-1,n).reshape(1,n)/scaling_x
y = np.linspace(-m/2,m/2-1,m).reshape(m,1)/scaling_y
#Definition of gradients (Differences)
I_DPC_y = -(np.float32(I_right) - np.float32(I_left)) #/ (I_right + I_left)
I_DPC_x = (np.float32(I_bottom) - np.float32(I_top)) #/ (I_bottom + I_top)
#Definition of transfer functions
H_x = np.tanh(x)
H_y = np.tanh(y)
I_x_ft = fft2c(I_DPC_x)
I_y_ft = fft2c(I_DPC_y)
numerator = I_x_ft * H_x + I_y_ft * H_y
alpha = 1e-3
denominator = H_x**2 + H_y**2 + alpha
phase_ft = numerator / denominator
phase = np.imag(ifft2c(phase_ft))
api.imcontrol.displayImageNapari(phase, "DPC Phase")
tifffile.imsave(Path+r'\Phase.tiff', phase)
api.imcontrol.displayImageNapari(I_DPC_y, "DPCy")
api.imcontrol.displayImageNapari(I_DPC_x, "DPCx")
api.imcontrol.displayImageNapari(np.float32(I_right)+np.float32(I_left), "DPCxy")
snap = np.float32(api.imcontrol.snapImage(True, False))
bfPseudo = np.float32(I_right)+np.float32(I_left)
api.imcontrol.displayImageNapari(bfPseudo/np.max(bfPseudo)-snap/np.max(snap), "DPCxyCheck")
Let me know if you find the issue.
HG
Aaron
Metadata
Metadata
Assignees
Labels
No labels