Exemple #1
0
def yGaussianND(param, inds, sidx):
    """
    param: (mean, max - mean, z, y, x, sigmaz, sigmay, sigmax)
    ind:   coordinate as N.indices (float32)
    sidx:  idx of param where sigma starts (2 + ndim)
    """
    s = 2. * param[sidx:] * param[sidx:]  # sigma
    zyx = param[2:sidx]  #- 0.5 # use pixel center
    DST = N.zeros(inds[0].shape, inds[0].dtype.type)
    for i in range(sidx - 2):
        IND = zyx[i] - inds[i]
        DST -= (IND * IND) / s[i]

    return param[0] + param[1] * N.exp(DST)
Exemple #2
0
def yGaussianND(param, inds, sidx):
    """
    param: (mean, max - mean, z, y, x, sigmaz, sigmay, sigmax)
    ind:   coordinate as N.indices (float32)
    sidx:  idx of param where sigma starts (2 + ndim)
    """
    s = 2. * param[sidx:] * param[sidx:] # sigma
    zyx = param[2:sidx] #- 0.5 # use pixel center
    DST = N.zeros(inds[0].shape, inds[0].dtype.type)
    for i in range(sidx - 2):
        IND = zyx[i] - inds[i]
        DST -= (IND * IND) / s[i]

    return param[0] + param[1] * N.exp(DST)
Exemple #3
0
def logpolar(image, center=None, angles=None, radii=None):
    """Return log-polar transformed image and log base."""
    shape = image.shape
    if center is None:
        center = shape[0] / 2, shape[1] / 2
    if angles is None:
        angles = shape[0]
    if radii is None:
        radii = shape[1]
    theta = N.zeros((angles, radii), dtype=N.float64)
    theta.T[:] = -N.linspace(0, N.pi, angles, endpoint=False)
    #d = radii
    d = N.hypot(shape[0] - center[0], shape[1] - center[1])
    log_base = 10.0**(N.log10(d) / (radii))
    radius = N.empty_like(theta)
    radius[:] = N.power(log_base, N.arange(radii, dtype=N.float64)) - 1.0
    x = radius * N.sin(theta) + center[0]
    y = radius * N.cos(theta) + center[1]
    output = N.zeros_like(x)
    ndii.map_coordinates(image, [x, y], output=output)
    return output, log_base
Exemple #4
0
def radialaverage(data, center=None, useMaxShape=False):
    """
    data: ND array
    center: coordinate of center of radii
    useMinShape: the output uses the maximum shape available

    return 1D array
    """
    if center is None:
        center = N.array(data.shape) // 2
    if len(center) != data.ndim:
        raise ValueError(
            'dimension of center (%i) does not match the dimension of data (%i)'
            % (len(center), data.ndim))

    zyx = N.indices((data.shape))
    r = N.zeros(data.shape, N.float32)
    for i, t in enumerate(zyx):
        r += (t - center[i])**2
    r = N.sqrt(r)
    #y, x = N.indices((data.shape))
    #r = N.sqrt((x - center[0])**2 + (y - center[1])**2) # distance from the center
    r = r.astype(N.int)

    if data.dtype.type in (N.complex64, N.complex128):
        rbin = N.bincount(r.ravel(), data.real.ravel())
        ibin = N.bincount(r.ravel(), data.imag.ravel())
        tbin = N.empty(rbin.shape, data.dtype.type)
        tbin.real = rbin
        tbin.imag = ibin

    else:
        tbin = N.bincount(r.ravel(), data.ravel())
    nr = N.bincount(r.ravel())
    radialprofile = tbin / nr.astype(N.float32)

    if not useMaxShape:
        minShape = min(list(N.array(data.shape) - center) + list(center))
        radialprofile = radialprofile[:minShape]
    return radialprofile
def logpolar(image, center=None, angles=None, radii=None):
    """Return log-polar transformed image and log base."""
    shape = image.shape
    if center is None:
        center = shape[0] / 2, shape[1] / 2
    if angles is None:
        angles = shape[0]
    if radii is None:
        radii = shape[1]
    theta = N.zeros((angles, radii), dtype=N.float64)
    theta.T[:] = -N.linspace(0, N.pi, angles, endpoint=False)
    #d = radii
    d = N.hypot(shape[0]-center[0], shape[1]-center[1])
    log_base = 10.0 ** (N.log10(d) / (radii))
    radius = N.empty_like(theta)
    radius[:] = N.power(log_base, N.arange(radii,
                                                   dtype=N.float64)) - 1.0
    x = radius * N.sin(theta) + center[0]
    y = radius * N.cos(theta) + center[1]
    output = N.zeros_like(x)
    ndii.map_coordinates(image, [x, y], output=output)
    return output, log_base
def radialaverage(data, center=None, useMaxShape=False):
    """
    data: ND array
    center: coordinate of center of radii
    useMinShape: the output uses the maximum shape available

    return 1D array
    """
    if center is None:
        center = N.array(data.shape) // 2
    if len(center) != data.ndim:
        raise ValueError('dimension of center (%i) does not match the dimension of data (%i)' % (len(center), data.ndim))

    zyx = N.indices((data.shape))
    r = N.zeros(data.shape, N.float32)
    for i, t in enumerate(zyx):
        r += (t - center[i])**2
    r = N.sqrt(r)
    #y, x = N.indices((data.shape))
    #r = N.sqrt((x - center[0])**2 + (y - center[1])**2) # distance from the center
    r = r.astype(N.int)

    if data.dtype.type in (N.complex64, N.complex128):
        rbin = N.bincount(r.ravel(), data.real.ravel())
        ibin = N.bincount(r.ravel(), data.imag.ravel())
        tbin = N.empty(rbin.shape, data.dtype.type)
        tbin.real = rbin
        tbin.imag = ibin
        
    else:
        tbin = N.bincount(r.ravel(), data.ravel())
    nr = N.bincount(r.ravel())
    radialprofile = tbin / nr.astype(N.float32)

    if not useMaxShape:
        minShape = min(list(N.array(data.shape) - center) + list(center))
        radialprofile = radialprofile[:minShape]
    return radialprofile 
Exemple #7
0
def keepShape(a, shape, difmod=None):
    canvas = N.zeros(shape, a.dtype.type)

    if difmod is None:
        dif = (shape - N.array(a.shape, N.float32)) / 2.
        mod = N.ceil(N.mod(dif, 1))
    else:
        dif, mod = difmod
    dif = N.where(dif > 0, N.ceil(dif), N.floor(dif))

    # smaller
    aoff = N.where(dif < 0, 0, dif)
    aslc = [slice(dp, shape[i] - dp + mod[i]) for i, dp in enumerate(aoff)]

    # larger
    coff = N.where(dif > 0, 0, -dif)
    cslc = [slice(dp, a.shape[i] - dp + mod[i]) for i, dp in enumerate(coff)]

    canvas[aslc] = a[cslc]

    if difmod is None:
        return canvas, mod
    else:
        return canvas
def keepShape(a, shape, difmod=None):
    canvas = N.zeros(shape, a.dtype.type)

    if difmod is None:
        dif = (shape - N.array(a.shape, N.float32)) / 2.
        mod = N.ceil(N.mod(dif, 1))
    else:
        dif, mod = difmod
    dif = N.where(dif > 0, N.ceil(dif), N.floor(dif))

    # smaller
    aoff = N.where(dif < 0, 0, dif)
    aslc = [slice(dp, shape[i]-dp+mod[i]) for i, dp in enumerate(aoff)]

    # larger
    coff = N.where(dif > 0, 0, -dif)
    cslc = [slice(dp, a.shape[i]-dp+mod[i]) for i, dp in enumerate(coff)]

    canvas[aslc] = a[cslc]

    if difmod is None:
        return canvas, mod
    else:
        return canvas