Advertisement
Guest User

Genetic

a guest
Apr 19th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.06 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. import random
  4. from operator import itemgetter
  5. import time
  6.  
  7.  
  8. def get_fitness_list(population, target):
  9.     answer = []
  10.     for img in population:
  11.         fitness_ = fitness(img, target)
  12.         answer.append(fitness_)
  13.     return answer
  14.  
  15.  
  16. def fitness(image, target):
  17.     ans = 0
  18.     for i in range(0, len(image)):
  19.         for j in range(0, len(image[0])):
  20.             ans += abs(int(image[i, j, 0]) - int(target[i, j, 0]))
  21.             ans += abs(int(image[i, j, 1]) - int(target[i, j, 1]))
  22.             ans += abs(int(image[i, j, 2]) - int(target[i, j, 2]))
  23.     return ans
  24.  
  25.  
  26. def change_matrix_in_random(image, max_change):
  27.     copy = image.copy()
  28.  
  29.     for i in range(0, len(copy)):
  30.         for j in range(0, len(copy[0])):
  31.             if (np.random.uniform(0,1) > 0.5): #rate
  32.                 continue
  33.             a = np.random.randint(-max_change, max_change)
  34.             b = np.random.randint(-max_change, max_change)
  35.             c = np.random.randint(-max_change, max_change)
  36.             copy[i, j, 0] = max(min(copy[i, j, 0] + a, 255), 0)
  37.             copy[i, j, 1] = max(min(copy[i, j, 1] + b, 255), 0)
  38.             copy[i, j, 2] = max(min(copy[i, j, 2] + c, 255), 0)
  39.     return copy
  40.  
  41.  
  42. def get_rand_img(side_y, side_x):
  43.     img = np.zeros((side_y, side_x, 3), np.uint8)
  44.     for i in range(0, side_y):
  45.         for j in range(0, side_x):
  46.             img[i, j, 0] = random.randint(0, 255)
  47.             img[i, j, 1] = random.randint(0, 255)
  48.             img[i, j, 2] = random.randint(0, 255)
  49.     return img
  50.  
  51.  
  52. def select_best(population, target):
  53.     fitness_list = get_fitness_list(population, target)
  54.     arr_sort = []
  55.     for i in range(GPS):
  56.         arr_sort.append([fitness_list[i], population[i]])
  57.     arr_sort = sorted(arr_sort, key=itemgetter(0))
  58.  
  59.  
  60.     answer = []
  61.     for i in range (PS):
  62.         answer.append(arr_sort[i][1])
  63.     return answer
  64.  
  65.  
  66. def grow(population):
  67.     answer = []
  68.     for i in range (len(population)):
  69.         for j in range(i+1, len(population)):
  70.             child = crossingover(population[i], population[j])
  71.             changed_child = change_matrix_in_random(child, 40)
  72.             answer.append(changed_child)
  73.     return answer
  74.  
  75. def crossingover(image1, image2):
  76.     answer = image1.copy()
  77.     for i in range(0, len(image1)):
  78.         for j in range(0, len(image1[0])):
  79.             change = np.random.randint(0,2)
  80.             if change > 0:
  81.                 answer[i, j, 0] = image2[i, j, 0]
  82.                 answer[i, j, 1] = image2[i, j, 1]
  83.                 answer[i, j, 2] = image2[i, j, 2]
  84.     return answer
  85.  
  86.  
  87. def solve(image, target):
  88.     copy = image.copy()
  89.  
  90.     population = []
  91.     for i in range(PS):
  92.         population.append(change_matrix_in_random(copy, 45))
  93.  
  94.     for iteration in range(8):
  95.         population = grow(population)
  96.         population = select_best(population, target)
  97.  
  98.     return population[0]
  99.  
  100.  
  101. def main2(target):
  102.     answer = get_rand_img(512, 512)
  103.  
  104.     for i in range(0, 512, 2):
  105.         print(i)
  106.         for j in range(0, 512, 2):
  107.             print(1000 + j)
  108.             solved = solve(answer[i:i+2], target[i:i+2])
  109.             answer[i, j] = solved[0, 0]
  110.             answer[i, j+1] = solved[0, 1]
  111.             answer[i+1, j] = solved[1, 0]
  112.             answer[i+1, j+1] = solved[1, 1]
  113.  
  114.     return answer
  115.  
  116. def main1():
  117.     filepath = "image1.png"
  118.     target = cv2.imread(filepath, cv2.IMREAD_COLOR)
  119.  
  120.     t = time.time()
  121.  
  122.     #cv2.namedWindow('target', cv2.WINDOW_NORMAL)
  123.     #cv2.imshow('target', target)
  124.     #cv2.waitKey(0)
  125.     #cv2.destroyAllWindows()
  126.  
  127.  
  128.     answer = main2(target)
  129.     #answer = target
  130.     #cv2.imshow("target", target)
  131.     #cv2.imshow("answer", answer)
  132.  
  133.     #cv2.namedWindow('answer', cv2.WINDOW_NORMAL)
  134.     #cv2.imshow('answer', answer)
  135.     #cv2.waitKey(0)
  136.     #cv2.destroyAllWindows()
  137.  
  138.     filepath = "C/Users/Insaf/PycharmProjects/genetic"
  139.  
  140.     cv2.imwrite('target.jpg', target)
  141.     cv2.imwrite('result.jpg', answer)
  142.  
  143.     print("TIME ELAPSED: {}".format(time.time() - t))
  144.  
  145.  
  146. PS = 3
  147. GPS = (PS*(PS-1)//2)
  148.  
  149. main1()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement