コード例 #1
0
ファイル: xcorr.py プロジェクト: macronucleus/Chromagnon
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
ファイル: xcorr.py プロジェクト: mueller-physics/Chromagnon
def phaseContrastFilter(a, inFourier=False, removeNan=True, nyquist=0.6):
    global GAUSS
    if inFourier:
        af = a.copy()
    else:
        af = F.rfft(a)

    # here is the phase contrast
    amp = N.abs(af)
    afa = af / amp

    if removeNan:
        afa = nanFilter(afa)

    # lowpass gaussian filter of phase image
    if nyquist:  # since this takes long time, gaussian array is re-used if possible
        if GAUSS is not None and GAUSS[0] == nyquist and GAUSS[
                1].shape == afa.shape:
            nq, gf = GAUSS
        else:
            sigma = af.shape[-1] * nyquist
            gf = F.gaussianArr(afa.shape,
                               sigma,
                               peakVal=1,
                               orig=0,
                               wrap=(1, ) * (afa.ndim - 1) +
                               (0, ))  #, dtype=afa.dtype.type)
            GAUSS = (nyquist, gf)
        afa *= gf

    if inFourier:
        ap = afa
    else:
        ap = F.irfft(afa)
    return ap
コード例 #3
0
ファイル: xcorr.py プロジェクト: iandobbie/Chromagnon
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
コード例 #4
0
    def OnAutoFocus(self, evt=None):
        """
        please read Chromagnon.alignfuncs.findBestRefZs() for detail of the logic
        """
        if self.doc.nt > 1:
            t = int(self.tSliderBox.GetValue())
        else:
            t = 0
        ws = [
            w for w, hist in enumerate(self.hist_toggleButton)
            if hist.GetValue()
        ]

        ms = N.zeros((len(ws), self.doc.nz), N.float32)

        # FFTW does not work with another program using it
        # here is the workaround for Chromagnon
        try:
            batch = self.GetParent().GetParent().GetParent()
            if hasattr(batch, 'th') and batch.th.isAlive():
                for wi, w in enumerate(ws):
                    arr = self.doc.get3DArr(t=t, w=w)
                    for z in range(self.doc.nz):
                        ms[wi, z] = N.prod(U.mmms(arr[z])[-2:])  # mean * std
                v, _, w, z = U.findMax(ms)
                self.zSliderBox.SetValue(str(z))
                self.OnZSliderBox()
                self.OnAutoScale()

                G.openMsg(
                    parent=self.parent,
                    msg=
                    'A clumsy focusing method was used since another program was using FFTW.\nPlease wait for the better method until the other program is done.',
                    title="Please wait")
                return
        except AttributeError:  # no parent?
            pass

        # Frequency-based caluculation starts
        from Priithon.all import F

        ring = F.ringArr(self.doc.shape[-2:],
                         radius1=self.doc.shape[-1] // 10,
                         radius2=self.doc.shape[-2] // 4,
                         orig=(0, 0),
                         wrap=1)

        for wi, w in enumerate(ws):
            arr = self.doc.get3DArr(t=t, w=w)
            arr = arr / arr.mean()
            for z in range(self.doc.nz):
                af = F.rfft(N.ascontiguousarray(arr[z]))
                ar = af * ring[:, :af.shape[-1]]
                ms[wi, z] = N.sum(N.abs(ar))
        v, _, w, z = U.findMax(ms)

        self.zSliderBox.SetValue(str(z))
        self.OnZSliderBox()
        self.OnAutoScale()
コード例 #5
0
ファイル: imgFilters.py プロジェクト: iandobbie/Chromagnon
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
コード例 #6
0
ファイル: imgFilters.py プロジェクト: macronucleus/Chromagnon
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 
コード例 #7
0
ファイル: imgFilters.py プロジェクト: iandobbie/Chromagnon
def zoomFourier(arr, factor, use_abs=False):
    shape = N.array(arr.shape)
    target = [int(s) for s in shape * factor]
    #target[-1] //= 2
    #target[-1] += 1
    af = F.fft(arr)
    ap = paddingFourier(af, target)
    afp = F.ifft(ap)
    factor = target / shape
    if use_abs:
        return N.abs(afp) * N.product(factor)
    else:
        return N.real(afp) * N.product(factor)
