@@ -72,8 +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
- latest_displacements = 0
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
77
83
78
84
gradient_0_direction = np .copy (self .gradient_0 )
79
85
gradient_1_direction = np .copy (self .gradient_1 )
@@ -104,22 +110,18 @@ def p_bar(x, **kwargs): return x # empty function
104
110
for i , image in enumerate (p_bar (limited_mraw , ncols = 100 )):
105
111
image_filtered = self .subset (image , self .subset_size )
106
112
107
- if self .pixel_shift :
108
- print ( 'Pixel-shifting is not yet implemented.' )
109
- break
113
+ self . image_roi = image_filtered [ video . points [:, 0 ] + self .delta_0 , video . points [:, 1 ] + self . delta_1 ]
114
+ self . latest_displacements = ( self . reference_image [ video . points [:, 0 ] , video . points [:, 1 ] ] - self . image_roi ) / \
115
+ self . gradient_magnitude [ video . points [:, 0 ], video . points [:, 1 ]]
110
116
111
- else :
112
- self .image_roi = image_filtered [video .points [:,
113
- 0 ], video .points [:, 1 ]]
117
+ self .displacements [:, i , 0 ] = signs_0 * (self .direction_correction_0 * self .latest_displacements ) + self .delta_0
118
+ self .displacements [:, i , 1 ] = signs_1 * (self .direction_correction_1 * self .latest_displacements ) + self .delta_1
114
119
115
- self .latest_displacements = (self .reference_image [video .points [:, 0 ], video .points [:, 1 ]] - self .image_roi ) / \
116
- self .gradient_magnitude [video .points [:,
117
- 0 ], video .points [:, 1 ]]
120
+ if self .pixel_shift :
121
+ self .pixel_shift_fun (i , video .points , image .shape )
118
122
119
- self .displacements [:, i , 0 ] = signs_0 * self .direction_correction_0 * \
120
- self .latest_displacements * self .convert_from_px
121
- self .displacements [:, i , 1 ] = signs_1 * self .direction_correction_1 * \
122
- self .latest_displacements * self .convert_from_px
123
+ # Convert the displacements from pixels to physical units:
124
+ self .displacements *= self .convert_from_px
123
125
124
126
# average the neighbouring points
125
127
if isinstance (self .mean_n_neighbours , int ):
@@ -155,10 +157,20 @@ def displacement_averaging(self):
155
157
(d_0 [:, :, np .newaxis ], d_1 [:, :, np .newaxis ]), axis = 2 )
156
158
print ('Finished!' )
157
159
158
- def pixel_shift (self ):
159
- """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.
160
162
"""
161
- 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
162
174
163
175
def reference (self , images , subset_size ):
164
176
"""Calculation of the reference image, image gradients and gradient amplitudes.
0 commit comments