Skip to content

Commit d37af22

Browse files
authored
Merge pull request #26 from ThijsIllimited/master
Implemented Pixel shift
2 parents 39ad506 + 5e82f91 commit d37af22

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

pyidi/methods/_simplified_optical_flow.py

+30-18
Original file line numberDiff line numberDiff line change
@@ -72,8 +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-
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
7783

7884
gradient_0_direction = np.copy(self.gradient_0)
7985
gradient_1_direction = np.copy(self.gradient_1)
@@ -104,22 +110,18 @@ def p_bar(x, **kwargs): return x # empty function
104110
for i, image in enumerate(p_bar(limited_mraw, ncols=100)):
105111
image_filtered = self.subset(image, self.subset_size)
106112

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]]
110116

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
114119

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)
118122

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
123125

124126
# average the neighbouring points
125127
if isinstance(self.mean_n_neighbours, int):
@@ -155,10 +157,20 @@ def displacement_averaging(self):
155157
(d_0[:, :, np.newaxis], d_1[:, :, np.newaxis]), axis=2)
156158
print('Finished!')
157159

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.
160162
"""
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
162174

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

0 commit comments

Comments
 (0)