コード例 #8
0
ファイル: imgFilters.py プロジェクト: macronucleus/Chromagnon
def zoomFourier(arr, factor, use_abs=False):
    shape = N.array(arr.shape)
    target = [int(s) for s in shape * factor]
    #target[-1] //= 2
    #target[-1] += 1
    af = F.fft(arr)
    ap = paddingFourier(af, target)
    afp = F.ifft(ap)
    factor = target / shape
    if use_abs:
        return N.abs(afp) * N.product(factor)
    else:
        return N.real(afp) * N.product(factor)
コード例 #9
0
ファイル: xcorr.py プロジェクト: mueller-physics/Chromagnon
def bandStopFilter(arr, x=4):
    """
    to remove large pixelation artifact in Z
    """
    #shape = N.asarray(arr.shape)
    #shape = N.where(shape % 2, shape+1, shape)
    #if N.sometrue(shape > arr.shape):
    #    arr = imgFilters.paddingMed(arr, shape)

    fa = F.rfft2d(arr)
    fa[:x, 1:] = 0
    fa[-x:, 1:] = 0
    return F.irfft2d(fa)
コード例 #10
0
ファイル: xcorr.py プロジェクト: macronucleus/Chromagnon
def bandStopFilter(arr, x=4):
    """
    to remove large pixelation artifact in Z
    """
    #shape = N.asarray(arr.shape)
    #shape = N.where(shape % 2, shape+1, shape)
    #if N.sometrue(shape > arr.shape):
    #    arr = imgFilters.paddingMed(arr, shape)

    fa = F.rfft2d(arr)
    fa[:x,1:] = 0
    fa[-x:,1:] = 0
    return F.irfft2d(fa)
コード例 #11
0
ファイル: main.py プロジェクト: macronucleus/Chromagnon
    def OnAutoFocus(self, evt=None):
        """
        please read Chromagnon.alignfuncs.findBestRefZs() for detail of the logic
        """
        if self.doc.nt > 1:
            t = int(self.tSliderBox.GetValue())
        else:
            t = 0
        ws = [w for w, hist in enumerate(self.hist_toggleButton) if hist.GetValue()]

        ms = N.zeros((len(ws),self.doc.nz), N.float32)

        # FFTW does not work with another program using it
        # here is the workaround for Chromagnon
        try:
            batch = self.GetParent().GetParent().GetParent()
            if hasattr(batch, 'th') and batch.th.isAlive():
                for wi, w in enumerate(ws):
                    arr = self.doc.get3DArr(t=t, w=w)
                    for z in range(self.doc.nz):
                        ms[wi,z] = N.prod(U.mmms(arr[z])[-2:]) # mean * std
                v,_,w,z = U.findMax(ms)
                self.zSliderBox.SetValue(str(z))
                self.OnZSliderBox()
                self.OnAutoScale()
                        
                G.openMsg(parent=self.parent, msg='A clumsy focusing method was used since another program was using FFTW.\nPlease wait for the better method until the other program is done.', title="Please wait")
                return
        except AttributeError: # no parent?
            pass

        # Frequency-based caluculation starts 
        from Priithon.all import F
        
        ring = F.ringArr(self.doc.shape[-2:], radius1=self.doc.shape[-1]//10, radius2=self.doc.shape[-2]//4, orig=(0,0), wrap=1)


        for wi, w in enumerate(ws):
            arr = self.doc.get3DArr(t=t, w=w)
            arr = arr / arr.mean()
            for z in range(self.doc.nz):
                af = F.rfft(N.ascontiguousarray(arr[z]))
                ar = af * ring[:,:af.shape[-1]]
                ms[wi,z] = N.sum(N.abs(ar))
        v,_,w,z = U.findMax(ms)

        self.zSliderBox.SetValue(str(z))
        self.OnZSliderBox()
        self.OnAutoScale()
コード例 #12
0
ファイル: imgFilters.py プロジェクト: iandobbie/Chromagnon
def shiftFullFFT(arr, delta=None):
    """
    returns new array: arr shifted by delta (tuple)
       it uses fft (not rfft), multiplying with "shift array", ifft
    delta defaults to half of arr.shape 
    """
    shape = arr.shape
    if delta is None:
        delta = N.array(shape) / 2.
    elif not hasattr(delta, '__len__'):
        delta = (delta, ) * len(shape)
    elif len(shape) != len(delta):
        raise ValueError("shape and delta not same dimension")

    return F.ifft(F.fourierShiftArr(shape, delta) * F.fft(arr))
コード例 #13
0
ファイル: imgFilters.py プロジェクト: macronucleus/Chromagnon
def shiftFullFFT(arr, delta=None):
    """
    returns new array: arr shifted by delta (tuple)
       it uses fft (not rfft), multiplying with "shift array", ifft
    delta defaults to half of arr.shape 
    """
    shape = arr.shape
    if delta is None:
        delta = N.array(shape) / 2.
    elif not hasattr(delta, '__len__'):
        delta = (delta,)*len(shape)
    elif len(shape) != len(delta):
        raise ValueError("shape and delta not same dimension")

    return F.ifft(F.fourierShiftArr(shape, delta) * F.fft(arr))
コード例 #14
0
ファイル: xcorr.py プロジェクト: macronucleus/Chromagnon
def normalizedXcorr(a, b):
    std = N.std(a) * N.std(b)
    a_ = (a - N.mean(a)) / std
    b_ = (b - N.mean(b)) / std

    c = F.convolve(a_, b_, conj=1)# / a.size
    
    return c
コード例 #15
0
ファイル: xcorr.py プロジェクト: mueller-physics/Chromagnon
def normalizedXcorr(a, b):
    std = N.std(a) * N.std(b)
    a_ = (a - N.mean(a)) / std
    b_ = (b - N.mean(b)) / std

    c = F.convolve(a_, b_, conj=1)  # / a.size

    return c
コード例 #16
0
def findBestRefZs(ref, sigma=0.5):
    """
    PSF spread along the Z axis is often tilted in the Y or X axis.
    Thus simple maximum intensity projection may lead to the wrong answer to estimate rotation and magnification.
    On the other hands, taking a single section may also contain out of focus flare from neighboring sections that are tilted.
    Therefore, here sections with high complexity are selected and projected.

    An intensity-based method does not work for most (eg. tissue or bright field) images.
    Using variance is not enough for bright field image where the right focus loses contrast.
    Thus here we used sections containing higher frequency.

    ref: 3D array
    return z idx at the focus
    """
    from Priithon.all import F
    nz = ref.shape[0]
    if ref.ndim == 2:
        return [0]
    elif nz <= 3:
        return range(nz)

    # ring array
    ring = F.ringArr(ref.shape[-2:],
                     radius1=ref.shape[-1] // 10,
                     radius2=ref.shape[-2] // 4,
                     orig=(0, 0),
                     wrap=1)

    # Due to the roll up after FFT, the edge sections in Z may contain different information among the channels. Thus these sections are left unused.
    ms = N.zeros((nz - 2, ), N.float32)
    for z in xrange(1, nz - 1):
        af = F.rfft(N.ascontiguousarray(ref[z]))
        ar = af * ring[:, :af.shape[-1]]
        ms[z - 1] = N.sum(N.abs(ar))

        #ms[z-1] = N.prod(U.mmms(arr)[-2:]) # mean * std
    del ring, af, ar

    mi, ma, me, st = U.mmms(ms)
    thr = me + st * sigma

    ids = [idx for idx in range(1, nz - 1) if ms[idx - 1] > thr]
    if not ids:
        ids = range(1, nz - 1)

    return ids
コード例 #17
0
def makeFiles(fns, std=10, div_step=50, div_max=800):  #1000):
    """
    makes a series of images added Gaussain and Poisson noise

    std: standard deviation for Gaussian, and mean=std*10 for Poisson
    div_step: the step that the original image is divided while noise is added
    div_max: the maximum value that the original image is divided

    return output filenames
    """

    outs = []

    for fn in fns:
        a = Mrc.bindFile(fn)

        for ns in NOISE_STATS:
            hdr = mrcIO.makeHdr_like(a.Mrc.hdr)

            if ns == NOISE_STATS[0]:
                noise = F.noiseArr(a.shape, stddev=std, mean=0)
            else:
                noise = F.poissonArr(a.shape, mean=std * 10)
            steps = range(div_step, div_max + div_step, div_step)
            ag = [a / c + noise for c in steps]
            for i, arr in enumerate(ag):
                val = steps[i]
                if hasattr(arr, "Mrc"):
                    del arr.Mrc
                if arr.dtype == N.float64:
                    arr = arr.astype(N.float32)
                    hdr.PixelType = 2

                out = a.Mrc.path + ns + '%04d' % val
                Mrc.save(arr, out, ifExists='overwrite', hdr=hdr)
                outs.append(out)
    return outs
コード例 #18
0
ファイル: xcorr.py プロジェクト: mueller-physics/Chromagnon
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)
コード例 #19
0
ファイル: xcorr.py プロジェクト: macronucleus/Chromagnon
def phaseContrastFilter(a, inFourier=False, removeNan=True, nyquist=0.6):
    global GAUSS
    if inFourier:
        af = a.copy()
    else:
        af = F.rfft(a)

    # here is the phase contrast
    amp = N.abs(af)
    afa = af / amp

    if removeNan:
        afa = nanFilter(afa)

    # lowpass gaussian filter of phase image
    if nyquist: # since this takes long time, gaussian array is re-used if possible
        if GAUSS is not None and GAUSS[0] == nyquist and GAUSS[1].shape == afa.shape:
            nq, gf = GAUSS
        else:
            #sigma = af.shape[-1] * nyquist
            #gf = F.gaussianArr(afa.shape, sigma, peakVal=1, orig=0, wrap=(1,)*(afa.ndim-1)+(0,))#, dtype=afa.dtype.type)
            gshape = N.array(af.shape)
            if inFourier:
                gshape[-1] *= 2
            sigma = gshape * nyquist
            gf = imgFilters.gaussianArrND(gshape, sigma, peakVal=1)
            gf = N.fft.fftshift(gf)[Ellipsis, :af.shape[-1]]
            
            GAUSS = (nyquist, gf)
        afa *= gf

    if inFourier:
        ap = afa
    else:
        ap = F.irfft(afa)
    return ap
