SHARE
TWEET

Untitled

a guest Jan 21st, 2020 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy as np
  2. import matplotlib.image as mpimg
  3. from random import randint
  4. import matplotlib.pyplot as plt
  5. import cv2
  6.  
  7.  
  8. original_img = mpimg.imread("/Users/06fioritom/Desktop/image5.jpg")
  9.  
  10. # original_img = original_img[:,:,:3]
  11.  
  12. # generates the list of random coordinate points
  13. coordinate_list = []
  14. training_dataset_size = 100
  15. img_height, img_width, img_colour = original_img.shape
  16. i = 0
  17. for i in range(training_dataset_size):
  18.     random_x = randint(4, img_width-6)
  19.     random_y = randint(4, img_height-6)
  20.     random_coordinate = (random_x, random_y)
  21.     coordinate_list.append(random_coordinate)
  22.  
  23. print("Co. list: ", coordinate_list)
  24.  
  25. # creates the noisy image
  26.  
  27.  
  28. # original_img = cv2.imread("/Users/06fioritom/Desktop/image5.jpg")
  29. # original_img = cv2.resize(original_img, (224, 224), interpolation=cv2.INTER_AREA)
  30.  
  31. mean = 0
  32. var = 4000
  33. sigma = var ** 0.5
  34. gaussian = np.random.normal(mean, sigma, (img_height, img_width))  # np.zeros((224, 224), np.float32)
  35.  
  36. noisy_image = np.zeros(original_img.shape, np.float32)
  37.  
  38. if len(original_img.shape) == 2:
  39.     noisy_image = original_img + gaussian
  40. else:
  41.     noisy_image[:, :, 0] = original_img[:, :, 0] + gaussian
  42.     noisy_image[:, :, 1] = original_img[:, :, 1] + gaussian
  43.     noisy_image[:, :, 2] = original_img[:, :, 2] + gaussian
  44.  
  45. cv2.normalize(noisy_image, noisy_image, 0, 255, cv2.NORM_MINMAX, dtype=-1)
  46. noisy_image = noisy_image.astype(np.uint8)
  47.  
  48.  
  49. print("Noisy image shape: ", noisy_image.shape)
  50. print("Original image shape: ", original_img.shape)
  51.  
  52. # cv2.imshow("img", original_img)
  53. # cv2.imshow("gaussian", gaussian)
  54. # cv2.imshow("noisy", noisy_image)
  55. # print(noisy_image)
  56.  
  57. cropped_original_compilation = []
  58. # cropped_original_compilation = np.array(cropped_original_compilation)
  59. cropped_noisy_compilation = []
  60. # cropped_noisy_compilation = np.array(cropped_noisy_compilation)
  61. similarity_rating_compilation = []
  62. # similarity_rating_compilation = np.array(similarity_rating_compilation)
  63.  
  64.  
  65. shifted_coordinate_list = []
  66.  
  67.  
  68. print(type(original_img))
  69. print(type(noisy_image))
  70. print(type(cropped_original_compilation))
  71. print(type(cropped_noisy_compilation))
  72. print(type(similarity_rating_compilation))
  73.  
  74. i = 0
  75. for i in range(training_dataset_size):
  76.  
  77.     # defines the area to crop for original image
  78.     x, y = coordinate_list[i][0], coordinate_list[i][1]
  79.     # print("x: ", x, "y: ", y)
  80.  
  81.     crop_range_y_lower = y + 9
  82.     crop_range_y_upper = y - 9
  83.     crop_range_x_lower = x + 9
  84.     crop_range_x_upper = x - 9
  85.  
  86.     cropped_original = original_img[crop_range_y_upper:crop_range_y_lower, crop_range_x_upper:crop_range_x_lower, :]
  87.     cropped_original_compilation.append(cropped_original)  # adds cropped region to list
  88.  
  89.  
  90.     # defines the area to crop for noisy image
  91.     noise_crop_y_shift = randint(-4, 4)  # gives random y shift of +/- 4 pixels
  92.     noise_crop_x_shift = randint(-8, 8)  # gives random x shift of +/- 8 pixels
  93.     xn, yn = x + noise_crop_x_shift, y + noise_crop_y_shift  # recenters the 18*18 selection based on y and y shift
  94.     crop_range_y_lower = yn + 9
  95.     crop_range_y_upper = yn - 9
  96.     crop_range_x_lower = xn + 9
  97.     crop_range_x_upper = xn - 9
  98.     cropped_noisy = noisy_image[crop_range_y_upper:crop_range_y_lower, crop_range_x_upper:crop_range_x_lower, :]  # crops the noisy image
  99.     cropped_noisy_compilation.append(cropped_noisy)
  100.  
  101.     # give a similarity rating based on x and y shift
  102.     shift_x = abs(noise_crop_x_shift)
  103.     shift_y = abs(noise_crop_y_shift)
  104.     similarity_rating = 324 - (shift_y * 18) - ((18-shift_y) * shift_x)
  105.     similarity_rating = similarity_rating/324
  106.     # print("Similarity rating: ", similarity_rating)
  107.     similarity_rating_compilation.append(similarity_rating)
  108.  
  109.     # print(i)
  110.  
  111.     i = i + 1
  112.  
  113.  
  114. cropped_noisy_compilation = np.array(cropped_noisy_compilation)
  115. # cropped_noisy_compilation = np.moveaxis(cropped_noisy_compilation, 3, 1)
  116. similarity_rating_compilation = np.array(similarity_rating_compilation)
  117.  
  118.  
  119. # # if uncommented, will plot the location of the randomly chosen pixel locations
  120. # for i in range(training_dataset_size):
  121. #     plt.plot(coordinate_list[i][0],coordinate_list[i][1], 'o')
  122. # plt.show()
  123.  
  124. cropped_original_compilation = np.array(cropped_original_compilation)
  125. # cropped_original_compilation = np.moveaxis(cropped_original_compilation, 3, 1)
  126.  
  127.  
  128. print("Cropped original array shape:  ", cropped_original_compilation.shape)
  129. print("Cropped noisy array shape:     ", cropped_noisy_compilation.shape)
  130. print("Similarity rating array shape: ", similarity_rating_compilation.shape)
  131.  
  132. np.save("/Users/06fioritom/Desktop/cropped_original_compilation_1", cropped_original_compilation)
  133. np.save("/Users/06fioritom/Desktop/cropped_noisy_compilation_1", cropped_noisy_compilation)
  134. np.save("/Users/06fioritom/Desktop/similarity_rating_compilation_1", similarity_rating_compilation)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top