-
Notifications
You must be signed in to change notification settings - Fork 3
/
pattern.py
executable file
·176 lines (134 loc) · 6.26 KB
/
pattern.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#######################################################################################
#This library is used to generate moving patterns
# Author: Ashish Rao M
# email: ashish.rao.m@gmail.com
#######################################################################################
import cv2
import numpy as np
from scipy import signal
from matplotlib import pyplot as plt
from cv2 import VideoWriter, VideoWriter_fourcc
class Pattern:
'''
General structure for moving patterns
Init Parameters:
----------------
height: <int>
Height of video resolution
width: <int>
width of the video resolution
fps: <int>
Number of frames per seconds
'''
def __init__(self, height, width, fps):
self.width = width
self.height = height
self.fps = fps
self.fourcc = VideoWriter_fourcc(*'MP42')
class Line(Pattern):
'''
Class to generate moving lines of different thicknesses, orientations and speeds
Init Parameters:
----------------
thickness: <int>
pixelwise thickness of the moving line pattern
'''
def __init__(self, height, width, fps, thickness):
Pattern.__init__(self, height, width, fps)
self.thickness = thickness
def generate_moving_line(self, direction):
'''
Generate moving line whose direction is defined by the variable direction
Parameters:
-----------
direction: <string>
Defines the direction in which the line has to move
Return: None
------------
'''
self.video = VideoWriter('./videos/' + direction + 'line_pattern_gen_'+str(self.fps)+'_bit.avis', self.fourcc, float(self.fps), (self.width, self.height))
if direction=='vertical':
print('generating video of vertical line')
for x_coord in range(0, self.width):
frame = np.zeros((self.height, self.width, 3), dtype=np.uint8)
cv2.line(frame, (x_coord, 0), (x_coord, self.height), (0, 0, 255),self.thickness)
self.video.write(frame)
if direction=='horizontal':
print('generating video of horizontal line')
for y_coord in range(0, self.height):
frame = np.zeros((self.height, self.width, 3), dtype=np.uint8)
cv2.line(frame, (0, y_coord), (self.width, y_coord), (0, 0, 255), self.thickness)
self.video.write(frame)
if direction=='tl_diag':
print('generating line from top left')
for i in range(0, 2*self.width):
frame = np.zeros((self.height, self.width, 3), dtype=np.uint8)
cv2.line(frame, (0,i), (i,0), (0, 0, 255), self.thickness)
self.video.write(frame)
if direction=='bl_diag':
print('generating line from bottom left')
for i in range(2*self.width):
frame = np.zeros((self.height, self.width, 3), dtype=np.uint8)
cv2.line(frame, (0,self.width-i), (i,self.width), (0, 0, 255), self.thickness)
self.video.write(frame)
cv2.destroyAllWindows()
self.video.release()
class Wave_2d(Pattern):
'''
Class to generate moving wave patterns
Init Parameters:
----------------
frequency: <int>
Spatial frequency of the wave patterns
pixel_shifted: <int>
Number of pixels to shift by in two successive frames of the video
'''
# generate moving patterns
def __init__(self, height, width, fps, frequency, pixel_shifted):
Pattern.__init__(self, height, width, fps)
self.f = frequency
self.pixel_shift = pixel_shifted
def generate_moving_wave(self, direction, type):
'''
Generate moving wave whose direction is defined by the variable, direction.
Parameters:
-----------
direction: <string>
Defines the direction in which the wave has to move
type: <string>
The type of wave being generated> Square and Sinusoidal waves have been implemented for now.
Return: None
-------
'''
self.video = VideoWriter('./videos/' + type +'/' + direction + '_'+type + '_' + str(self.f) + 'hz_pattern_gen_'+str(self.fps) + '_fps.mkv', self.fourcc, float(self.fps), (self.width, self.height))
if direction=='horizontal':
print('generating video of horizontal wave based phase patterns')
dt = 1/self.width
x = np.arange(0, self.width, dt)
if type == 'sine':
y = np.sin(2 * np.pi * x * self.f)
if type == 'square':
y = signal.square(2 * np.pi * x * self.f)
y += max(y) # to shift range of signals to positive values
frame = np.array([[y[j]*127 for j in range(self.width)] for i in range(self.height)], dtype=np.uint8) # create 2-D array of sine-wave
for _ in range(0, self.width):
self.video.write(cv2.cvtColor(frame,cv2.COLOR_GRAY2RGB))
shifted_frame = np.roll(frame, self.pixel_shift, axis=1) # simulated the circular shifting effect to make it a continuous
frame = shifted_frame
if direction=='vertical':
print('generating video of horizontal wave based phase patterns')
dt = 1/self.height
x = np.arange(0, 1, dt)
if type == 'sine':
y = np.sin(2 * np.pi * x * self.f)
if type == 'square':
y = signal.square(2 * np.pi * x * self.f)
y += max(y) # to shift range of signals to positive values
frame = np.array([[y[j]*127 for j in range(self.width)] for i in range(self.height)], dtype=np.uint8).T # create 2-D array of sine-wave. Transpose it to make it vertical
for _ in range(0, self.height):
self.video.write(cv2.cvtColor(frame,cv2.COLOR_GRAY2RGB))
shifted_frame = np.roll(frame, self.pixel_shift, axis=0)
frame = shifted_frame
cv2.destroyAllWindows()
self.video.release()
print('generation of moving patterns done')