Skip to content

Commit 5e82f91

Browse files
Updated pixel shifting
delta_0/delta_1 are now 0 instead of an array of zeros when pixel shifting is off. Moved the execution of the pixel shift in the already existing definition for pixel shifting. Implemented a method that catches points going outside of the image. Displacements of these points are set to NaN after this happens. Before, a point going outside of the image caused an error.
1 parent 8f1a5ba commit 5e82f91

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

pyidi/methods/_simplified_optical_flow.py

+23-9
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,14 @@ def calculate_displacements(self, video):
7272
if not hasattr(video, 'points'):
7373
raise Exception('Please set points for analysis!')
7474

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
7883

7984
gradient_0_direction = np.copy(self.gradient_0)
8085
gradient_1_direction = np.copy(self.gradient_1)
@@ -113,9 +118,8 @@ def p_bar(x, **kwargs): return x # empty function
113118
self.displacements[:, i, 1] = signs_1 * (self.direction_correction_1 * self.latest_displacements) + self.delta_1
114119

115120
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+
119123
# Convert the displacements from pixels to physical units:
120124
self.displacements *= self.convert_from_px
121125

@@ -153,10 +157,20 @@ def displacement_averaging(self):
153157
(d_0[:, :, np.newaxis], d_1[:, :, np.newaxis]), axis=2)
154158
print('Finished!')
155159

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.
158162
"""
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
160174

161175
def reference(self, images, subset_size):
162176
"""Calculation of the reference image, image gradients and gradient amplitudes.

0 commit comments

Comments
 (0)