Exemplo n.º 1
0
def centerOfMass(img, yx, window=5):
    """
    find peak by center of mass in a 2D image

    img:    a 2D image array
    yx:     (y,x) in the image
    window: a window where CM calculation is performed on

    return yx
    """
    # prepare small image
    s = N.array([window,window])
    c = s/2.
    yx = N.round_(yx)
    yx -= c
    yi, xi = N.indices(s)
    yi += yx[0]
    xi += yx[1]
    cc = img[yi,xi]

    # calculate center of mass
    yxi = N.indices(s)
    yxi *= cc
    yxi = yxi.T
    vv = N.sum(yxi, axis=0)
    vv = N.sum(vv, axis=0)
    yxs = vv / float(N.sum(cc))
    yxs += yx
    return yxs
Exemplo n.º 2
0
def centerOfMass(img, yx, window=5):
    """
    find peak by center of mass in a 2D image

    img:    a 2D image array
    yx:     (y,x) in the image
    window: a window where CM calculation is performed on

    return yx
    """
    # prepare small image
    s = N.array([window, window])
    c = s / 2.
    yx = N.round_(yx)
    yx -= c
    yi, xi = N.indices(s)
    yi += yx[0]
    xi += yx[1]
    cc = img[yi, xi]

    # calculate center of mass
    yxi = N.indices(s)
    yxi *= cc
    yxi = yxi.T
    vv = N.sum(yxi, axis=0)
    vv = N.sum(vv, axis=0)
    yxs = vv / float(N.sum(cc))
    yxs += yx
    return yxs
Exemplo n.º 3
0
def paddingValue(img, shape, value=0, shift=None, smooth=0, interpolate=True):
    """
    shape:       in the same dimension as img
    value:       value in padded region, can be scaler or array with the shape
    shift:       scaler or in the same dimension as img and shape (default 0)
    smooth:      scaler value to smoothen border (here value must be scaler)
    interpolate: shift array by subpixel interpolation to adjust center

    return:      padded array with shape
    """
    # create buffer
    dtype = img.dtype.type
    canvas = N.empty(shape, dtype)
    canvas[:] = value

    # calculate position
    shape = N.array(shape)
    shapeS = img.shape
    center = N.divide(shape, 2)
    if shift is None:
        shift = 0#[0] * len(shapeS)
    shapeL = shape#N.add(shapeS, center+shift)
    #start, stop = (shapeL - shapeS)/2., (shapeL + shapeS)/2.
    start = N.round_((shapeL - shapeS)/2.).astype(N.int)
    stop = shapeS + start
    slc = [slice(start[d], stop[d], None) for d in range(img.ndim)]

    #slc = [slice(int(round(start[d])), int(round(stop[d])), None) for d in range(img.ndim)]
    #print slc, shapeS, shapeL

    # shift if necessary
    if interpolate:
        subpx_shift = start % 1 # should be 0.5 or 0
        if N.sometrue(subpx_shift):
            img = U.nd.shift(img, subpx_shift)
    # padding
    canvas[slc] = img
    if smooth:
        canvas = _smoothBorder(canvas, start, stop, smooth, value)
    canvas = N.ascontiguousarray(canvas)
    #print shapeS, shapeL, slc
    return canvas
Exemplo n.º 4
0
def paddingValue(img, shape, value=0, shift=None, smooth=0, interpolate=True):
    """
    shape:       in the same dimension as img
    value:       value in padded region, can be scaler or array with the shape
    shift:       scaler or in the same dimension as img and shape (default 0)
    smooth:      scaler value to smoothen border (here value must be scaler)
    interpolate: shift array by subpixel interpolation to adjust center

    return:      padded array with shape
    """
    # create buffer
    dtype = img.dtype.type
    canvas = N.empty(shape, dtype)
    canvas[:] = value

    # calculate position
    shape = N.array(shape)
    shapeS = img.shape
    center = N.divide(shape, 2)
    if shift is None:
        shift = 0  #[0] * len(shapeS)
    shapeL = shape  #N.add(shapeS, center+shift)
    #start, stop = (shapeL - shapeS)/2., (shapeL + shapeS)/2.
    start = N.round_((shapeL - shapeS) / 2.).astype(N.int)
    stop = shapeS + start
    slc = [slice(start[d], stop[d], None) for d in range(img.ndim)]

    #slc = [slice(int(round(start[d])), int(round(stop[d])), None) for d in range(img.ndim)]
    #print slc, shapeS, shapeL

    # shift if necessary
    if interpolate:
        subpx_shift = start % 1  # should be 0.5 or 0
        if N.sometrue(subpx_shift):
            img = U.nd.shift(img, subpx_shift)
    # padding
    canvas[slc] = img
    if smooth:
        canvas = _smoothBorder(canvas, start, stop, smooth, value)
    canvas = N.ascontiguousarray(canvas)
    #print shapeS, shapeL, slc
    return canvas
Exemplo n.º 5
0
def pointsCutOut3D(arr, posList, windowSize=100, d2=None, interpolate=True, removeWrongShape=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)
    d2:          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.

    if arr.ndim <= 2:
        ndim_arr = arr.ndim
    else:
        ndim_arr = 3

    # prepare N-dimensional window size
    try:
        len(windowSize) # seq
        if d2:
            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 d2:
                w[:-2] = shape[:-2]
            elif ndim_arr > 3:
                w[:-3] = shape[:-3]
            windowSize = w.astype(N.uint)
        else:
            windowSize = N.where(shape >= windowSize, windowSize, shape)
            if d2:
                windowSize[:-2] = shape[:-2]
    windowSize = N.asarray(windowSize)
    halfWin = windowSize / 2
    #ndim_win = len(windowSize)

    margin = int(interpolate)
    # cutout individual position
    arrList = []
    for pos in posList:
        ndim_pos = len(pos)

        # calculate idx
        dif = pos +0.5 - halfWin[-ndim_pos:]

        dif = N.round_(dif)
        dif = dif.astype(N.int)
        starts = dif - margin
        starts = N.where(starts < 0, 0, starts)
        stops = dif + windowSize[-ndim_pos:] + margin
        stops = N.where(stops > shape[-ndim_pos:], shape[-ndim_pos:], stops)

        #if removeWrongShape and (N.any((starts) < 0) or N.any((stops) > shape[-ndim_pos:])):
        #    continue
        
        slc = [Ellipsis]
        for dim, s0 in enumerate(starts):
            s1 = stops[dim]
            slc.append(slice(s0, s1))
        cpa = arr[slc]
        
        if interpolate:
            dif = pos + 0.5 - halfWin[-ndim_pos:]
            sub = (arr.ndim - len(dif)) * [0] + list(dif)
            sub = N.array(sub)
            sar = U.nd.shift(cpa, sub % 1)

            slc = [Ellipsis]
            for d in range(ndim_arr):
                slc.append(slice(margin, -margin))
            cpa = sar[slc]

        arrList.append(cpa)


    if removeWrongShape:
        if d2 and arr.ndim == 2:
            arrList = N.array([a for a in arrList if N.all(a.shape[-2:] == windowSize[-2:])])
        else:
            arrList = N.array([a for a in arrList if N.all(a.shape[-3:] == windowSize[-3:])])
    return arrList