コード例 #20
0
ファイル: xcorr.py プロジェクト: mueller-physics/Chromagnon
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
コード例 #21
0
ファイル: xcorr.py プロジェクト: macronucleus/Chromagnon
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
コード例 #22
0
ファイル: imgFilters.py プロジェクト: macronucleus/Chromagnon
def paddingFourier(af, shape, value=0):
    afo = N.fft.fftshift(af)
    afp = F.getPadded(afo, shape, pad=value)
    return N.fft.ifftshift(afp)
コード例 #23
0
ファイル: imgFilters.py プロジェクト: iandobbie/Chromagnon
def paddingFourier(af, shape, value=0):
    afo = N.fft.fftshift(af)
    afp = F.getPadded(afo, shape, pad=value)
    return N.fft.ifftshift(afp)
コード例 #24
0
ファイル: xcorr.py プロジェクト: mueller-physics/Chromagnon
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
コード例 #25
0
ファイル: microscope.py プロジェクト: macronucleus/Chromagnon
def airyND(shape, orig=None, wrap=False):
    from Priithon.all import F
    from PriCommon import imgFit
    return F.radialArr(shape, imgFit.airy1D, orig, wrap)
コード例 #26
0
ファイル: xcorr.py プロジェクト: macronucleus/Chromagnon
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
コード例 #27
0
ファイル: xcorr.py プロジェクト: mueller-physics/Chromagnon
        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)

    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


GAUSS = None


def phaseContrastFilter(a, inFourier=False, removeNan=True, nyquist=0.6):
    global GAUSS
    if inFourier:
        af = a.copy()
    else:
