• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Apr 19th, 2019 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import cv2
2. import numpy as np
3. import random
4. from operator import itemgetter
5.
6.
7. def get_fitness_list(population, target):
9.     for img in population:
10.         fitness_ = fitness(img, target)
13.
14.
15. def fitness(image, target):
16.     ans = 0
17.     for i in range(len(image)):
18.         for j in range(len(image[0])):
19.             ans += (image[i, j, 0] - target[i, j, 0]) ** 2
20.             ans += (image[i, j, 1] - target[i, j, 1]) ** 2
21.             ans += (image[i, j, 2] - target[i, j, 2]) ** 2
22.     return ans
23.
24.
25. def change_matrix_in_random(image, max_change):
26.     for i in range(0, len(image)):
27.         for j in range(0, len(image[0])):
28.             a = np.random.randint(-max_change, max_change)
29.             b = np.random.randint(-max_change, max_change)
30.             c = np.random.randint(-max_change, max_change)
31.             image[i, j, 0] = max(min(image[i, j, 0] + a, 0), 255)
32.             image[i, j, 1] = max(min(image[i, j, 1] + b, 0), 255)
33.             image[i, j, 2] = max(min(image[i, j, 2] + c, 0), 255)
34.
35.
36. def get_rand_img():
37.     img = np.zeros((512, 512, 3), np.uint8)
38.     for i in range(0, 512):
39.         for j in range(0, 512):
40.             img[i, j, 0] = random.randint(0, 255)
41.             img[i, j, 1] = random.randint(0, 255)
42.             img[i, j, 2] = random.randint(0, 255)
43.     return img
44.
45.
46. def make_median(population, image):
47.     copy = image.copy()
48.     for i in range(0, len(image)):
49.         for j in range(0, len(image[0])):
50.             pixel = get_pixel_median(i, j, population)
51.             copy[i, j, 0] = pixel[0]
52.             copy[i, j, 1] = pixel[1]
53.             copy[i, j, 2] = pixel[2]
54.     return copy
55.
56.
57. def get_pixel_median(i, j, population):
58.     answer = [0, 0, 0]
59.     counter = 0
60.     for img in population:
61.         answer[0] += img[i, j, 0]
62.         answer[1] += img[i, j, 1]
63.         answer[2] += img[i, j, 2]
64.         counter += 1
69.
70.
71. def select_best(population, target):
72.     fitness_list = get_fitness_list(population, target)
73.     arr_sort = []
74.     for i in range(GPS):
75.         arr_sort.append([fitness_list[i], population[i]])
76.     arr_sort = sorted(arr_sort, key=itemgetter(0))
79.
80.
81. def grow(population):
83.     for img in population:
84.         for i in range(GPS//PS):
87.
88.
89. def main2(target):
90.     population = []
91.     for i in range(PS):
92.         population.append(get_rand_img())
93.         print(i)
94.     for i in range(10):
95.         print(i)
96.         population = grow(population)
97.         population = select_best(population, target)
98.     return population[0]
99.
100.
101. def main1():
102.     filepath = "kim.jpg"
104.
105.     #cv2.namedWindow('target', cv2.WINDOW_NORMAL)
106.     #cv2.imshow('target', target)
107.     #cv2.waitKey(0)
108.     #cv2.destroyAllWindows()
109.
110.
112.     #cv2.imshow("target", target)
114.
115.     cv2.namedWindow('target', cv2.WINDOW_NORMAL)
116.     cv2.imshow('target', target)
117.     cv2.waitKey(0)
118.     cv2.destroyAllWindows()
119.
120.
121. PS = 10
122. GPS = 20
123.
124. main1()
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.

Top