Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- import random
- from operator import itemgetter
- def get_fitness_list(population, target):
- answer = []
- for img in population:
- fitness_ = fitness(img, target)
- answer.append(fitness_)
- return answer
- def fitness(image, target):
- ans = 0
- for i in range(len(image)):
- for j in range(len(image[0])):
- ans += (image[i, j, 0] - target[i, j, 0]) ** 2
- ans += (image[i, j, 1] - target[i, j, 1]) ** 2
- ans += (image[i, j, 2] - target[i, j, 2]) ** 2
- return ans
- def change_matrix_in_random(image, max_change):
- for i in range(0, len(image)):
- for j in range(0, len(image[0])):
- a = np.random.randint(-max_change, max_change)
- b = np.random.randint(-max_change, max_change)
- c = np.random.randint(-max_change, max_change)
- image[i, j, 0] = max(min(image[i, j, 0] + a, 0), 255)
- image[i, j, 1] = max(min(image[i, j, 1] + b, 0), 255)
- image[i, j, 2] = max(min(image[i, j, 2] + c, 0), 255)
- def get_rand_img():
- img = np.zeros((512, 512, 3), np.uint8)
- for i in range(0, 512):
- for j in range(0, 512):
- img[i, j, 0] = random.randint(0, 255)
- img[i, j, 1] = random.randint(0, 255)
- img[i, j, 2] = random.randint(0, 255)
- return img
- def make_median(population, image):
- copy = image.copy()
- for i in range(0, len(image)):
- for j in range(0, len(image[0])):
- pixel = get_pixel_median(i, j, population)
- copy[i, j, 0] = pixel[0]
- copy[i, j, 1] = pixel[1]
- copy[i, j, 2] = pixel[2]
- return copy
- def get_pixel_median(i, j, population):
- answer = [0, 0, 0]
- counter = 0
- for img in population:
- answer[0] += img[i, j, 0]
- answer[1] += img[i, j, 1]
- answer[2] += img[i, j, 2]
- counter += 1
- answer[0] /= len(population)
- answer[1] /= len(population)
- answer[2] /= len(population)
- return answer
- def select_best(population, target):
- fitness_list = get_fitness_list(population, target)
- arr_sort = []
- for i in range(GPS):
- arr_sort.append([fitness_list[i], population[i]])
- arr_sort = sorted(arr_sort, key=itemgetter(0))
- answer = arr_sort[0:PS]
- return answer
- def grow(population):
- answer = []
- for img in population:
- for i in range(GPS//PS):
- answer.append(change_matrix_in_random(img, 3))
- return answer
- def main2(target):
- population = []
- for i in range(PS):
- population.append(get_rand_img())
- print(i)
- for i in range(10):
- print(i)
- population = grow(population)
- population = select_best(population, target)
- return population[0]
- def main1():
- filepath = "kim.jpg"
- target = cv2.imread(filepath, cv2.IMREAD_COLOR)
- #cv2.namedWindow('target', cv2.WINDOW_NORMAL)
- #cv2.imshow('target', target)
- #cv2.waitKey(0)
- #cv2.destroyAllWindows()
- answer = main2(target)
- #cv2.imshow("target", target)
- #cv2.imshow("answer", answer)
- cv2.namedWindow('target', cv2.WINDOW_NORMAL)
- cv2.imshow('target', target)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
- PS = 10
- GPS = 20
- main1()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement