Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 42
#---------------------------------------------------------------- # Sample code for UEET2533 Information Theory and Coding # by Chong Zan Kai # Email [email protected] # Universiti Tunku Abdul Rahman, Malaysia. # # We will demonstrate how to contrast an image file. # # Last modified on 9-Jan-2015. #---------------------------------------------------------------- #---------------------------------------------------------------- # Functions to convert image to matrix and vice versa. #---------------------------------------------------------------- import matplotlib.pyplot as plt import matplotlib.image as mpimg import numpy as np # # Convert image 2darray to GF2 matrix. # def img_arr_to_matrix(img_2darr): #img_2darr = mpimg.imread(bmp_file) img_2dlist = [] # # Convert to 0 and 1 first. for p4_arr in img_2darr: a_list = [int(p[0]>0) for p in p4_arr] img_2dlist.append(a_list) # img_mat = matrix(GF(2), img_2dlist) return img_mat # # Convert GF2 matrix back to image 2darr. # def img_matrix_to_arr(img_mat): img_2darr = [] for row in img_mat.rows(): pixel_list = [ np.uint8([int(r) * 255, int(r) * 255, int(r) * 255, 255 ]) for r in row] img_2darr.append(pixel_list) return np.array(img_2darr) #---------------------------------------------------------------- #---------------------------------------------------------------- # Main #---------------------------------------------------------------- sample_file = 'sample01.bmp' # Read the image file as array img_arr = mpimg.imread(sample_file) # # Display the image # print 'Original image:' plt.imshow(img_arr) #plt.show() # Convert image array to GF2 matrix img_mat = img_arr_to_matrix (img_arr) print # # Manipulate the matrix. # Basically I just inverse the image # total_row, total_col = img_mat.dimensions() for r in range(total_row): for c in range(total_col): img_mat[r, c] = img_mat[r, c] + 1 # GF2's plus 1 == XOR. # # Convert the GF2 matrix to image array again # new_img_arr = img_matrix_to_arr (img_mat) print 'New image:' plt.imshow(new_img_arr)
Original image:
New image: