示例#1
0
def test_shift_ratio():
    f = np.zeros((128,128))
    f[32:64,32:64] = 128
    for s in [0,1,2,3]:
        output = interpolate.shift(f,(s,s))
        ratio = output.sum()/f.sum()
        assert np.abs(ratio - 1.) < .01
示例#2
0
def lbp_transform_local(image, radius, points, ignore_zeros=False, preserve_shape=True):

    if ignore_zeros and preserve_shape:
        raise ValueError('mahotas.features.lbp_transform: *ignore_zeros* and *preserve_shape* cannot both be used together')

    image = np.asanyarray(image, dtype=np.float64)
    if image.ndim != 2:
        raise ValueError('mahotas.features.lbp_transform: This function is only defined for two dimensional images')

    if ignore_zeros:
        Y,X = np.nonzero(image)
        def select(im):
            return im[Y,X].ravel()
    else:
        select = np.ravel

    pixels = select(image)
    angles = np.linspace(0, 2*np.pi, points+1)[:-1]
    data = []
    for dy,dx in zip(np.sin(angles), np.cos(angles)):
        data.append(
            select(shift(image, [radius*dy,radius*dx], order=1)))
    data = np.array(data)
    codes = (data > pixels).astype(np.int32)
    codes *= (2**np.arange(points)[:,np.newaxis])
    codes = codes.sum(0)
    codes = _lbp.map(codes.astype(np.uint32), points)
    if preserve_shape:
        codes = codes.reshape(image.shape)
    return codes
示例#3
0
def test_shift_uint8():
    im = np.arange(256).reshape((16,-1))
    im = im.astype(np.uint8)
    interpolate.shift(im, [0, np.pi/2], order=1)
示例#4
0
def test_short_shift():
    im = np.arange(256).reshape((16,4,-1))
    interpolate.shift(im, [1,0])
示例#5
0
def test_short_shift():
    im = np.arange(256).reshape((16, 4, -1))
    with raises(ValueError):
        interpolate.shift(im, [1, 0])
示例#6
0
文件: C2.py 项目: pchatzou/emprost
def lddp(image,
         radius1,
         radius2,
         points,
         ignore_zeros=False,
         preserve_shape=True):
    """
    Custom implementation of 2nd order local directional derivative pattern
    Originally obtained from mahotas.features.lbp_transform function.

    An inner and an outer radius with respect to a point, which is each image pixel are selected.
    Then, a set of points are obtained by interpolation on these radii, according to the number defined by *points*
    argument. Note that if, for example, 8 points are given, there are 8 points that are considered on the inner radius
    defined by equal angles starting from the central point and each one of them. If these two points (the origin, or the
    centre) and each point define a straight line, also 8 points on the same lines are considered for the outer radius.

    For reference see :

    Guo, Zhenhua, et al. "Local directional derivative pattern for rotation invariant texture classification."
    Neural Computing and Applications 21.8 (2012): 1893-1904.

    :param np.array image: numpy array input image
    :param int radius1: inner radius (in pixels)
    :param int radius2: outer radius (in pixels)
    :param int points: number of points to consider. It should be given regarding the inner radius, as the second set of points will be aligned to the ones lying in the inner circle.

    :return: lddp histogram
    """
    from mahotas import interpolate
    from mahotas.features import _lbp
    from mahotas import histogram

    if ignore_zeros and preserve_shape:
        raise ValueError(
            'mahotas.features.lbp_transform: *ignore_zeros* and *preserve_shape* cannot both be used together'
        )

    image = np.asanyarray(image, dtype=np.float64)
    if image.ndim != 2:
        raise ValueError(
            'mahotas.features.lbp_transform: This function is only defined for two dimensional images'
        )

    if ignore_zeros:
        Y, X = np.nonzero(image)

        def select(im):
            return im[Y, X].ravel()
    else:
        select = np.ravel

    pixels = select(image)
    angles = np.linspace(0, 2 * np.pi, points + 1)[:-1]
    data1 = []
    for dy, dx in zip(np.sin(angles), np.cos(angles)):
        data1.append(
            select(
                interpolate.shift(image, [radius1 * dy, radius1 * dx],
                                  order=1)))
    data1 = np.array(data1)

    data2 = []
    for dy, dx in zip(np.sin(angles), np.cos(angles)):
        data2.append(
            select(
                interpolate.shift(image, [radius2 * dy, radius2 * dx],
                                  order=1)))
    data2 = np.array(data2)
    data = np.array(data2 + pixels - 2 * data1)
    codes = (data >= 0).astype(np.int32)
    codes *= (2**np.arange(points)[:, np.newaxis])
    codes = codes.sum(0)

    codes = _lbp.map(codes.astype(np.uint32), points)
    if preserve_shape:
        codes = codes.reshape(image.shape)

    final = histogram.fullhistogram(codes.astype(np.uint32))

    codes = np.arange(2**points, dtype=np.uint32)
    iters = codes.copy()
    codes = _lbp.map(codes.astype(np.uint32), points)
    pivots = (codes == iters)
    npivots = np.sum(pivots)
    compressed = final[pivots[:len(final)]]
    compressed = np.append(compressed, np.zeros(npivots - len(compressed)))
    return compressed