示例#1
0
def mexhatFilter(a, mexSize=1):#, trimRatio=0.9):
    """
    returned array is trimmed to remove edge
    """
    global mexhatC, mexhatC_size, mexhatCf

    a = imgFilters.evenShapeArr(a)
    from Priithon.all import F as fftw
    try:
        if mexhatC.shape != a.shape:
            raise ValueError('go to except')
        if mexhatC_size != mexSize:
            raise ValueError('go to except')
    except NameError as ValueError:
        mexhatC_size = mexSize
        shape = N.asarray(a.shape, N.int)#N.float32)
        mexhatC = F.shift(F.mexhatArr(shape, scaleHalfMax=mexhatC_size, orig=None)) # orig 0.5 pixel does not work...
        mexhatCf = fftw.rfft(mexhatC) / N.multiply.reduce( shape )

    ar = fftw.irfft( fftw.rfft(a.astype(N.float32)) * mexhatCf )
    
    #if trimRatio < 1:
    #    ar = trim3D(ar, trimRatio)
    #ar = imgFilters.maskEdgeWithValue2D(ar) # 2 pixels at the edges
    return ar
示例#2
0
def mexhatFilter(a, mexSize=1):  #, trimRatio=0.9):
    """
    returned array is trimmed to remove edge
    """
    global mexhatC, mexhatC_size, mexhatCf

    a = imgFilters.evenShapeArr(a)
    from Priithon.all import F as fftw
    try:
        if mexhatC.shape != a.shape:
            raise ValueError('go to except')
        if mexhatC_size != mexSize:
            raise ValueError('go to except')
    except NameError as ValueError:
        mexhatC_size = mexSize
        shape = N.asarray(a.shape, N.int)  #N.float32)
        mexhatC = F.shift(
            F.mexhatArr(shape, scaleHalfMax=mexhatC_size,
                        orig=None))  # orig 0.5 pixel does not work...
        mexhatCf = fftw.rfft(mexhatC) / N.multiply.reduce(shape)

    ar = fftw.irfft(fftw.rfft(a.astype(N.float32)) * mexhatCf)

    #if trimRatio < 1:
    #    ar = trim3D(ar, trimRatio)
    #ar = imgFilters.maskEdgeWithValue2D(ar) # 2 pixels at the edges
    return ar
示例#3
0
def arr_edgeFilter(img, sigma=1.5):
    """
    average-deviation with a gaussian prefilter
    img must be in an even shape
    """
    if sigma:
        g = gaussianArrND(img.shape, sigma)
        g = F.shift(g)
        img = F.convolve(img.astype(N.float32), g)
    gr = N.gradient(img.astype(N.float32))
    ff = N.sum(N.power(gr, 2), 0)
    return ff 
示例#4
0
def arr_edgeFilter(img, sigma=1.5):
    """
    average-deviation with a gaussian prefilter
    img must be in an even shape
    """
    if sigma:
        g = gaussianArrND(img.shape, sigma)
        g = F.shift(g)
        img = F.convolve(img.astype(N.float32), g)
    gr = N.gradient(img.astype(N.float32))
    ff = N.sum(N.power(gr, 2), 0)
    return ff
示例#5
0
def highPassF(af, highpassSigma=2.5, wiener=0.2, cutoffFreq=3):
    """
    fourie space operations
    af: array after rfft
    half_nyx: half shape required for highpass filter
    highpassSigma: highpass filter, if 0, highpass is not done
    wiener: wiener coefficient for highpass filte
    cutoffFreq: band-pass around origin

    return: array BEFORE irfft

    WARNING: af will be changed, so use copy() if necessary
    """
    global _G, _G_SHAPE
    if highpassSigma:
        shape = N.array(af.shape)
        shape[-1] = (shape[-1] - 1) * 2
        szyx = shape / 2.

        if _G is not None and N.alltrue(_G_SHAPE == shape):
            g = _G
        else:
            g = imgFilters.gaussianArrND(shape,
                                         highpassSigma,
                                         peakVal=1,
                                         orig=szyx)
            g = F.shift(g)[..., :af.shape[-1]]

            _G = g
            _G_SHAPE = N.asarray(g.shape)
        g += wiener
        af /= g

    # kill DC
    af.flat[0] = 0
    # kill lowest freq in YX
    for d in range(af.ndim - 2, af.ndim):
        upperdim = ':,' * d
        exec('af[%s0:cutoffFreq] = 0' % upperdim)

    return af
示例#6
0
def highPassF(af, highpassSigma=2.5, wiener=0.2, cutoffFreq=3):
    """
    fourie space operations
    af: array after rfft
    half_nyx: half shape required for highpass filter
    highpassSigma: highpass filter, if 0, highpass is not done
    wiener: wiener coefficient for highpass filte
    cutoffFreq: band-pass around origin

    return: array BEFORE irfft

    WARNING: af will be changed, so use copy() if necessary
    """
    global _G, _G_SHAPE
    if highpassSigma:
        shape = N.array(af.shape)
        shape[-1] = (shape[-1] - 1) * 2
        szyx = shape / 2.

        if _G is not None and N.alltrue(_G_SHAPE == shape):
            g = _G
        else:
            g = imgFilters.gaussianArrND(shape, highpassSigma, peakVal=1, orig=szyx)
            g = F.shift(g)[...,:af.shape[-1]]

            _G = g
            _G_SHAPE = N.asarray(g.shape)
        g += wiener
        af /= g

    # kill DC
    af.flat[0] = 0
    # kill lowest freq in YX
    for d in range(af.ndim-2, af.ndim):
        upperdim = ':,' * d
        exec('af[%s0:cutoffFreq] = 0' % upperdim)
    
    return af
示例#7
0
def mexhatFilter(a, mexSize=1):  #, trimRatio=0.9):
    """
    returned array is trimmed to remove edge
    """
    global mexhatC, mexhatC_size, mexhatCf

    a = imgFilters.evenShapeArr(a)
    try:  # inside package
        from ..Priithon.all import F as fftw
    except ValueError:  # Attempted relative import beyond toplevel package
        from Priithon.all import F as fftw
    #from Priithon.all import F as fftw
    try:
        if mexhatC.shape != a.shape:
            raise ValueError, 'go to except'
        if mexhatC_size != mexSize:
            raise ValueError, 'go to except'
    except NameError, ValueError:
        mexhatC_size = mexSize
        shape = N.asarray(a.shape, N.float32)
        mexhatC = F.shift(
            F.mexhatArr(shape, scaleHalfMax=mexhatC_size,
                        orig=None))  # orig 0.5 pixel does not work...
        mexhatCf = fftw.rfft(mexhatC) / N.multiply.reduce(shape)
