-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerator.py
134 lines (106 loc) · 4.02 KB
/
generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import numpy as np
import cv2
import math
import pygame
import time
import general_usefull_package
def convert_image_if_needed(image, convert_to="gray"):
if convert_to == "gray":
if len(image.shape) < 3:
return image
elif len(image.shape) == 3:
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
else:
print("Nie wiadomo, co to za obraz! :(")
elif convert_to == "color":
if len(image.shape) < 3:
return cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
elif len(image.shape) == 3:
return image
else:
print("Nie wiadomo, co to za obraz! :(")
else:
print("Zdefiniuj poprawnie paramter 'convert_to', "
"ktory moze przyjmowac wartosci 'gray' lub 'color'")
class PatternGenerator(object):
def __init__(self):
pass
# self.__phi = None
# self.__T = None
# self.__w = None
# self.__h = None
# self.__image = None
def calculate_vertical_pattern(self, phi, T, w, h):
# Creating frame with given size
image = np.empty((h, w))
# Calculating intensity
omega = 2 * math.pi / T
y_single_row = [math.sin(omega*x + phi) for x in range(w)]
for index, row in enumerate(image):
image[index] = y_single_row
# Scaling sinus to 0-1
image = (image + 1) / 2
return image
def calculate_horizontal_pattern(self, phi, T, w, h):
return self.calculate_vertical_pattern(phi, T, h, w).T
def pattern_sin(self, w, h, phases=[], periods=[], directions=[]):
images = []
names = []
for direction in directions:
for period in periods:
for phi in phases:
if direction == 90:
images.append(self.calculate_vertical_pattern(phi, T=period, w=w, h=h))
names.append("phi=" + str(phi) + " T=" + str(period) + " direct=" + str(direction))
elif direction == 0:
images.append(self.calculate_horizontal_pattern(phi, T=period, w=w, h=h))
names.append("phi=" + str(phi) + " T=" + str(period) + " direct=" + str(direction))
return images, names
def gray(im):
im = 255 * (im / im.max())
w, h = im.shape
ret = np.empty((w, h, 3), dtype=np.uint8)
ret[:, :, 2] = ret[:, :, 1] = ret[:, :, 0] = im
return ret
def get_picture(index, images):
Z = images[index].T
Z = 255 * Z / Z.max()
Z = gray(Z)
return pygame.surfarray.make_surface(Z)
def main(images, names, w, h):
for index, (image, name) in enumerate(zip(images, names)):
cv2.imshow("Pattern_" + str(name), image)
cv2.waitKey(200)
cv2.destroyAllWindows()
pygame.display.set_mode((w, h), pygame.FULLSCREEN)
main_surface = pygame.display.get_surface()
a = 0
while a < len(images):
Z = get_picture(index=a, images=images)
main_surface.blit(Z, (0, 0))
pygame.display.update()
time.sleep(600)
a += 1
if __name__ == "__main__":
#pg = PatternGenerator()
# pattern = pg.calculate_horizontal_pattern(phi=20, T=20, w=500, h=400)
# print(pattern)
# cv2.imshow("Pattern_horizontal", pattern)
# pattern2 = pg.calculate_vertical_pattern(phi=20, T=20, w=500, h=400)
# print(pattern2)
# cv2.imshow("Pattern_vertical", pattern2)
#phases = list(np.linspace(0, 90, num=5)) # radiany
# images, names = pg.pattern_sin(w=800, h=600, phases=phases, periods=[160], directions=[0])
# for index, (image, name) in enumerate(zip(images, names)):
# cv2.imshow("Pattern_" + str(name), image)
w = 1500
h = 1200
pg = PatternGenerator()
phases = list(np.linspace(0, 90, num=5))
images, names = pg.pattern_sin(w=w, h=h, phases=[+math.pi], periods=[20], directions=[0])
cv2.imshow("Img", images[0])
cv2.waitKey()
color_img = 255*images[0]
cv2.imwrite("test.bmp", color_img)
for name in names: print(name)
#main(images, names, w, h)