def correlateFrames(A, B): A = A.squeeze()/A.mean() - 1 B = B.squeeze()/B.mean() - 1 X, Y = np.mgrid[0.0:A.shape[0], 0.0:A.shape[1]] C = ifftshift(np.abs(ifftn(fftn(A)*ifftn(B)))) Cm = C.max() Cp = np.maximum(C - 0.5*Cm, 0) Cpsum = Cp.sum() x0 = (X*Cp).sum()/Cpsum y0 = (Y*Cp).sum()/Cpsum return x0 - A.shape[0]/2, y0 - A.shape[1]/2, Cm, Cpsum
def correlateFrames(A, B): A = A.squeeze() / A.mean() - 1 B = B.squeeze() / B.mean() - 1 X, Y = np.mgrid[0.0:A.shape[0], 0.0:A.shape[1]] C = ifftshift(np.abs(ifftn(fftn(A) * ifftn(B)))) Cm = C.max() Cp = np.maximum(C - 0.5 * Cm, 0) Cpsum = Cp.sum() x0 = (X * Cp).sum() / Cpsum y0 = (Y * Cp).sum() / Cpsum return x0 - A.shape[0] / 2, y0 - A.shape[1] / 2, Cm, Cpsum
def calcCorrShift(im1, im2): im1 = im1 - im1.mean() im2 = im2 - im2.mean() xc = np.abs(ifftshift(ifftn(fftn(im1) * ifftn(im2)))) xct = (xc - xc.max() / 1.1) * (xc > xc.max() / 1.1) print((xct.shape)) #figure(1) #imshow(xct) #dx, dy = ndimage.measurements.center_of_mass(xct) #print np.where(xct==xct.max()) dx, dy = np.where(xct == xct.max()) return dx[0] - im1.shape[0] / 2, dy[0] - im1.shape[1] / 2
def correlateAndCompareFrames(A, B): A = A.squeeze() / A.mean() - 1 B = B.squeeze() / B.mean() - 1 X, Y = np.mgrid[0.0:A.shape[0], 0.0:A.shape[1]] C = ifftshift(np.abs(ifftn(fftn(A) * ifftn(B)))) Cm = C.max() Cp = np.maximum(C - 0.5 * Cm, 0) Cpsum = Cp.sum() x0 = (X * Cp).sum() / Cpsum y0 = (Y * Cp).sum() / Cpsum dx, dy = x0 - A.shape[0] / 2, y0 - A.shape[1] / 2 As = ndimage.shift(A, [-dx, -dy]) #print A.shape, As.shape return (As - B).mean(), dx, dy
def correlateAndCompareFrames(A, B): A = A.squeeze()/A.mean() - 1 B = B.squeeze()/B.mean() - 1 X, Y = np.mgrid[0.0:A.shape[0], 0.0:A.shape[1]] C = ifftshift(np.abs(ifftn(fftn(A)*ifftn(B)))) Cm = C.max() Cp = np.maximum(C - 0.5*Cm, 0) Cpsum = Cp.sum() x0 = (X*Cp).sum()/Cpsum y0 = (Y*Cp).sum()/Cpsum dx, dy = x0 - A.shape[0]/2, y0 - A.shape[1]/2 As = ndimage.shift(A, [-dx, -dy]) #print A.shape, As.shape return (As -B).mean(), dx, dy
def setIllumPattern(pattern, z0): global illPattern, illZOffset, illPCache sx, sy = pattern.shape psx, psy, sz = interpModel.shape il = np.zeros([sx,sy,sz], 'f') il[:,:,sz/2] = pattern ps = np.zeros_like(il) if sx > psx: ps[(sx/2-psx/2):(sx/2+psx/2), (sy/2-psy/2):(sy/2+psy/2), :] = interpModel else: ps[:,:,:] = interpModel[(psx/2-sx/2):(psx/2+sx/2), (psy/2-sy/2):(psy/2+sy/2), :] ps= ps/ps[:,:,sz/2].sum() illPattern = abs(ifftshift(ifftn(fftn(il)*fftn(ps)))).astype('f') illPCache = None
def compare(self): d = 1.0 * self.scope.pa.dsa.squeeze() dm = d / d.mean() - 1 #find x-y drift C = ifftshift(np.abs(ifftn(fftn(dm) * self.FA))) Cm = C.max() Cp = np.maximum(C - 0.5 * Cm, 0) Cpsum = Cp.sum() dx = (self.X * Cp).sum() / Cpsum dy = (self.Y * Cp).sum() / Cpsum ds = ndimage.shift(dm, [-dx, -dy]) * self.mask #print A.shape, As.shape ds_A = (ds - self.refA) return dx, dy, self.deltaZ * np.dot(ds_A.ravel(), self.dz) * self.dzn
def compare(self): d = 1.0*self.scope.pa.dsa.squeeze() dm = d/d.mean() - 1 #find x-y drift C = ifftshift(np.abs(ifftn(fftn(dm)*self.FA))) Cm = C.max() Cp = np.maximum(C - 0.5*Cm, 0) Cpsum = Cp.sum() dx = (self.X*Cp).sum()/Cpsum dy = (self.Y*Cp).sum()/Cpsum ds = ndimage.shift(dm, [-dx, -dy])*self.mask #print A.shape, As.shape ds_A = (ds - self.refA) return dx, dy, self.deltaZ*np.dot(ds_A.ravel(), self.dz)*self.dzn
def setRefA(self): d = 1.0 * self.scope.pa.dsa.squeeze() self.refA = d / d.mean() - 1 self.FA = ifftn(self.refA) self.refA *= self.mask
def compare(self): d = 1.0 * self.scope.frameWrangler.currentFrame.squeeze() dm = d / d.mean() - 1 #where is the piezo suppposed to be #nomPos = self.piezo.GetPos(0) nomPos = self.piezo.GetTargetPos(0) #find closest calibration position posInd = np.argmin(np.abs(nomPos - self.calPositions)) #dz = float('inf') #count = 0 #while np.abs(dz) > 0.5*self.deltaZ and count < 1: # count += 1 #retrieve calibration information at this location calPos = self.calPositions[posInd] FA = self.calFTs[:, :, posInd] refA = self.calImages[:, :, posInd] ddz = self.dz[:, posInd] dzn = self.dzn[posInd] #what is the offset between our target position and the calibration position posDelta = nomPos - calPos print('%s' % [nomPos, posInd, calPos, posDelta]) #find x-y drift C = ifftshift(np.abs(ifftn(fftn(dm) * FA))) Cm = C.max() Cp = np.maximum(C - 0.5 * Cm, 0) Cpsum = Cp.sum() dx = (self.X * Cp).sum() / Cpsum dy = (self.Y * Cp).sum() / Cpsum ds = ndimage.shift(dm, [-dx, -dy]) * self.mask #print A.shape, As.shape self.ds_A = (ds - refA) #calculate z offset between actual position and calibration position dz = self.deltaZ * np.dot(self.ds_A.ravel(), ddz) * dzn #posInd += np.round(dz / self.deltaZ) #posInd = int(np.clip(posInd, 0, self.NCalibStates)) # print count, dz #add the offset back to determine how far we are from the target position dz = dz - posDelta # if 1000*np.abs((dz + posDelta))>200 and self.WantRecord: #dz = np.median(self.buffer) # tif.imsave('C:\\Users\\Lab-test\\Desktop\\peakimage.tif', d) # np.savetxt('C:\\Users\\Lab-test\\Desktop\\parameter.txt', self.buffer[-1]) #np.savetxt('C:\\Users\\Lab-test\\Desktop\\posDelta.txt', posDelta) # self.WantRecord = False #return dx, dy, dz + posDelta, Cm, dz, nomPos, posInd, calPos, posDelta return dx, dy, dz, Cm, dz, nomPos, posInd, calPos, posDelta
def setRefN(self, N): d = 1.0 * self.scope.frameWrangler.currentFrame.squeeze() ref = d / d.mean() - 1 self.refImages[:, :, N] = ref self.calFTs[:, :, N] = ifftn(ref) self.calImages[:, :, N] = ref * self.mask
def propagate(self, F, z): return ifftshift(ifftn(F*np.exp(self.propFac*z)))
def setRefA(self): d = 1.0*self.scope.pa.dsa.squeeze() self.refA = d/d.mean() - 1 self.FA = ifftn(self.refA) self.refA *= self.mask