def index_image(id, img): im = Image.open(img) arr = np.asarray(im, dtype='float') row, col = arr.shape[0], arr.shape[1] tranform = np.array([[0.299, 0.587, 0.114], [0.596, -0.275, -0.321], [0.212, -0.523, 0.311]]) arr = np.dot(arr, tranform.T) images = [arr[:,:,:1].reshape(row, col), arr[:,:,1:2].reshape(row, col), arr[:,:,2:].reshape(row, col)] avgl = [] haars = [] colors = 3 num_coefs = 40 imgbuckets = dbspace["buckets"] for i in range(colors): haar = haar2d(images[i]).reshape(row*col) avgl.append(haar[0]/(256*128)) haar[0] = 0.0 ind = np.argpartition(np.absolute(haar), 0-num_coefs)[-num_coefs:] haar = haar[ind] small = (haar <= 0) big = (haar > 0) origin_small = ind[small] origin_big = ind[big] for j in origin_small: imgbuckets[i][0][j].append(id) for j in origin_big: imgbuckets[i][1][j].append(id) dbspace["sigs"][id] = avgl
def test_haar2d2(img): im = Image.open(img) arr = np.asarray(im, dtype='float') row, col = arr.shape[0], arr.shape[1] tranform = np.array([[0.299, 0.587, 0.114], [0.596, -0.275, -0.321], [0.212, -0.523, 0.311]]) arr = np.dot(arr, tranform.T) images = [arr[:,:,:1].reshape(row, col), arr[:,:,1:2].reshape(row, col), arr[:,:,2:].reshape(row, col)] colors = 3 haars = [haar2d(images[i]).reshape(row*col) for i in range(colors)] avgl = [0.0, 0.0, 0.0] avgl = [haars[i][0]/(256*128) for i in range(colors)] for i in range(colors): haars[i][0] = 0.0 print 'avgl', avgl lefts = 40 inds = [np.argpartition(np.absolute(haars[i]), 0-lefts)[-lefts:] for i in range(colors)] #lefts haars = [haars[i][inds[i]] for i in range(colors)] #value in lefts big_i = [(haars[i] > 0) for i in range(colors)] small_i = [(haars[i] < 0) for i in range(colors)] for i in range(colors): print inds[i][big_i[i]] print inds[i][small_i[i]]
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."
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."
def query_img(img): imgbuckets = dbspace["buckets"] sigs = dbspace["sigs"] #TODO do in a function im2 = Image.open(img) im = im2.resize((128, 128)) arr = np.asarray(im, dtype='float') row, col = arr.shape[0], arr.shape[1] tranform = np.array([[0.299, 0.587, 0.114], [0.596, -0.275, -0.321], [0.212, -0.523, 0.311]]) arr = np.dot(arr, tranform.T) images = [ arr[:, :, :1].reshape(row, col), arr[:, :, 1:2].reshape(row, col), arr[:, :, 2:].reshape(row, col) ] avgl = [] haars = [] colors = 3 num_coefs = 40 img_sig = [[0] * 2 for i in range(colors)] for i in range(colors): haar = haar2d(images[i]).reshape(row * col) avgl.append(haar[0] / (256 * 128)) haar[0] = 0.0 ind = np.argpartition(np.absolute(haar), 0 - num_coefs)[-num_coefs:] haar = haar[ind] small = (haar <= 0) big = (haar > 0) img_sig[i][0] = ind[small] img_sig[i][1] = ind[big] scores = {} for id, sig in sigs.items(): scores[id] = 0.0 for c in range(colors): scores[id] = scores[id] + (weights[0][c] * math.fabs(sig[c] - avgl[c])) for pn in range(2): for c in range(colors): for idx in img_sig[c][pn]: for uit in imgbuckets[c][pn][idx]: scores[uit] = scores[uit] - weights[img_bin[idx]][c] scores = sorted(scores.items(), key=lambda item: -item[1]) results = [] for k, v in scores: results.append(k) results.append(v) return normalizeResults(results)
def query_img(img): imgbuckets = dbspace["buckets"] sigs = dbspace["sigs"] #TODO do in a function im2 = Image.open(img) im = im2.resize((128,128)) arr = np.asarray(im, dtype='float') row, col = arr.shape[0], arr.shape[1] tranform = np.array([[0.299, 0.587, 0.114], [0.596, -0.275, -0.321], [0.212, -0.523, 0.311]]) arr = np.dot(arr, tranform.T) images = [arr[:,:,:1].reshape(row, col), arr[:,:,1:2].reshape(row, col), arr[:,:,2:].reshape(row, col)] avgl = [] haars = [] colors = 3 num_coefs = 40 img_sig = [[0]*2 for i in range(colors)] for i in range(colors): haar = haar2d(images[i]).reshape(row*col) avgl.append(haar[0]/(256*128)) haar[0] = 0.0 ind = np.argpartition(np.absolute(haar), 0-num_coefs)[-num_coefs:] haar = haar[ind] small = (haar <= 0) big = (haar > 0) img_sig[i][0] = ind[small] img_sig[i][1] = ind[big] scores = {} for id,sig in sigs.items(): scores[id] = 0.0 for c in range(colors): scores[id] = scores[id] + (weights[0][c] * math.fabs(sig[c] - avgl[c])) for pn in range(2): for c in range(colors): for idx in img_sig[c][pn]: for uit in imgbuckets[c][pn][idx]: scores[uit] = scores[uit] - weights[img_bin[idx]][c] scores = sorted(scores.items(), key = lambda item: -item[1]) results = [] for k,v in scores: results.append(k) results.append(v) return normalizeResults(results)
def index_image(id, img_file): img = Image.open(img_file) size = (128, 128) im = img.resize(size) arr = np.asarray(im, dtype='float') row, col = arr.shape[0], arr.shape[1] tranform = np.array([[0.299, 0.587, 0.114], [0.596, -0.275, -0.321], [0.212, -0.523, 0.311]]) arr = np.dot(arr, tranform.T) images = [ arr[:, :, :1].reshape(row, col), arr[:, :, 1:2].reshape(row, col), arr[:, :, 2:].reshape(row, col) ] avgl = [] haars = [] colors = 3 num_coefs = 40 imgbuckets = dbspace["buckets"] for i in range(colors): haar = haar2d(images[i]).reshape(row * col) avgl.append(haar[0] / (256 * 128)) haar[0] = 0.0 ind = np.argpartition(np.absolute(haar), 0 - num_coefs)[-num_coefs:] haar = haar[ind] small = (haar <= 0) big = (haar > 0) origin_small = ind[small] origin_big = ind[big] #print "i is :", i, "neg is:" #print origin_small #print "pos is :" #print origin_big for j in origin_small: imgbuckets[i][0][j].append(id) for j in origin_big: imgbuckets[i][1][j].append(id) print avgl dbspace["sigs"][id] = avgl
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."
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."
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()