def logpolar_cv(img, center=None, mag=1): des = N.zeros_like(img) if center is None: center = N.divide(img.shape, 2) # cv.fromarray: array can be 2D or 3D only cimg = cv.fromarray(img) cdes = cv.fromarray(des) cv.LogPolar(cimg, cdes, tuple(center), mag)#, cv.CV_WARP_FILL_OUTLIERS) return N.array(cdes)
def logpolar_cv(img, center=None, mag=1): des = N.zeros_like(img) if center is None: center = N.divide(img.shape, 2) # cv.fromarray: array can be 2D or 3D only cimg = cv.fromarray(img) cdes = cv.fromarray(des) cv.LogPolar(cimg, cdes, tuple(center), mag) #, cv.CV_WARP_FILL_OUTLIERS) return N.array(cdes)
def paddingValue(img, shape, value=0, shift=None, smooth=0, interpolate=True): """ shape: in the same dimension as img value: value in padded region, can be scaler or array with the shape shift: scaler or in the same dimension as img and shape (default 0) smooth: scaler value to smoothen border (here value must be scaler) interpolate: shift array by subpixel interpolation to adjust center return: padded array with shape """ # create buffer dtype = img.dtype.type canvas = N.empty(shape, dtype) canvas[:] = value # calculate position shape = N.array(shape) shapeS = img.shape center = N.divide(shape, 2) if shift is None: shift = 0#[0] * len(shapeS) shapeL = shape#N.add(shapeS, center+shift) #start, stop = (shapeL - shapeS)/2., (shapeL + shapeS)/2. start = N.round_((shapeL - shapeS)/2.).astype(N.int) stop = shapeS + start slc = [slice(start[d], stop[d], None) for d in range(img.ndim)] #slc = [slice(int(round(start[d])), int(round(stop[d])), None) for d in range(img.ndim)] #print slc, shapeS, shapeL # shift if necessary if interpolate: subpx_shift = start % 1 # should be 0.5 or 0 if N.sometrue(subpx_shift): img = U.nd.shift(img, subpx_shift) # padding canvas[slc] = img if smooth: canvas = _smoothBorder(canvas, start, stop, smooth, value) canvas = N.ascontiguousarray(canvas) #print shapeS, shapeL, slc return canvas
def paddingValue(img, shape, value=0, shift=None, smooth=0, interpolate=True): """ shape: in the same dimension as img value: value in padded region, can be scaler or array with the shape shift: scaler or in the same dimension as img and shape (default 0) smooth: scaler value to smoothen border (here value must be scaler) interpolate: shift array by subpixel interpolation to adjust center return: padded array with shape """ # create buffer dtype = img.dtype.type canvas = N.empty(shape, dtype) canvas[:] = value # calculate position shape = N.array(shape) shapeS = img.shape center = N.divide(shape, 2) if shift is None: shift = 0 #[0] * len(shapeS) shapeL = shape #N.add(shapeS, center+shift) #start, stop = (shapeL - shapeS)/2., (shapeL + shapeS)/2. start = N.round_((shapeL - shapeS) / 2.).astype(N.int) stop = shapeS + start slc = [slice(start[d], stop[d], None) for d in range(img.ndim)] #slc = [slice(int(round(start[d])), int(round(stop[d])), None) for d in range(img.ndim)] #print slc, shapeS, shapeL # shift if necessary if interpolate: subpx_shift = start % 1 # should be 0.5 or 0 if N.sometrue(subpx_shift): img = U.nd.shift(img, subpx_shift) # padding canvas[slc] = img if smooth: canvas = _smoothBorder(canvas, start, stop, smooth, value) canvas = N.ascontiguousarray(canvas) #print shapeS, shapeL, slc return canvas
def Xcorr(a, b, phaseContrast=PHASE, nyquist=NYQUIST, removeEdge=0, gFit=True, win=11, ret=None, searchRad=None): """ sigma uses F.gaussianArr in the Fourier domain if ret is None: return zyx, xcf elif ret is 2: return s, v, zyx, xcf elif ret: return v, zyx, xcf """ #print 'phase contrast: %s' % str(phaseContrast) #global DATA # correct odd shape particularly Z axis a = N.squeeze(a) b = N.squeeze(b) a = imgFilters.evenShapeArr(a) b = imgFilters.evenShapeArr(b) shape = N.array(a.shape) # apodize a = apodize(a) b = apodize(b) # fourier transform af = F.rfft(a.astype(N.float32)) bf = F.rfft(b.astype(N.float32)) del a, b # phase contrast filter (removing any intensity information) if phaseContrast: afa = phaseContrastFilter(af, True, nyquist=nyquist) bfa = phaseContrastFilter(bf, True, nyquist=nyquist) else: afa = af bfa = bf del af, bf # removing edge if gaussian is not sufficient targetShape = shape - N.multiply(removeEdge, 2) if removeEdge: ap = imgFilters.cutOutCenter(F.irfft(afa), targetShape) bp = imgFilters.cutOutCenter(F.irfft(bfa), targetShape) afa = F.rfft(ap) bfa = F.rfft(bp) del ap, bp # shift array delta = targetShape / 2. shiftarr = F.fourierRealShiftArr(tuple(targetShape), delta) bfa *= shiftarr # cross correlation bfa = bfa.conjugate() c = cc = F.irfft(afa * bfa) center = N.divide(c.shape, 2) if searchRad: slc = imgGeo.nearbyRegion(c.shape, center, searchRad) cc = N.zeros_like(c) cc[slc] = c[slc] v, zyx, s = _findMaxXcor(cc, win, gFit=gFit) zyx -= center if ret == 2: return s, v, zyx, c elif ret: return v, zyx, c else: return zyx, c
def Xcorr(a, b, phaseContrast=PHASE, nyquist=NYQUIST, gFit=True, win=11, ret=None, searchRad=None, npad=4): """ sigma uses F.gaussianArr in the Fourier domain if ret is None: return zyx, xcf elif ret is 2: return s, v, zyx, xcf elif ret is 3: return zyx, xcf, a_phase_cotrast, b_phase_contrast elif ret: return v, zyx, xcf """ #print 'phase contrast: %s' % str(phaseContrast) #global DATA # correct odd shape particularly Z axis a = N.squeeze(a) b = N.squeeze(b) a = imgFilters.evenShapeArr(a) b = imgFilters.evenShapeArr(b) shape = N.array(a.shape) # padding strange shape #nyx = max(shape[-2:]) #pshape = N.array(a.shape[:-2] + (nyx,nyx)) # apodize a = paddAndApo(a, npad)#, pshape) #apodize(a) b = paddAndApo(b, npad)#, pshape) #apodize(b) # fourier transform af = F.rfft(a.astype(N.float32)) bf = F.rfft(b.astype(N.float32)) del a, b # phase contrast filter (removing any intensity information) if phaseContrast: afa = phaseContrastFilter(af, True, nyquist=nyquist) bfa = phaseContrastFilter(bf, True, nyquist=nyquist) else: afa = af bfa = bf del af, bf #targetShape = shape + (npad * 2) targetShape = shape + (npad * 2) # shift array delta = targetShape / 2. shiftarr = F.fourierRealShiftArr(tuple(targetShape), delta) bfa *= shiftarr # cross correlation bfa = bfa.conjugate() #c = cc = F.irfft(afa * bfa) c = F.irfft(afa * bfa) # 20180214 the padded region was cutout before finding the peak. c = cc = imgFilters.cutOutCenter(c, N.array(c.shape) - (npad * 2), interpolate=False) #cc = c center = N.divide(c.shape, 2) if searchRad: slc = imgGeo.nearbyRegion(c.shape, center, searchRad) cc = N.zeros_like(c) cc[slc] = c[slc] v, zyx, s = _findMaxXcor(cc, win, gFit=gFit) #return cc #print(zyx, center) zyx -= center #c = imgFilters.cutOutCenter(c, N.array(c.shape) - (npad * 2), interpolate=False) #c = imgFilters.cutOutCenter(c, shape, interpolate=False) if ret == 3: return zyx, c, F.irfft(afa), F.irfft(bfa) elif ret == 2: return s, v, zyx, c elif ret: return v, zyx, c else: return zyx, c
def Xcorr(a, b, phaseContrast=PHASE, nyquist=NYQUIST, gFit=True, win=11, ret=None, searchRad=None, npad=4): """ sigma uses F.gaussianArr in the Fourier domain if ret is None: return zyx, xcf elif ret is 2: return s, v, zyx, xcf elif ret is 3: return zyx, xcf, a_phase_cotrast, b_phase_contrast elif ret: return v, zyx, xcf """ #print 'phase contrast: %s' % str(phaseContrast) #global DATA # correct odd shape particularly Z axis a = N.squeeze(a) b = N.squeeze(b) a = imgFilters.evenShapeArr(a) b = imgFilters.evenShapeArr(b) shape = N.array(a.shape) # padding strange shape #nyx = max(shape[-2:]) #pshape = N.array(a.shape[:-2] + (nyx,nyx)) # apodize a = paddAndApo(a, npad) #, pshape) #apodize(a) b = paddAndApo(b, npad) #, pshape) #apodize(b) # fourier transform af = F.rfft(a.astype(N.float32)) bf = F.rfft(b.astype(N.float32)) del a, b # phase contrast filter (removing any intensity information) if phaseContrast: afa = phaseContrastFilter(af, True, nyquist=nyquist) bfa = phaseContrastFilter(bf, True, nyquist=nyquist) else: afa = af bfa = bf del af, bf #targetShape = shape + (npad * 2) targetShape = shape + (npad * 2) # shift array delta = targetShape / 2. shiftarr = F.fourierRealShiftArr(tuple(targetShape), delta) bfa *= shiftarr # cross correlation bfa = bfa.conjugate() c = cc = F.irfft(afa * bfa) center = N.divide(c.shape, 2) if searchRad: slc = imgGeo.nearbyRegion(c.shape, center, searchRad) cc = N.zeros_like(c) cc[slc] = c[slc] v, zyx, s = _findMaxXcor(cc, win, gFit=gFit) zyx -= center c = imgFilters.cutOutCenter(c, N.array(c.shape) - (npad * 2), interpolate=False) #c = imgFilters.cutOutCenter(c, shape, interpolate=False) if ret == 3: return zyx, c, F.irfft(afa), F.irfft(bfa) elif ret == 2: return s, v, zyx, c elif ret: return v, zyx, c else: return zyx, c