@@ -72,9 +72,14 @@ def calculate_displacements(self, video):
72
72
if not hasattr (video , 'points' ):
73
73
raise Exception ('Please set points for analysis!' )
74
74
75
- self .displacements = np .zeros ((video .points .shape [0 ], video .N , 2 ))
76
- self .delta_0 = np .zeros ((video .points .shape [0 ],)).astype (int )
77
- self .delta_1 = np .zeros ((video .points .shape [0 ],)).astype (int )
75
+ self .displacements = np .zeros ((video .points .shape [0 ], video .N , 2 ))
76
+ if self .pixel_shift :
77
+ self .delta_0 = np .zeros ((video .points .shape [0 ],)).astype (int )
78
+ self .delta_1 = np .zeros ((video .points .shape [0 ],)).astype (int )
79
+ self .valid_points = np .ones ((video .points .shape [0 ],)).astype (bool )
80
+ else :
81
+ self .delta_0 = 0
82
+ self .delta_1 = 0
78
83
79
84
gradient_0_direction = np .copy (self .gradient_0 )
80
85
gradient_1_direction = np .copy (self .gradient_1 )
@@ -113,9 +118,8 @@ def p_bar(x, **kwargs): return x # empty function
113
118
self .displacements [:, i , 1 ] = signs_1 * (self .direction_correction_1 * self .latest_displacements ) + self .delta_1
114
119
115
120
if self .pixel_shift :
116
- self .delta_0 = np .round (self .displacements [:, i , 0 ]).astype (int )
117
- self .delta_1 = np .round (self .displacements [:, i , 1 ]).astype (int )
118
-
121
+ self .pixel_shift_fun (i , video .points , image .shape )
122
+
119
123
# Convert the displacements from pixels to physical units:
120
124
self .displacements *= self .convert_from_px
121
125
@@ -153,10 +157,20 @@ def displacement_averaging(self):
153
157
(d_0 [:, :, np .newaxis ], d_1 [:, :, np .newaxis ]), axis = 2 )
154
158
print ('Finished!' )
155
159
156
- def pixel_shift (self ):
157
- """Pixel shifting implementation.
160
+ def pixel_shift_fun (self , i , points , image_shape ):
161
+ """Pixel shifting implementation. Points that are going outside of the image range are excluded.
158
162
"""
159
- pass
163
+ self .delta_0 = np .round (self .displacements [:, i , 0 ]).astype (int )
164
+ self .delta_1 = np .round (self .displacements [:, i , 1 ]).astype (int )
165
+
166
+ # Exlude the points that have displacement going outside of the image range
167
+ out_of_range_it = np .logical_or (self .delta_0 + points [:, 0 ] > image_shape [0 ] - 1 , self .delta_1 + points [:, 1 ] > image_shape [1 ] - 1 )
168
+ if np .any (out_of_range_it ):
169
+ self .delta_0 [out_of_range_it ] = 0
170
+ self .delta_1 [out_of_range_it ] = 0
171
+ self .valid_points [out_of_range_it ] = False
172
+ warnings .warn ('Displacement is going outside of the image range! The valid points are saved in self.method.valid_points' )
173
+ self .displacements [~ self .valid_points , i , :] = np .nan
160
174
161
175
def reference (self , images , subset_size ):
162
176
"""Calculation of the reference image, image gradients and gradient amplitudes.
0 commit comments