def test_mat(m): data, color = file2mat('img01.png') mat2display(m*data, color)
from math import sin,cos,pi,atan from mat import Mat from matutil import listlist2mat, coldict2mat, mat2coldict from image_mat_util import file2mat, mat2display #Task 4.15.1 eliezer = file2mat("eliezer.png") eliezer_l = eliezer[0] eliezer_c = eliezer[1] #Task 4.15.2 def identity(): return Mat(({'x','y','u'},{'x', 'y', 'u'}), {('x','x'):1, ('y','y'):1, ('u','u'):1}) #print(eliezer_l == identity() * eliezer_l.copy()) #True #Task 4.15.3 def translation(alpha, beta): return Mat(({'x', 'y', 'u'}, {'x', 'y', 'u'}), {('x', 'x'):1, ('x', 'u'): alpha, ('y', 'y'):1, ('y', 'u'):beta, ('u', 'u'): 1}) #Task 4.15.4 def scale(alpha, beta): return Mat(({'x','y','u'},{'x','y','u'}), {('x','x'): alpha, ('y','y'): beta, ('u','u'): 1}) #Task 4.15.5 def rotation(theta): return Mat(({'x', 'y', 'u'}, {'x', 'y', 'u'}), {('x', 'x'): cos(theta), ('y', 'x'): -sin(theta), ('x', 'y'): cos(theta + 3/2*pi), ('y', 'y'): -sin(theta + 3/2*pi), ('u', 'u'): 1}) #Task 4.15.6 def rotation_about(theta, x, y): return translation(x, y) * rotation(theta) * translation(-x, -y)
def find_error_matrix(S): cols = mat2coldict(S) return coldict2mat({key: find_error(col) for key, col in cols.items()}) print("With error matrix:") print( find_error_matrix( coldict2mat({ 0: list2vec([one, one, one]), 1: list2vec([0, 0, one]) }))) print('%%%%%%%%%%%%%%%%%%%%%%%%%') print("IMAGE TRANSFORMATIONS") file = file2mat('./tt.png') # file = (identity() * file[0], file[1]) # file = (translation(-100, 0) * file[0], file[1]) # file = (scale(1, 2) * file[0], file[1]) # file = (rotation(-0 / 8) * (translation(300, 200) * file[0]), file[1]) # file = (rotation_about(0, 300, 200) * file[0], file[1]) # file = (file[0], scale_colors(1, 10, 1) * file[1]) file = (file[0], grayscale() * file[1]) # mat2display(file[0], file[1]) print('%%%%%%%%%%%%%%%%%%%%%%%%%') print("Different Matrix Opertaions Implementation") A = listlist2mat([[-1, 1, 2], [1, 2, 3], [2, 2, 1]]) v1 = list2vec([1, 2, 0]) print(A * v1)
def file2display(filename="cit.png"): pos, color = file2mat(filename) mat2display(pos,color)
# Apply make_nine_equations to the list of tuples specifying the pixel coordinates of the # whiteboard corners in the image. Assign the resulting list of nine vectors to veclist: veclist = make_nine_equations([(358, 36), (329, 597), (592, 157), (580, 483)]) # Build a Mat whose rows are the Vecs in veclist # print(rowdict2mat(make_equations(358, 36, 0, 0))) L = rowdict2mat(veclist) # print(L) ## 8: () Solve linear system # Now solve the matrix-vector equation to get a Vec hvec, and turn it into a matrix H. hvec = solve(L, b) H = Mat(({'y1', 'y2', 'y3'}, {'x1', 'x2', 'x3'}), hvec.f) (X_pts, colors) = image_mat_util.file2mat('board.png', ('x1', 'x2', 'x3')) Y_pts = H * X_pts ## 9: (Task 5.12.7) Y Board Comprehension def mat_move2board(Y): ''' Input: - Y: a Mat each column of which is a {'y1', 'y2', 'y3'}-Vec giving the whiteboard coordinates of a point q. Output: - a Mat each column of which is the corresponding point in the whiteboard plane (the point of intersection with the whiteboard plane of the line through the origin and q). Example:
def show_col_transform(CT, img): loc, clr = file2mat(img) mat2display(loc, CT * clr)
def show_transform(T, img): loc, clr = file2mat(img) mat2display(T * loc, clr)
from mat import Mat from vec import Vec from geometry_lab import identity, translation, scale, rotation, grayscale, reflect_about from matutil import coldict2mat, mat2coldict from image_mat_util import file2mat, mat2display import math pos_mat, color_mat = file2mat('cit.png') coldict = mat2coldict(pos_mat) #coldict = {colkey : identity * colvec for colkey, colvec in coldict.items()} #coldict = {colkey : translation(200,200) * colvec for colkey, colvec in coldict.items()} #coldict = {colkey : scale(2,4) * colvec for colkey, colvec in coldict.items()} #coldict = {colkey : rotation(math.pi/30) * colvec for colkey, colvec in coldict.items()} #coldict = {colkey : reflect_about((0,0),(1,1)) * colvec for colkey, colvec in coldict.items()} #coldict = {colkey : reflect_about((0,400),(1,400)) * colvec for colkey, colvec in coldict.items()} #coldict = {colkey : reflect_about((0,400),(400,0)) * colvec for colkey, colvec in coldict.items()} pos_mat1 = coldict2mat(coldict) colormat_coldict = {colkey : grayscale()*colvec for colkey, colvec in mat2coldict(color_mat).items()} color_mat1 = coldict2mat(colormat_coldict) #color_mat1=color_mat mat2display(pos_mat1, color_mat1) input("press enter to exit") #1,0 cos30, sin30 #0,1 -sin30, cos30 #0,0 0,0 #t=theta