示例#8
0
def Xcorr(a, b, highpassSigma=2.5, wiener=0.2, cutoffFreq=3,
forceSecondPeak=None, acceptOrigin=True, maskSigmaFact=1., removeY=None, removeX=None, ret=None, normalize=True, gFit=True, lap=None, win=11):
    """
    returns (y,x), image
    if ret is True, returns [v, yx, image]

    to get yx cordinate of the image,
    yx += N.divide(picture.shape, 2)

    a, b:            2D array
    highpassSigma:   sigma value used for highpass pre-filter
    wiener:          wiener value used for highpass pre-filter
    cutoffFreq:      kill lowest frequency component from 0 to this level
    forceSecondPeak: If input is n>0 (True is 1), pick up n-th peak
    acceptOrigin:    If None, result at origin is rejected, look for the next peak
    maskSigmaFact:   Modifier to remove previous peak to look for another peak
    removeYX:        Rremove given number of pixel high intensity lines of the Xcorr
                     Y: Vertical, X: Horizontal
    normalize:       intensity normalized
    gFit:            peak is fitted to 2D gaussian array, if None use center of mass
    win:             window for gFit

    if b is a + (y,x) then, answer is (-y,-x)
    """
    shapeA = N.asarray(a.shape)
    shapeB = N.asarray(b.shape)
    shapeM = N.max([shapeA, shapeB], axis=0)
    shapeM = N.where(shapeM % 2, shapeM+1, shapeM)
    center = shapeM / 2.

    arrs = [a,b]
    arrsS = ['a','b']
    arrsF = []
    for i, arr in enumerate(arrs):
        if arr.dtype not in [N.float32, N.float64]:
            arr = N.asarray(arr, N.float32)
        # this convolution has to be done beforehand to remove 2 pixels at the edge
        if lap == 'nothing':
            pass
        elif lap:
            arr = arr_Laplace(arr, mask=2)
        else:
            arr = arr_sorbel(arr, mask=1)
    
        if N.sometrue(shapeA < shapeM):
            arr = paddingMed(arr, shapeM)

        if normalize:
            mi, ma, me, sd = U.mmms(arr)
            arr = (arr - me) / sd
    
        if i ==1:
            arr = F.shift(arr)
        af = F.rfft(arr)

        af = highPassF(af, highpassSigma, wiener, cutoffFreq)
        arrsF.append(af)

    # start cross correlation
    af, bf = arrsF
    bf = bf.conjugate()
    cf = af * bf

    # go back to space domain
    c = F.irfft(cf)
  #  c = _changeOrigin(cr)

    # removing lines
    if removeX:
        yi, xi = N.indices((removeX, shapeM[-1]))#sx))
        yi += center[-2] - removeX/2.#sy/2 - removeX/2
        c[yi, xi] = 0
    if removeY:
        yi, xi = N.indices((shapeM[-2], removeY))#sy, removeY))
        xi += center[-1] - removeY/2.#sx/2 - removeY/2
        c[yi, xi] = 0

    # find the first peak
    if gFit:
        v, yx, s = findMaxWithGFit(c, win=win)#, window=win, gFit=gFit)
        if v == 0:
            v, yx, s = findMaxWithGFit(c, win=win+2)#, window=win+2, gFit=gFit)
            if v == 0:
                v = U.findMax(c)[0]
        yx = N.add(yx, 0.5)
        #yx += 0.5
    else:
        vzyx = U.findMax(c)
        v = vzyx[0]
        yx = vzyx[-2:]
        s = 2.5

    yx -= center

    if N.alltrue(N.abs(yx) < 1.0) and not acceptOrigin:
        forceSecondPeak = True

    # forceSecondPeak:
    if not forceSecondPeak:
        forceSecondPeak = 0
    for i in range(int(forceSecondPeak)):
        print('%i peak was removed' % (i+1)) #, sigma: %.2f' % (i+1, s)
        yx += center
        g = gaussianArr2D(c.shape, sigma=s/maskSigmaFact, peakVal=v, orig=yx)
        c = c - g
        #c = mask_gaussian(c, yx[0], yx[1], v, s)
        if gFit:
            v, yx, s = findMaxWithGFit(c, win=win)#, window=win, gFit=gFit)
            if v == 0:
                v, yx, s = findMaxWithGFit(c, win=win+2)#, window=win+2, gFit=gFit)
                if v == 0:
                    v = U.findMax(c)[0]
            yx -= (center - 0.5)
        else:
            vzyx = U.findMax(c)
            v = vzyx[0]

    if not gFit:
        yx = centerOfMass(c, vzyx[-2:]) - center
    if lap is not 'nothing':
        c = paddingValue(c, shapeM+2)

    if ret == 2:
        return yx, af, bf.conjugate()
    elif ret:
        return v, yx, c
    else:
        return yx, c