コード例 #28
0
ファイル: readerSDT.py プロジェクト: LLNL/WVL
def open_bh_sdt(fn, mode='r'):
    """
    read Becker&Hickel SDT file into separate structures
    any error here might result from the file being corrupt
    """
    #b = file(fn, 'rb').read()
    m = N.memmap(fn, mode=mode)

    hdrEnd = dtype_bhfile_header.itemsize
    lenDBhdr = dtype_BHFileBlockHeader.itemsize

    hdr = N.rec.array(m[:hdrEnd], dtype=dtype_bhfile_header)   [0]
    #hdr = m[:hdrEnd].view(dtype_bhfile_header)

    if hdr['header_valid'] != BH_HEADER_VALID:
        print " * WARNING * SDT file file header not valid"

    infoStart = hdr['info_offs']
    infoEnd   = infoStart + hdr['info_length']
    info= m[infoStart:infoEnd]
    info= info.view(N.dtype( (N.string_, len(info)) ))     [0]

    setupStart= hdr['setup_offs']
    setupEnd  = setupStart + hdr['setup_length']
    setup = m[setupStart:setupEnd]
    setup = setup.view(N.dtype( (N.string_, len(setup)) ))    [0]
    setupTxtEnd = N.char.find(setup, '*END') + len('*END')
    setupTxt = setup[:setupTxtEnd]
    # following bytes are: '\r\n\r\n.......'
    # >>> U.hexdump(s.setup[s.setupTxtEnd:s.setupTxtEnd+21])
    # 0000  0d 0a 0d 0a 42 49 4e 5f 50 41 52 41 5f 42 45 47   ....BIN_PARA_BEG
    # 0010  49 4e 3a 00 0e                                    IN:..
    #setupBin = 

    nDBs = hdr['no_of_data_blocks']
    if nDBs == 0x7fff:
        nDBs = hdr['reserved1']

    #if nDBs != 1:
    #    print " * WARNING * SDT file has more than one data block(nDBs=%s)"%(nDBs,)

    nMDBs = hdr['no_of_meas_desc_blocks']
    mdbStart = hdr['meas_desc_block_offs']
    lenMDB = hdr['meas_desc_block_length']
    numMDB = hdr['no_of_meas_desc_blocks']
    dataMeasInfos = m[mdbStart:mdbStart+numMDB*lenMDB].view(dtype=dtype_MeasureInfo)   

    import weakref
    dbHdr  = []
    dbData = []
    db0Start = hdr['data_block_offs']
    o = db0Start
    for i in range(nDBs):
        dbh = m[o:o+lenDBhdr].view(dtype=dtype_BHFileBlockHeader)   [0]
        dbHdr.append( dbh )
        dbo = dbh['data_offs']
        dbl = dbh['block_length']
        #dbd = N.array(m[dbo:dbo+dbl], dtype=N.uint16)
        #ValueError: setting an array element with a sequence.
        dbd = m[dbo:dbo+dbl].view(dtype=N.uint16)

        #dbd.SDT_DataHdr      = dbh
        #dbd.SDT_DataMeasInfo = weakref.proxy( measInfos[ dbh['meas_desc_block_no'] ] )
        dbd = ndarray_inSdtFile(dbd, 
                                dataHdr=dbh, 
                                dataMeasInfo=dataMeasInfos[ dbh['meas_desc_block_no'] ]
                                )

        nx = dbd.SDT_DataMeasInfo['image_x']
        ny = dbd.SDT_DataMeasInfo['image_y']

        if nx>0 and ny >0:           # FLIM !!
            dbd.shape = nx,ny,-1
            dbd = dbd.T

        dbData.append(dbd)
        o = dbh['next_block_offs']
        del dbh,dbd,dbo, dbl,  nx, ny


    del o, i, weakref

    dataBlkHdrs = F.mockNDarray(*dbHdr)
    dataBlks = F.mockNDarray(*dbData)

    del dbHdr, dbData
                                          
    #return U.localsAsOneObject()
    #data = ndarray_inSdtFile(dbData[0], U.localsAsOneObject())
    #data.SDT.data = weakref.proxy( self.) # remove circular reference
    dataBlks.SDT = U.localsAsOneObject()
    import weakref
    dataBlks.SDT.dataBlks = weakref.proxy( dataBlks ) # remove circular reference

    if len(dataBlks) == 1 and dataBlks[0].ndim>1:
        # one FLIM data block
        dataBlks[0].SDT = dataBlks.SDT.dataBlks
        return dataBlks[0]
    else:
        return dataBlks
コード例 #29
0
ファイル: xcorr.py プロジェクト: iandobbie/Chromagnon
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
コード例 #30
0
ファイル: imgFilters.py プロジェクト: macronucleus/Chromagnon
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
コード例 #31
0
ファイル: imgFilters.py プロジェクト: iandobbie/Chromagnon
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