Exemple #1
0
def test_haar2d():
    """
    Asserts the forwards and inverse wavelet transformations are correct.
    """

    assert haar2d(np.random.random([5,3]),2,debug=True).shape == (8,4), \
        "Transform data must be padded to compatible shape."

    assert haar2d(np.random.random([8,4]),2,debug=True).shape == (8,4), \
        "Transform data must be padded to compatible shape, if required."

    image = np.random.random([5,3])

    haart = haar2d(image, 3)
    haari = ihaar2d(haart, 3)[:image.shape[0], :image.shape[1]]

    assert (image - haari < 0.001).all(), "Transform must be circular."
Exemple #2
0
def test_haar2d():
    """
    Asserts the forwards and inverse wavelet transformations are correct.
    """

    assert haar2d(np.random.random([5,3]),2,debug=True).shape == (8,4), \
        "Transform data must be padded to compatible shape."

    assert haar2d(np.random.random([8,4]),2,debug=True).shape == (8,4), \
        "Transform data must be padded to compatible shape, if required."

    image = np.random.random([5, 3])

    haart = haar2d(image, 3)
    haari = ihaar2d(haart, 3)[:image.shape[0], :image.shape[1]]

    assert (image - haari < 0.001).all(), "Transform must be circular."
Exemple #3
0
    assert haar2d(np.random.random([5,3]),2,debug=True).shape == (8,4), \
        "Transform data must be padded to compatible shape."

    assert haar2d(np.random.random([8,4]),2,debug=True).shape == (8,4), \
        "Transform data must be padded to compatible shape, if required."

    image = np.random.random([5,3])

    haart = haar2d(image, 3)
    haari = ihaar2d(haart, 3)[:image.shape[0], :image.shape[1]]

    assert (image - haari < 0.001).all(), "Transform must be circular."

if __name__ == '__main__':
    #test_haar2d()

    #http://pda.readthedocs.org/en/latest/chp4.html
    # , duli de weidu
    image = np.random.random([8,8]) * 10
    #image = np.asarray([83.000000, 53.000000, 93.000000, 63.000000, 82.000000, 71.000000, 44.000000, 49.000000, 51.000000, 33.000000, 63.000000, 12.000000, 78.000000, 98.000000, 34.000000, 39.000000], dtype=float)

    print image
    haar = haar2d(image)
    #print haari

    ihaar = ihaar2d(haar)
    print ihaar

    assert (image - ihaar < 0.001).all(), "Transform must be circular."

Exemple #4
0
    assert haar2d(np.random.random([5,3]),2,debug=True).shape == (8,4), \
        "Transform data must be padded to compatible shape."

    assert haar2d(np.random.random([8,4]),2,debug=True).shape == (8,4), \
        "Transform data must be padded to compatible shape, if required."

    image = np.random.random([5, 3])

    haart = haar2d(image, 3)
    haari = ihaar2d(haart, 3)[:image.shape[0], :image.shape[1]]

    assert (image - haari < 0.001).all(), "Transform must be circular."


if __name__ == '__main__':
    #test_haar2d()

    #http://pda.readthedocs.org/en/latest/chp4.html
    # , duli de weidu
    image = np.random.random([8, 8]) * 10
    #image = np.asarray([83.000000, 53.000000, 93.000000, 63.000000, 82.000000, 71.000000, 44.000000, 49.000000, 51.000000, 33.000000, 63.000000, 12.000000, 78.000000, 98.000000, 34.000000, 39.000000], dtype=float)

    print image
    haar = haar2d(image)
    #print haari

    ihaar = ihaar2d(haar)
    print ihaar

    assert (image - ihaar < 0.001).all(), "Transform must be circular."
Exemple #5
0
def test_haar2d(img):
    im = Image.open(img)
    #im.show()
    arr = np.asarray(im, dtype='float')

    #plt.imshow(arr, cmap = cm.Greys_r)
    #plt.show()

    arr = arr/255
    #arr = arr[0:5,0:5]

    arr2 = arr.copy()
    row, col = arr.shape[0], arr.shape[1]

    assert (arr - arr2 < 0.0001).all()

    tranform = np.array([[0.299, 0.587, 0.114], [0.596, -0.275, -0.321], [0.212, -0.523, 0.311]])

    #print arr[0,0]
    #print np.dot(arr[0,0], tranform.T)
    #print colorsys.rgb_to_yiq(*arr[0,0])

    arr = np.dot(arr, tranform.T)

    arr_r,arr_g,arr_b = (np.zeros([row, col]), np.zeros([row, col]), np.zeros([row, col]))
    arr3 = arr.copy()
    for i in range(row):
        for j in range(col):
            r,g,b =  colorsys.rgb_to_yiq(*arr2[i,j])
            arr_r[i,j] = r
            arr_g[i,j] = g
            arr_b[i,j] = b
            arr3[i,j] = [r,g,b]

    assert (arr - arr3 < 0.01).all()

    images = np.array([arr[:,:,:1].reshape(row, col), arr[:,:,1:2].reshape(row, col), arr[:,:,2:].reshape(row, col)])
    assert (images[0] - arr_r < 0.01).all()

    colors = images.shape[0]

    haars = [haar2d(images[i]) for i in range(colors)]
    #print haars[0].shape
    #print row, col
    #print haars[0] - images[0]
    assert not (images[0] - haars[0] < 0.1).all()

    haars = [haars[i].reshape(row*col) for i in range(colors)]

    lefts = 41
    inds = [np.argpartition(np.absolute(haars[i]), 0-lefts)[:((row**2)-lefts)] for i in range(colors)]
    print inds[0].shape
    #reversed_inds = [list(set(range(row**2)) - set(inds[i])) for i in range(colors)]

    for i in range(colors):
        haars[i][inds[i]] = np.zeros(inds[i].shape[0])

    haars = [haars[i].reshape([row, col]) for i in range(colors)]

    ihaars = [ihaar2d(haars[i]) for i in range(colors)]

    #assert (images[0] - ihaars[0] < 0.1).all()

    for i in range(row):
        for j in range(col):
            r,g,b =  colorsys.yiq_to_rgb(ihaars[0][i,j], ihaars[1][i,j], ihaars[2][i,j])
            arr3[i,j] = [r,g,b]
    arr3 = arr3*255

    #arr3 = arr3.astype(numpy.int32, copy=False)
    #plt.imshow(arr3, cmap = cm.Greys_r)
    #plt.show()

    img = Image.new('RGB', [row,col])
    pixels = img.load()
    for i in range(row):
        for j in range(col):
            pixels[j,i] = (int(arr3[i,j][0]), int(arr3[i,j][1]), int(arr3[i,j][2]))
    img.show()