def PropagateWave(img, ctf): fft = cc.FFT(img) fft.ReIm2AmPh() ctf = cc.fft2diff_cpu(ctf) ctf.MoveToGPU() fftProp = imsup.ImageExp(img.height, img.width, imsup.Image.cmp['CAP'], imsup.Image.mem['GPU']) fftProp.amPh = imsup.MultAmPhMatrices(fft.amPh, ctf.amPh) imgProp = cc.IFFT(fftProp) # normalization norm_factor = 1.0 / (img.height * img.width) imgProp.AmPh2ReIm() imgProp.MoveToCPU() imgProp.reIm *= norm_factor imgProp.ReIm2AmPh() # imgProp.MoveToCPU() # imgProp = imsup.re_im_2_am_ph_cpu(imgProp) # !!! imgProp.defocus = img.defocus + ctf.defocus imgProp.px_dim = img.px_dim ctf.ClearGPUMemory() fft.ClearGPUMemory() fftProp.ClearGPUMemory() img.MoveToCPU() imgProp.MoveToCPU() return imgProp
def displayFFT(self): fft = cc.FFT(self.imageSim) diff = cc.FFT2Diff(fft) diff.ReIm2AmPh() diff.MoveToCPU() diffToDisp = np.log10(diff.amPh.am) self.imageSim.buffer = imsup.ScaleImage(diffToDisp, 0.0, 255.0) self.createPixmap() self.imageSim.UpdateBuffer() self.phase_unwrap_rbutton.setDisabled(True) self.phase_wrap_rbutton.setDisabled(True)
def correlateWithImage(self, imageToCorr, fragCoords, wat): image = self.btnGrid.image mcfBest = cc.MaximizeMCFCore(imageToCorr, image, self.btnGrid.gridDim, fragCoords, float(self.dfMinEdit.input.text()), float(self.dfMaxEdit.input.text()), float(self.dfStepEdit.input.text())) shift = cc.GetShift(mcfBest) image.shift = (image.prev.shift if wat else [0, 0]) cc.ShiftImageAmpBuffer(image, shift) self.btnGrid.image.defocus = mcfBest.defocus self.btnGrid.createPixmap() ccfPath = const.ccfResultsDir + const.ccfName + str(image.numInSeries) + '.png' imsup.SaveAmpImage(mcfBest, ccfPath)
def PropagateWave(img, ctf): fft = cc.FFT(img) fft.ReIm2AmPh() ctf = cc.Diff2FFT(ctf) # !!! ctf.ReIm2AmPh() fftProp = imsup.Image(img.height, img.width, imsup.Image.cmp['CAP'], imsup.Image.mem['GPU']) fftProp.amPh = imsup.MultAmPhMatrices(fft.amPh, ctf.amPh) imgProp = cc.IFFT(fftProp) imgProp.ReIm2AmPh() imgProp.defocus = img.defocus + ctf.defocus return imgProp
def runEwr(self): currentImage = self.parent().parent().getCcWidgetRef().btnGrid.image firstImage = imsup.GetFirstImage(currentImage) imgList = imsup.CreateImageListFromFirstImage(firstImage) idxInFocus = int(self.numInFocusEdit.input.text()) - 1 cc.DetermineAbsoluteDefocus(imgList, idxInFocus) squareCoords = imsup.MakeSquareCoords(self.parent().parent().getCcWidgetRef().commonCoords) # print(squareCoords) imgListAll = imsup.CreateImageListFromFirstImage(self.imageSim) firstIdx = int(self.numOfFirstEdit.input.text()) - 1 howMany = int(self.numOfImagesEdit.input.text()) imgListToEWR = imsup.CreateImageListFromImage(imgListAll[firstIdx], howMany) for img, idx in zip(imgListToEWR, range(len(imgListToEWR))): print('df = {0:.1f} nm'.format(img.defocus * 1e9)) imgListToEWR[idx] = imsup.CropImageROICoords(img, squareCoords) cropPath = const.cropResultsDir + const.cropName + str(idx + 1) + '.png' imsup.SaveAmpImage(imgListToEWR[idx], cropPath) # imgListToEWR = uw.UnwarpImageList(imgListToEWR, const.nDivForUnwarp) # unwarp off exitWave = prop.PerformIWFR(imgListToEWR, int(self.numOfItersEdit.input.text())) exitWave.ChangeComplexRepr(imsup.Image.cmp['CAP']) # !!! exitWave.MoveToCPU() # !!! # exitWave.amPh.ph = np.abs(exitWave.amPh.ph) # !!! self.exitWave = imsup.CreateImageWithBufferFromImage(exitWave) self.imageSim = imsup.CreateImageWithBufferFromImage(self.exitWave) # copy self.exitWave to self.imageSim # self.imageSim.MoveToCPU() self.createPixmap() self.imageSim.UpdateBufferFromPhase() # !!!
def PropagateWave(img, ctf): fft = cc.FFT(img) fft.ReIm2AmPh() ctf = cc.Diff2FFT(ctf) # !!! ctf.ReIm2AmPh() fftProp = imsup.Image(img.height, img.width, imsup.Image.cmp['CAP'], imsup.Image.mem['GPU']) fftProp.amPh = imsup.MultAmPhMatrices(fft.amPh, ctf.amPh) imgProp = cc.IFFT(fftProp) imgProp.ReIm2AmPh() imgProp.defocus = img.defocus + ctf.defocus imgProp.px_dim = img.px_dim # print('{0}: {1:.2f}, {2:.2f}, {3:.2f}'.format(img.numInSeries, img.defocus * 1e9, ctf.defocus * 1e9, imgProp.defocus * 1e9)) return imgProp
def ScaleAmpImages(images): amMax = 0.0 amMin = cc.FindMaxInImage(images[0]) # amMin = np.max(images[0].amPh.am) for img in images: amMaxCurr = cc.FindMaxInImage(img) amMinCurr = cc.FindMinInImage(img) # amMaxCurr = np.max(img.amPh.am) # amMinCurr = np.min(img.amPh.am) if amMaxCurr >= amMax: amMax = amMaxCurr if amMinCurr <= amMin: amMin = amMinCurr for img in images: img.MoveToCPU() # !!! img.amPh.am = ScaleImage(img.amPh.am, amMin, amMax) img.MoveToGPU()
def rec_holo_no_ref_2(holo_fft, shift, ap_sz=const.aperture, N_hann=const.hann_win): holo_fft.MoveToGPU() sband_mid_img = cc.ShiftImage(holo_fft, shift) holo_fft.MoveToCPU() sband_img_ap = insert_aperture(sband_mid_img, ap_sz) sband_img_ap = mult_by_hann_window(sband_img_ap, N=N_hann) imsup.SaveAmpImage(sband_img_ap, 'sband_am.png') imsup.SavePhaseImage(sband_img_ap, 'sband_ph.png') return sband_img_ap
def rec_holo_no_ref(holo_img, rec_sz=128, ap_sz=32, mask_sz=50, N_hann=100): holo_fft = cc.FFT(holo_img) holo_fft = cc.FFT2Diff(holo_fft) # diff is re_im holo_fft.ReIm2AmPh() holo_fft.MoveToCPU() mfft = mask_fft_center(holo_fft.amPh.am, mask_sz, True) sband_xy = find_img_max(mfft) holo_fft.MoveToGPU() # rec_sz_half = rec_sz // 2 # coords = [sband_xy[0] - rec_sz_half, sband_xy[1] - rec_sz_half, sband_xy[0] + rec_sz_half, sband_xy[1] + rec_sz_half] # sband_img = imsup.CropImageROICoords(holo_fft, coords) mid = holo_img.width // 2 shift = [mid - sband_xy[0], mid - sband_xy[1]] sband_img = cc.ShiftImage(holo_fft, shift) sband_img_ap = mult_by_hann_window(sband_img, N=N_hann) sband_img_ap = insert_aperture(sband_img_ap, ap_sz) imsup.SaveAmpImage(sband_img_ap, 'sband_am.png') imsup.SavePhaseImage(sband_img_ap, 'sband_ph.png') sband_img_ap = cc.Diff2FFT(sband_img_ap) rec_holo = cc.IFFT(sband_img_ap) imsup.SaveAmpImage(rec_holo, 'amp.png') imsup.SavePhaseImage(rec_holo, 'phs.png') # factor = holo_img.width / rec_sz # rec_holo_resc = tr.RescaleImageSki(rec_holo, factor) # imsup.SaveAmpImage(rec_holo_resc, 'amp.png') # imsup.SavePhaseImage(rec_holo_resc, 'phs.png') rec_holo = imsup.CreateImageWithBufferFromImage(rec_holo) return rec_holo
def UnwarpImage(imgRef, img, nDiv): mt = img.memType dt = img.cmpRepr dfChange = -abs(abs(imgRef.defocus) - abs(img.defocus)) print('df_uw({0}, {1}) = {2:.2f} um'.format(imgRef.numInSeries, img.numInSeries, dfChange * 1e6)) imgRefProp = prop.PropagateBackToDefocus(imgRef, dfChange) fragCoords = [(b, a) for a in range(nDiv) for b in range(nDiv)] shifts = cc.CalcPartialCrossCorrFunUW(imgRefProp, img, nDiv, fragCoords) fName = 'uw_out.txt' textFile = open(fName, 'ab') np.savetxt(textFile, shifts, fmt='%.1f') textFile.write( b'---------------------------------------------------------------\n') textFile.close() fragDimSize = img.width // nDiv src = np.array(fragCoords) src *= fragDimSize dst = src - shifts img.ReIm2AmPh() img.MoveToCPU() oldMin, oldMax = np.min(img.amPh.am), np.max(img.amPh.am) scaledArray = imsup.ScaleImage(img.amPh.am, -1.0, 1.0) tform3 = tf.ProjectiveTransform() tform3.estimate(src, dst) warped = tf.warp(scaledArray, tform3, output_shape=(img.height, img.width)).astype(np.float32) warpedScaledBack = imsup.ScaleImage(warped, oldMin, oldMax) warpedImage = imsup.Image(warped.shape[0], warped.shape[1]) warpedImage.amPh.am = np.copy(warpedScaledBack) img.amPh.am = np.copy(warpedImage.amPh.am) img.ChangeMemoryType(mt) img.ChangeComplexRepr(dt) imgRef.ChangeMemoryType(mt) imgRef.ChangeComplexRepr(dt) return img
def simulate_images(exit_wave, df1, df2=None, df3=None, use_aberrs=True, A1_amp=0.0, A1_phs=0.0, aper=const.aperture, smooth_w=const.smooth_width): if df2 is None or df3 is None: df2 = df1 + 1 df3 = 2 sim_imgs = imsup.ImageList() if use_aberrs: const.A1_amp = A1_amp # !!! const.A1_phs = A1_phs # !!! for df in cc.frange(df1, df2, df3): print('Sim. {0:.2f} nm'.format(df * 1e9)) img = PropagateBackToDefocus(exit_wave, df, use_aberrs, aper, smooth_w) img.MoveToCPU() img.defocus = df sim_imgs.append(img) return sim_imgs
def triangulateAdvanced(self): triangles = [[ CalcRealCoords(const.dimSize, self.pointSets[trIdx][pIdx]) for pIdx in range(3) ] for trIdx in range(2)] # tr1 = [ CalcRealCoords(const.dimSize, self.pointSets[0][pIdx]) for pIdx in range(3) ] # tr2 = [ CalcRealCoords(const.dimSize, self.pointSets[1][pIdx]) for pIdx in range(3) ] # tr1 = self.pointSets[0][:3] # tr2 = self.pointSets[1][:3] tr1Dists = [ CalcDistance(triangles[0][pIdx1], triangles[0][pIdx2]) for pIdx1, pIdx2 in zip([0, 0, 1], [1, 2, 2]) ] tr2Dists = [ CalcDistance(triangles[1][pIdx1], triangles[1][pIdx2]) for pIdx1, pIdx2 in zip([0, 0, 1], [1, 2, 2]) ] # zrobic prostsza wersje oparta na zalozeniu ze rotCenter = [0, 0] # i bardziej zaawansowana, ktora bierze pod uwage inne polozenie srodka obrotu (rotCenter != [0, 0]) # w tym drugim przypadku potrzebne jest obliczenie shiftow # mozna wyznaczyc dokladniej (sredni) rotCenter (na podstawie trzech a nie dwoch punktow) rcSum = [0, 0] rotCenters = [] for idx1 in range(3): for idx2 in range(idx1 + 1, 3): print(idx1, idx2) rotCenter = tr.FindRotationCenter( [triangles[0][idx1], triangles[0][idx2]], [triangles[1][idx1], triangles[1][idx2]]) rotCenters.append(rotCenter) print('rotCenter = {0}'.format(rotCenter)) rcSum = list(np.array(rcSum) + np.array(rotCenter)) rotCenterAvg = list(np.array(rcSum) / 3.0) rcShift = [] rcShift[:] = rotCenters[0][:] rcShift.reverse() print('rotCenterAvg = {0}'.format(rotCenterAvg)) # shift(-rotCenter) obu obrazow rcShift = [-int(rc) for rc in rcShift] print('rcShift = {0}'.format(rcShift)) img1 = imsup.CopyImage(self.image.prev) img2 = imsup.CopyImage(self.image) imsup.SaveAmpImage(img1, 'img1.png') imsup.SaveAmpImage(img2, 'img2.png') img1Rc = cc.ShiftImage(img1, rcShift) img2Rc = cc.ShiftImage(img2, rcShift) cropCoords = imsup.MakeSquareCoords( imsup.DetermineCropCoords(img1Rc.width, img1Rc.height, rcShift)) img1Rc = imsup.CropImageROICoords(img1Rc, cropCoords) img2Rc = imsup.CropImageROICoords(img2Rc, cropCoords) imsup.SaveAmpImage(img1Rc, 'holo1.png') imsup.SaveAmpImage(img2Rc, 'img2rc.png') rotAngles = [] for idx, p1, p2 in zip(range(3), triangles[0], triangles[1]): p1New = CalcNewCoords(p1, rotCenters[0]) p2New = CalcNewCoords(p2, rotCenters[0]) triangles[0][idx] = p1New triangles[1][idx] = p2New rotAngles.append(CalcRotAngle(p1New, p2New)) rotAngleAvg = np.average(rotAngles) mags = [dist1 / dist2 for dist1, dist2 in zip(tr1Dists, tr2Dists)] magAvg = np.average(mags) tr1InnerAngles = [ CalcInnerAngle(a, b, c) for a, b, c in zip(tr1Dists, tr1Dists[-1:] + tr1Dists[:-1], tr1Dists[-2:] + tr1Dists[:-2]) ] tr2InnerAngles = [ CalcInnerAngle(a, b, c) for a, b, c in zip(tr2Dists, tr2Dists[-1:] + tr2Dists[:-1], tr2Dists[-2:] + tr2Dists[:-2]) ] triangles[1] = [ tr.RotatePoint(p, ang) for p, ang in zip(triangles[1], rotAngles) ] shifts = [ list(np.array(p1) - np.array(p2)) for p1, p2 in zip(triangles[0], triangles[1]) ] shiftAvg = [ np.average([sh[0] for sh in shifts]), np.average([sh[1] for sh in shifts]) ] shiftAvg = [int(round(sh)) for sh in shiftAvg] print('---- Triangle 1 ----') print([ 'R{0} = {1:.2f} px\n'.format(idx + 1, dist) for idx, dist in zip(range(3), tr1Dists) ]) print([ 'alpha{0} = {1:.0f} deg\n'.format(idx + 1, angle) for idx, angle in zip(range(3), tr1InnerAngles) ]) # print('R12 = {0:.2f} px\nR13 = {1:.2f} px\nR23 = {2:.2f} px\n---'.format(r12, r13, r23)) # print('a1 = {0:.0f} deg\na2 = {1:.0f} deg\na3 = {2:.0f} deg\n---'.format(alpha1, alpha2, alpha3)) print('---- Triangle 2 ----') print([ 'R{0} = {1:.2f} px\n'.format(idx + 1, dist) for idx, dist in zip(range(3), tr2Dists) ]) print([ 'alpha{0} = {1:.0f} deg\n'.format(idx + 1, angle) for idx, angle in zip(range(3), tr2InnerAngles) ]) # print('R12 = {0:.2f} px\nR13 = {1:.2f} px\nR23 = {2:.2f} px\n---'.format(R12, R13, R23)) # print('a1 = {0:.0f} deg\na2 = {1:.0f} deg\na3 = {2:.0f} deg\n---'.format(Alpha1, Alpha2, Alpha3)) print('---- Magnification ----') print([ 'mag{0} = {1:.2f}x\n'.format(idx + 1, mag) for idx, mag in zip(range(3), mags) ]) print('---- Rotation ----') print([ 'phi{0} = {1:.0f} deg\n'.format(idx + 1, angle) for idx, angle in zip(range(3), rotAngles) ]) print('---- Shifts ----') print([ 'dxy{0} = ({1:.1f}, {2:.1f}) px\n'.format(idx + 1, sh[0], sh[1]) for idx, sh in zip(range(3), shifts) ]) print('------------------') print('Average magnification = {0:.2f}x'.format(magAvg)) print('Average rotation = {0:.2f} deg'.format(rotAngleAvg)) print('Average shift = ({0:.0f}, {1:.0f}) px'.format( shiftAvg[0], shiftAvg[1])) # img2Mag = tr.RescaleImageSki2(img2Rc, magAvg) # imsup.SaveAmpImage(img2Mag, 'img2_mag.png') img2Rot = tr.RotateImageSki2(img2Rc, rotAngleAvg, cut=False) imsup.SaveAmpImage(img2Rot, 'holo2.png') # cropCoords = imsup.DetermineCropCoordsForNewWidth(img1Rc.width, img2Rot.width) # img1Crop = imsup.CropImageROICoords(img1Rc, cropCoords) # imsup.SaveAmpImage(img1Crop, 'holo1.png') # --- # imgs1H = imsup.LinkTwoImagesSmoothlyH(img1Crop, img1Crop) # linkedImages1 = imsup.LinkTwoImagesSmoothlyV(imgs1H, imgs1H) # imgs2H = imsup.LinkTwoImagesSmoothlyH(img2Rot, img2Rot) # linkedImages2 = imsup.LinkTwoImagesSmoothlyV(imgs2H, imgs2H) # # img1Alg, img2Alg = cc.AlignTwoImages(linkedImages1, linkedImages2, [0, 1, 2]) # # newCoords = imsup.DetermineCropCoords(img1Crop.width, img1Crop.height, img2Alg.shift) # newSquareCoords = imsup.MakeSquareCoords(newCoords) # print(newSquareCoords) # newSquareCoords[2:4] = list(np.array(newSquareCoords[2:4]) - np.array(newSquareCoords[:2])) # newSquareCoords[:2] = [0, 0] # print(newSquareCoords) # # img1Res = imsup.CropImageROICoords(img1Alg, newSquareCoords) # img2Res = imsup.CropImageROICoords(img2Alg, newSquareCoords) # imsup.SaveAmpImage(img1Alg, 'holo1_big.png') # imsup.SaveAmpImage(img2Alg, 'holo2_big.png') # imsup.SaveAmpImage(img1Res, 'holo1.png') # imsup.SaveAmpImage(img2Res, 'holo2.png') self.pointSets[0][:] = [ CalcNewCoords(SwitchXY(rotCenters[idx]), [-512, -512]) for idx in range(3) ] self.pointSets[1][:] = [ CalcNewCoords(SwitchXY(rotCenters[idx]), [-512, -512]) for idx in range(3) ] print(self.pointSets[0]) print(self.pointSets[1]) return
def movePixmapRight(self): cc.MoveImageRight(self.btnGrid.image, int(self.shiftStepEdit.text())) self.btnGrid.createPixmap()
def rec_holo_no_ref_3(sband_img): sband_img = cc.Diff2FFT(sband_img) rec_holo = cc.IFFT(sband_img) rec_holo = imsup.CreateImageWithBufferFromImage(rec_holo) return rec_holo
def triangulateAdvanced(self): triangles = [[ CalcRealCoords(const.dimSize, self.pointSets[trIdx][pIdx]) for pIdx in range(3) ] for trIdx in range(2)] tr1Dists = [ CalcDistance(triangles[0][pIdx1], triangles[0][pIdx2]) for pIdx1, pIdx2 in zip([0, 0, 1], [1, 2, 2]) ] tr2Dists = [ CalcDistance(triangles[1][pIdx1], triangles[1][pIdx2]) for pIdx1, pIdx2 in zip([0, 0, 1], [1, 2, 2]) ] rcSum = [0, 0] rotCenters = [] for idx1 in range(3): for idx2 in range(idx1 + 1, 3): # print(triangles[0][idx1], triangles[0][idx2]) # print(triangles[1][idx1], triangles[1][idx2]) rotCenter = tr.FindRotationCenter( [triangles[0][idx1], triangles[0][idx2]], [triangles[1][idx1], triangles[1][idx2]]) rotCenters.append(rotCenter) print(rotCenter) rcSum = list(np.array(rcSum) + np.array(rotCenter)) rotCenterAvg = list(np.array(rcSum) / 3.0) # print(rotCenterAvg) rcShift = [-int(rc) for rc in rotCenterAvg] rcShift.reverse() img1 = imsup.CopyImage(self.image.prev) img2 = imsup.CopyImage(self.image) # ten padding trzeba jednak dodac ze wszystkich stron bufSz = max([abs(x) for x in rcShift]) # dirV = 't-' if rcShift[1] > 0 else '-b' # dirH = 'l-' if rcShift[0] > 0 else '-r' dirs = 'tblr' # img1Pad = imsup.PadImage(img1, bufSz, 0.0, dirV+dirH) # img2Pad = imsup.PadImage(img2, bufSz, 0.0, dirV+dirH) img1Pad = imsup.PadImage(img1, bufSz, 0.0, dirs) img2Pad = imsup.PadImage(img2, bufSz, 0.0, dirs) img1Rc = cc.ShiftImage(img1Pad, rcShift) img2Rc = cc.ShiftImage(img2Pad, rcShift) # cropCoords = imsup.MakeSquareCoords(imsup.DetermineCropCoords(img1Rc.width, img1Rc.height, rcShift)) # img1Rc = imsup.CropImageROICoords(img1Rc, cropCoords) # img2Rc = imsup.CropImageROICoords(img2Rc, cropCoords) img1Rc = imsup.CreateImageWithBufferFromImage(img1Rc) img2Rc = imsup.CreateImageWithBufferFromImage(img2Rc) imsup.SaveAmpImage(img1Rc, 'a.png') imsup.SaveAmpImage(img2Rc, 'b.png') rotAngles = [] for idx, p1, p2 in zip(range(3), triangles[0], triangles[1]): p1New = CalcNewCoords(p1, rotCenterAvg) p2New = CalcNewCoords(p2, rotCenterAvg) triangles[0][idx] = p1New triangles[1][idx] = p2New rotAngles.append(CalcRotAngle(p1New, p2New)) rotAngleAvg = np.average(rotAngles) mags = [dist1 / dist2 for dist1, dist2 in zip(tr1Dists, tr2Dists)] magAvg = np.average(mags) # tr1InnerAngles = [ CalcInnerAngle(a, b, c) for a, b, c in zip(tr1Dists, tr1Dists[-1:] + tr1Dists[:-1], tr1Dists[-2:] + tr1Dists[:-2]) ] # tr2InnerAngles = [ CalcInnerAngle(a, b, c) for a, b, c in zip(tr2Dists, tr2Dists[-1:] + tr2Dists[:-1], tr2Dists[-2:] + tr2Dists[:-2]) ] # print('---- Triangle 1 ----') # print([ 'R{0} = {1:.2f} px\n'.format(idx + 1, dist) for idx, dist in zip(range(3), tr1Dists) ]) # print([ 'alpha{0} = {1:.0f} deg\n'.format(idx + 1, angle) for idx, angle in zip(range(3), tr1InnerAngles) ]) # print('---- Triangle 2 ----') # print([ 'R{0} = {1:.2f} px\n'.format(idx + 1, dist) for idx, dist in zip(range(3), tr2Dists) ]) # print([ 'alpha{0} = {1:.0f} deg\n'.format(idx + 1, angle) for idx, angle in zip(range(3), tr2InnerAngles) ]) print('---- Magnification ----') print([ 'mag{0} = {1:.2f}x\n'.format(idx + 1, mag) for idx, mag in zip(range(3), mags) ]) print('---- Rotation ----') print([ 'phi{0} = {1:.0f} deg\n'.format(idx + 1, angle) for idx, angle in zip(range(3), rotAngles) ]) # print('---- Shifts ----') # print([ 'dxy{0} = ({1:.1f}, {2:.1f}) px\n'.format(idx + 1, sh[0], sh[1]) for idx, sh in zip(range(3), shifts) ]) # print('------------------') # print('Average magnification = {0:.2f}x'.format(magAvg)) print('Average rotation = {0:.2f} deg'.format(rotAngleAvg)) # print('Average shift = ({0:.0f}, {1:.0f}) px'.format(shiftAvg[0], shiftAvg[1])) # img2Mag = tr.RescaleImageSki2(img2Rc, magAvg) img2Rot = tr.RotateImageSki2(img2Rc, rotAngleAvg, cut=False) # img2Rot = imsup.RotateImage(img2Rc, rotAngleAvg) padSz = (img2Rot.width - img1Rc.width) // 2 img1RcPad = imsup.PadImage(img1Rc, padSz, 0.0, 'tblr') img1RcPad.MoveToCPU() img2Rot.MoveToCPU() img1RcPad.UpdateBuffer() img2Rot.UpdateBuffer() tmpImgList = imsup.ImageList([self.image, img1RcPad, img2Rot]) tmpImgList.UpdateLinks() for img in tmpImgList: print(img.numInSeries) self.pointSets.append([]) self.pointSets.append([]) self.changePixmap(True) print('Triangulation complete!')
def triangulateBasic(self): triangles = [[ CalcRealCoords(const.dimSize, self.pointSets[trIdx][pIdx]) for pIdx in range(3) ] for trIdx in range(2)] tr1Dists = [ CalcDistance(triangles[0][pIdx1], triangles[0][pIdx2]) for pIdx1, pIdx2 in zip([0, 0, 1], [1, 2, 2]) ] tr2Dists = [ CalcDistance(triangles[1][pIdx1], triangles[1][pIdx2]) for pIdx1, pIdx2 in zip([0, 0, 1], [1, 2, 2]) ] mags = [dist1 / dist2 for dist1, dist2 in zip(tr1Dists, tr2Dists)] rotAngles = [] for idx, p1, p2 in zip(range(3), triangles[0], triangles[1]): rotAngles.append(CalcRotAngle(p1, p2)) magAvg = np.average(mags) rotAngleAvg = np.average(rotAngles) img1 = imsup.CopyImage(self.image.prev) img2 = imsup.CopyImage(self.image) # magnification # img2Mag = tr.RescaleImageSki2(img2, magAvg) # rotation print('rotAngles = {0}'.format(rotAngles)) print('rotAngleAvg = {0}'.format(rotAngleAvg)) img2Rot = tr.RotateImageSki2(img2, rotAngleAvg, cut=False) img2RotCut = tr.RotateImageSki2(img2, rotAngleAvg, cut=True) cropCoords = imsup.DetermineCropCoordsForNewWidth( img1.width, img2RotCut.width) img1Crop = imsup.CropImageROICoords(img1, cropCoords) # x-y alignment (shift) imgs1H = imsup.LinkTwoImagesSmoothlyH(img1Crop, img1Crop) linkedImages1 = imsup.LinkTwoImagesSmoothlyV(imgs1H, imgs1H) imgs2H = imsup.LinkTwoImagesSmoothlyH(img2RotCut, img2RotCut) linkedImages2 = imsup.LinkTwoImagesSmoothlyV(imgs2H, imgs2H) img1Alg, img2Alg = cc.AlignTwoImages(linkedImages1, linkedImages2, [0, 1, 2]) newSquareCoords = imsup.MakeSquareCoords( imsup.DetermineCropCoords(img1Crop.width, img1Crop.height, img2Alg.shift)) newSquareCoords[2:4] = list( np.array(newSquareCoords[2:4]) - np.array(newSquareCoords[:2])) newSquareCoords[:2] = [0, 0] img1Res = imsup.CropImageROICoords(img1Alg, newSquareCoords) img2Res = imsup.CropImageROICoords(img2Alg, newSquareCoords) # --- img2RotShift = cc.ShiftImage(img2Rot, img2Alg.shift) newSquareCoords2 = imsup.MakeSquareCoords( imsup.DetermineCropCoords(img2RotShift.width, img2RotShift.height, img2Alg.shift)) img1Crop2 = imsup.CropImageROICoords(img1, newSquareCoords2) img2RotCrop = imsup.CropImageROICoords(img2RotShift, newSquareCoords2) imsup.SaveAmpImage(img1Crop2, 'holo1.png') imsup.SaveAmpImage(img2RotCrop, 'holo2.png') # --- imsup.SaveAmpImage(img1Alg, 'holo1_big.png') imsup.SaveAmpImage(img2Alg, 'holo2_big.png') imsup.SaveAmpImage(img1Res, 'holo1_cut.png') imsup.SaveAmpImage(img2Res, 'holo2_cut.png')
def UnwarpImage(imgRef, img, nDiv, fragCoords): mt = img.memType dt = img.cmpRepr dfChange = -abs(abs(imgRef.defocus) - abs(img.defocus)) print('df_uw({0}, {1}) = {2:.2f} um'.format(imgRef.numInSeries, img.numInSeries, dfChange * 1e6)) # imgRefProp = prop.PropagateBackToDefocus(imgRef, dfChange) # fragCoords = [(b, a) for a in range(nDiv) for b in range(nDiv)] shifts = cc.CalcPartialCrossCorrFunUW(imgRef, img, nDiv, fragCoords) # interpolacja shiftsY = np.copy(shifts.real) shiftsX = np.copy(shifts.imag) shiftsX[shiftsX == 0] = np.nan shiftsY[shiftsY == 0] = np.nan x = np.arange(0, shifts.shape[0]) y = np.arange(0, shifts.shape[1]) # mask invalid values shiftsX = np.ma.masked_invalid(shiftsX) shiftsY = np.ma.masked_invalid(shiftsY) xx, yy = np.meshgrid(x, y) # get only the valid values x1 = xx[~shiftsX.mask] y1 = yy[~shiftsX.mask] x2 = xx[~shiftsY.mask] y2 = yy[~shiftsY.mask] newShiftsX = shiftsX[~shiftsX.mask] newShiftsY = shiftsY[~shiftsY.mask] GDX = interpolate.griddata((x1, y1), newShiftsX.ravel(), (xx, yy), method='nearest') GDY = interpolate.griddata((x2, y2), newShiftsY.ravel(), (xx, yy), method='nearest') # print(type(GDX[0, 0])) newShifts = [ (int(gdx), int(gdy)) for gdx, gdy in zip(GDX.reshape(nDiv**2), GDY.reshape(nDiv**2)) ] # oldShifts = [(int(ox), int(oy)) for ox, oy in zip(shifts.reshape(nDiv ** 2).imag, shifts.reshape(nDiv ** 2).real)] # print(oldShifts) # newShifts = GDX + 1j * GDY # --- allFragCoords = [(b, a) for a in range(nDiv) for b in range(nDiv)] roiNR, roiNC = img.height // nDiv, img.width // nDiv fragsToCorrelate1 = [] fragsToCorrelate2 = [] for x, y in allFragCoords: frag1 = imsup.CropImageROI(imgRef, (y * roiNR, x * roiNC), (roiNR, roiNC), 1) fragsToCorrelate1.append(frag1) frag2 = imsup.CropImageROI(img, (y * roiNR, x * roiNC), (roiNR, roiNC), 1) fragsToCorrelate2.append(frag2) fragsToJoin = imsup.ImageList() for shift, frag1, frag2 in zip(newShifts, fragsToCorrelate1, fragsToCorrelate2): frag2Shifted = cc.ShiftImage(frag2, shift) fragsToJoin.append(frag2Shifted) img2Unwarped = imsup.JoinImages(fragsToJoin, nDiv) imsup.SaveAmpImage(img2Unwarped, 'warp_field.png') # --- # allFragCoords = [(b, a) for a in range(nDiv) for b in range(nDiv)] fragDimSize = img.width // nDiv src = np.array(allFragCoords) print(src, type(src)) src *= fragDimSize dst = src - newShifts img.ReIm2AmPh() img.MoveToCPU() oldMin, oldMax = np.min(img.amPh.am), np.max(img.amPh.am) scaledArray = imsup.ScaleImage(img.amPh.am, -1.0, 1.0) tform3 = tf.ProjectiveTransform() tform3.estimate(src, dst) warped = tf.warp(scaledArray, tform3, output_shape=(img.height, img.width)).astype(np.float32) warpedScaledBack = imsup.ScaleImage(warped, oldMin, oldMax) warpedImage = imsup.Image(warped.shape[0], warped.shape[1]) warpedImage.amPh.am = np.copy(warpedScaledBack) # img.amPh.am = np.copy(warpedImage.amPh.am) img.ChangeMemoryType(mt) img.ChangeComplexRepr(dt) imgRef.ChangeMemoryType(mt) imgRef.ChangeComplexRepr(dt) # imsup.SaveAmpImage(imgRef, 'ref.png') # imsup.SaveAmpImage(img, 'uwImg.png') return warpedImage
def rec_holo_no_ref_1(holo_img): holo_fft = cc.FFT(holo_img) holo_fft = cc.FFT2Diff(holo_fft) holo_fft.ReIm2AmPh() holo_fft.MoveToCPU() return holo_fft