Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 33
#---------------------------------------------------------------- # Sample code for UEET2533 Information Theory and Coding # by Chong Zan Kai # Email [email protected] # Universiti Tunku Abdul Rahman, Malaysia. # # We demonstrate how an image look like after passing a BSC channel. # # 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) # # Introduce noise to the message in BSC channel. # def bsc_channel(msg_mat, p_flip): new_msg_mat = copy (msg_mat) total_row, total_col = new_msg_mat.dimensions() for r in range(total_row): for c in range(total_col): rand_val = random() if rand_val <= p_flip: new_msg_mat[r, c] += 1 return new_msg_mat #---------------------------------------------------------------- #---------------------------------------------------------------- # Main #---------------------------------------------------------------- #---------------------------------------------------------------- # Main #---------------------------------------------------------------- p_flip = 0.1 # Noise probability sample_file = 'sample01.bmp' # # Present the orignial image # # Read the image file as array img_arr = mpimg.imread(sample_file) # Display the image print 'Original image:' plt.imshow(img_arr) print # Convert image array to GF2 matrix img_mat = img_arr_to_matrix (img_arr) print # # Introduce noise to the image # recv_img_mat = bsc_channel(img_mat, p_flip) # # Present the received image # # Convert to arr first recv_img_arr = img_matrix_to_arr (recv_img_mat) # Display image print 'Received image after BSC channel:' plt.imshow(recv_img_arr)
Original image:
Received image after BSC channel: