Exemplo n.º 1
0
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
Exemplo n.º 2
0
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
Exemplo n.º 3
0
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