示例#9
0
def Xcorr(a,
          b,
          highpassSigma=2.5,
          wiener=0.2,
          cutoffFreq=3,
          forceSecondPeak=None,
          acceptOrigin=True,
          maskSigmaFact=1.,
          removeY=None,
          removeX=None,
          ret=None,
          normalize=True,
          gFit=True,
          lap=None,
          win=11):
    """
    returns (y,x), image
    if ret is True, returns [v, yx, image]

    to get yx cordinate of the image,
    yx += N.divide(picture.shape, 2)

    a, b:            2D array
    highpassSigma:   sigma value used for highpass pre-filter
    wiener:          wiener value used for highpass pre-filter
    cutoffFreq:      kill lowest frequency component from 0 to this level
    forceSecondPeak: If input is n>0 (True is 1), pick up n-th peak
    acceptOrigin:    If None, result at origin is rejected, look for the next peak
    maskSigmaFact:   Modifier to remove previous peak to look for another peak
    removeYX:        Rremove given number of pixel high intensity lines of the Xcorr
                     Y: Vertical, X: Horizontal
    normalize:       intensity normalized
    gFit:            peak is fitted to 2D gaussian array, if None use center of mass
    win:             window for gFit

    if b is a + (y,x) then, answer is (-y,-x)
    """
    shapeA = N.asarray(a.shape)
    shapeB = N.asarray(b.shape)
    shapeM = N.max([shapeA, shapeB], axis=0)
    shapeM = N.where(shapeM % 2, shapeM + 1, shapeM)
    center = shapeM / 2.

    arrs = [a, b]
    arrsS = ['a', 'b']
    arrsF = []
    for i, arr in enumerate(arrs):
        if arr.dtype not in [N.float32, N.float64]:
            arr = N.asarray(arr, N.float32)
        # this convolution has to be done beforehand to remove 2 pixels at the edge
        if lap == 'nothing':
            pass
        elif lap:
            arr = arr_Laplace(arr, mask=2)
        else:
            arr = arr_sorbel(arr, mask=1)

        if N.sometrue(shapeA < shapeM):
            arr = paddingMed(arr, shapeM)

        if normalize:
            mi, ma, me, sd = U.mmms(arr)
            arr = (arr - me) / sd

        if i == 1:
            arr = F.shift(arr)
        af = F.rfft(arr)

        af = highPassF(af, highpassSigma, wiener, cutoffFreq)
        arrsF.append(af)

    # start cross correlation
    af, bf = arrsF
    bf = bf.conjugate()
    cf = af * bf

    # go back to space domain
    c = F.irfft(cf)
    #  c = _changeOrigin(cr)

    # removing lines
    if removeX:
        yi, xi = N.indices((removeX, shapeM[-1]))  #sx))
        yi += center[-2] - removeX / 2.  #sy/2 - removeX/2
        c[yi, xi] = 0
    if removeY:
        yi, xi = N.indices((shapeM[-2], removeY))  #sy, removeY))
        xi += center[-1] - removeY / 2.  #sx/2 - removeY/2
        c[yi, xi] = 0

    # find the first peak
    if gFit:
        v, yx, s = findMaxWithGFit(c, win=win)  #, window=win, gFit=gFit)
        if v == 0:
            v, yx, s = findMaxWithGFit(c, win=win +
                                       2)  #, window=win+2, gFit=gFit)
            if v == 0:
                v = U.findMax(c)[0]
        yx = N.add(yx, 0.5)
        #yx += 0.5
    else:
        vzyx = U.findMax(c)
        v = vzyx[0]
        yx = vzyx[-2:]
        s = 2.5

    yx -= center

    if N.alltrue(N.abs(yx) < 1.0) and not acceptOrigin:
        forceSecondPeak = True

    # forceSecondPeak:
    if not forceSecondPeak:
        forceSecondPeak = 0
    for i in range(int(forceSecondPeak)):
        print('%i peak was removed' % (i + 1))  #, sigma: %.2f' % (i+1, s)
        yx += center
        g = gaussianArr2D(c.shape, sigma=s / maskSigmaFact, peakVal=v, orig=yx)
        c = c - g
        #c = mask_gaussian(c, yx[0], yx[1], v, s)
        if gFit:
            v, yx, s = findMaxWithGFit(c, win=win)  #, window=win, gFit=gFit)
            if v == 0:
                v, yx, s = findMaxWithGFit(c, win=win +
                                           2)  #, window=win+2, gFit=gFit)
                if v == 0:
                    v = U.findMax(c)[0]
            yx -= (center - 0.5)
        else:
            vzyx = U.findMax(c)
            v = vzyx[0]

    if not gFit:
        yx = centerOfMass(c, vzyx[-2:]) - center
    if lap is not 'nothing':
        c = paddingValue(c, shapeM + 2)

    if ret == 2:
        return yx, af, bf.conjugate()
    elif ret:
        return v, yx, c
    else:
        return yx, c