def image2mat(img, row_labels=('x', 'y', 'u')): """ input: an image in list-of-lists format output: a pair (points, colors) of matrices representing the image. Note: The input list-of-lists can consist of either integers n [0, 255] for grayscale or 3-tuple of integers representing the rgb color coordinates """ h = len(img) w = len(img[0]) rx, ry, ru = row_labels ptsD = (set(row_labels), {(x, y) for x in range(w + 1) for y in range(h + 1)}) ptsF = {} colorsD = ({'r', 'g', 'b'}, {(x, y) for x in range(w) for y in range(h)}) colorsF = {} for y in range(h + 1): for x in range(w + 1): pt = (x, y) ptsF[(rx, pt)] = x ptsF[(ry, pt)] = y ptsF[(ru, pt)] = 1 if x < w and y < h: col = img[y][x] if type(col) is int: red, green, blue = col, col, col else: red, green, blue = col colorsF[('r', pt)] = red colorsF[('g', pt)] = green colorsF[('b', pt)] = blue return mat.Mat(ptsD, ptsF), mat.Mat(colorsD, colorsF)
def noise(A, freq): """ return a random noise matrix with the same domain as A. The probability for 1 in any entry of the matrix is freq. """ f = {(i, j): one for i in A.D[0] for j in A.D[1] if random.random() < freq} return mat.Mat(A.D, f)
def test_uint8(self): m = mat.Mat(3, 2, cv2.CV_8UC3) a = np.asarray(m) assert len(a.shape) == 3 assert a.shape[0] == 3 assert a.shape[1] == 2 assert a.shape[2] == 3 assert a.dtype == np.uint8
def bits2mat(bits,nrows=4,trans=False): """ Convert a list of bits into a matrix with nrows rows. The matrix is populated by bits column by column Keyword arguments: nrows -- number of rows in the matrix (default 4) trans -- whether to reverse rows and columns of the matrix (default False) """ ncols = len(bits)//nrows f = {(i,j):one for j in range(ncols) for i in range(nrows) if bits[nrows*j+i]} A = mat.Mat((set(range(nrows)), set(range(ncols))), f) if trans: A = mat.transpose(A) return A
def noise(A, freq): """ return a random noise matrix with the same domain as A. The probability for 1 in any entry of the matrix is freq. """ f = {(i, j): one for i in A.D[0] for j in A.D[1] if random.random() < freq} return mat.Mat(A.D, f) # s = "I'm trying to free your mind, Neo. But I can only show you the door. You're the one that has to walk through it" # print s # L = bits2mat(str2bits(s)) # print bits2str(mat2bits(L)) # E = noise(L, 0.02) # print bits2str(mat2bits(E)) # L = [[one,0, one,one],[one, one, 0,one],[0,0,0,one],[one, one,one,0],[0,0,one,0],[0,one,0,0],[one,0,0,0]] # G = listlist2mat(L) # print G
if __name__ == '__main__': a = np.random.randint(10, 20, (1000)) b = np.random.randint(10, 20, (1000)) c = vec.Vec(a.tolist()) d = vec.Vec(b.tolist()) add_numpy(a, b) add_vec(c, d) M = np.random.randint(10, 20, (30, 10)) N = np.random.randint(10, 20, (10, 30)) u = np.random.randint(10, 20, (30)) v = np.random.randint(10, 20, (10)) mult_numpy(M, v) mult_mat(mat.Mat(M.tolist()), vec.Vec(v.tolist())) mult_numpy(u, M) mult_mat(vec.Vec(u.tolist()), mat.Mat(M.tolist())) mult_numpy(M, N) mult_mat(mat.Mat(M.tolist()), mat.Mat(N.tolist())) print("\n" + "=" * 20, "Sparse matrices", "=" * 20) M = sps.random(30, 20, density=0.01, format='csc') N = sps.random(20, 30, density=0.01, format='csc') M_s = sps2sparseMat(M) N_s = sps2sparseMat(N) M_m = mat.Mat(M.toarray().tolist()) N_m = mat.Mat(N.toarray().tolist())