Пример #1
0
def _smoothBorder(arr, start, stop, smooth, value):
    """
    start, stop: [z,y,x]
    """
    # prepare coordinates
    shape = N.array(arr.shape)
    start = N.ceil(start).astype(N.int16)
    stop = N.ceil(stop).astype(N.int16)
    smooth_start = start - smooth
    smooth_stop = stop + smooth
    smooth_start = N.where(smooth_start < 0, 0, smooth_start)
    smooth_stop = N.where(smooth_stop > shape, shape, smooth_stop)
    #print smooth_start, smooth_stop

    import copy
    sliceTemplate = [slice(None, None, None)] * arr.ndim
    shapeTemplate = list(shape)
    for d in range(arr.ndim):
        smooth_shape = shapeTemplate[:d] + shapeTemplate[d + 1:]

        # make an array containing the edge value
        edges = N.empty([2] + smooth_shape, N.float32)
        # start side
        slc = copy.copy(sliceTemplate)
        slc[d] = slice(start[d], start[d] + 1, None)
        edges[0] = arr[slc].reshape(smooth_shape)
        # stop side
        slc = copy.copy(sliceTemplate)
        slc[d] = slice(stop[d] - 1, stop[d], None)
        edges[1] = arr[slc].reshape(smooth_shape)

        edges = (edges - value) / float(
            smooth + 1)  # this value can be array??

        # both side
        for s, side in enumerate([start, stop]):
            if s == 0:
                rs = range(smooth_start[d], start[d])
                rs.sort(reverse=True)
            elif s == 1:
                rs = range(stop[d], smooth_stop[d])
            # smoothing
            for f, i in enumerate(rs):
                slc = copy.copy(sliceTemplate)
                slc[d] = slice(i, i + 1, None)
                edgeArr = edges[s].reshape(arr[slc].shape)
                #arr[slc] += edgeArr * (smooth - f)
                arr[slc] = arr[slc] + edgeArr * (smooth - f)  # casting rule

    arr = N.ascontiguousarray(arr)
    return arr
Пример #2
0
def img2polar2D(img,
                center,
                final_radius=None,
                initial_radius=None,
                phase_width=360):
    """
    img: array
    center: coordinate y, x
    final_radius: ending radius
    initial_radius: starting radius
    phase_width: npixles / circle
    """
    if img.ndim > 2 or len(center) > 2:
        raise ValueError, 'this function only support 2D, you entered %i-dim array and %i-dim center coordinate' % (
            img.ndim, len(center))

    if initial_radius is None:
        initial_radius = 0

    if final_radius is None:
        rad0 = N.ceil(N.array(img.shape) - center)
        final_radius = min((min(rad0), min(N.ceil(center))))

    if phase_width is None:
        phase_width = N.sum(img.shape[-2:]) * 2

    theta, R = np.meshgrid(np.linspace(0, 2 * np.pi, phase_width),
                           np.arange(initial_radius, final_radius))

    Ycart, Xcart = polar2cart2D(R, theta, center)

    Ycart = N.where(Ycart >= img.shape[0], img.shape[0] - 1, Ycart)
    Xcart = N.where(Xcart >= img.shape[1], img.shape[1] - 1, Xcart)

    Ycart = Ycart.astype(int)
    Xcart = Xcart.astype(int)

    polar_img = img[Ycart, Xcart]
    polar_img = np.reshape(polar_img,
                           (final_radius - initial_radius, phase_width))

    return polar_img
Пример #3
0
def getSphericalAbbe(arr3D, kmin=2, kmax=60, plot=False):
    """
    compare frequency above and below the focus

    return amplitude var(above)/var(below)
    """
    afz = getFourierZprofile(arr3D)

    # get z profile around the reasonable frequency
    aa = N.abs(N.average(afz[:, kmin:kmax], axis=1))
    # previously it was N.average(N.abs(afz[:,kmin:kmax]), axis=1), but that seems wrong...

    # findMax
    inds = N.indices(aa.shape, dtype=N.float64)
    v, _0, _1, z = U.findMax(aa)
    parm, check = imgFit.fitGaussianND(aa, [z], window=len(aa))
    if check == 5:
        raise RuntimeError, 'Peak not found check=%i' % check

    gg = imgFit.yGaussianND(parm, inds, 3)
    amg = aa - gg

    z0 = parm[2]
    mask = (parm[-1] * 3) / 2.  # sigma * 3 / 2
    ms0 = N.ceil(z0 - mask)
    ms1 = N.ceil(z0 + mask)
    amg[ms0:ms1] = 0

    below = N.var(amg[:ms0])
    above = N.var(amg[ms1:])

    if plot:
        Y.ploty(N.array((aa, gg, amg)))
        print 'below: ', below
        print 'above: ', above
        print ms0, ms1, z0

    return above / (above + below)  #above / below
Пример #4
0
def pointsCutOutND(arr,
                   posList,
                   windowSize=100,
                   sectWise=None,
                   interpolate=True):
    """
    array:       nd array
    posList:     ([(z,)y,x]...)
    windowSize:  scalar (in pixel or as percent < 1.) or ((z,)y,x)
                 if arr.ndim > 2, and len(windowSize) == 2, then
                 cut out section-wise (higher dimensions stay the same)
    sectWise:    conern only XY of windowSize (higher dimensions stay the same)
    interpolate: shift array by subpixel interpolation to adjust center

    return:      list of array centered at each pos in posList
    """
    shape = N.array(arr.shape)
    center = shape / 2.
    # prepare N-dimensional window size
    try:
        len(windowSize)  # seq
        if sectWise:
            windowSize = windowSize[-2:]
        if len(windowSize) != arr.ndim:
            dim = len(windowSize)
            windowSize = tuple(shape[:-dim]) + tuple(windowSize)
    except TypeError:  # scaler
        if windowSize < 1 and windowSize > 0:  # percentage
            w = shape * windowSize
            if sectWise:
                w[:-2] = shape[:-2]
            windowSize = w.astype(N.uint16)
        else:
            windowSize = N.where(shape >= windowSize, windowSize, shape)
            if sectWise:
                windowSize = arr.shape[:-2] + windowSize[-2:]
    windowSize = N.asarray(windowSize)

    # cutout individual position
    arrList = []
    for pos in posList:
        # prepare N-dimensional coordinate
        n = len(pos)
        if n != len(windowSize):
            temp = center.copy()
            center[-n:] = pos
            pos = center

        # calculate idx
        ori = pos - (windowSize / 2.)  # float value
        oidx = N.ceil(ori)  # idx
        subpxl = oidx - ori  # subpixel mod
        if interpolate and N.sometrue(subpxl):  # comit to make shift
            SHIFT = 1
        else:
            SHIFT = 0

        # prepare slice
        # when comitted to make shift, first cut out window+1,
        # then make subpixle shift, and then cutout 1 edge
        slc = [Ellipsis]  # Ellipsis is unnecessary, just in case...
        slc_edge = [slice(1, -1, None)] * arr.ndim
        for d in range(arr.ndim):
            start = oidx[d] - SHIFT
            if start < 0:
                start = 0
                slc_edge[d] = slice(0, slc_edge[d].stop, None)
            stop = oidx[d] + windowSize[d] + SHIFT
            if stop > shape[d]:
                stop = shape[d]
                slc_edge[d] = slice(slc_edge[d].start, shape[d], None)
            slc += [slice(int(start), int(stop), None)]

        # cutout, shift and cutout
        try:
            canvas = arr[slc]
            if SHIFT:
                canvas = U.nd.shift(canvas, subpxl)
                canvas = canvas[slc_edge]
            check = 1
        except IndexError:
            print 'position ', pos, ' was skipped'
            check = 0
            raise
        if check:
            arrList += [N.ascontiguousarray(canvas)]

    return arrList