def add_line(self, points, color='w', is_closed=True, mark_points=False, width=1): """ adds a polygon to the _frame """ if len(points) == 0: return points = np.asarray(points) # find the regions where the points are finite # Here, we compare to 0 to capture nans in the int32 array indices = contiguous_true_regions(points[:, 0] > 0) for start, end in indices: # add the line line_points = (points[start:end, :] / self.zoom_factor).astype( np.int) thickness = int(np.ceil(width / self.zoom_factor)) cv2.polylines(self._frame, [line_points], isClosed=is_closed, color=self.get_color(color), thickness=thickness) # mark the anchor points if requested if mark_points: for p in points[start:end, :]: self.add_circle(p, 2 * width, color, thickness=-1)
def trajectory_smoothed(self, sigma): """ returns the mouse trajectory smoothed with a Gaussian filter of standard deviation `sigma` """ trajectory = np.empty_like(self.pos) trajectory.fill(np.nan) # smooth position indices = contiguous_true_regions(np.isfinite(self.pos[:, 0])) for start, end in indices: if end - start > 1: filters.gaussian_filter1d(self.pos[start:end, :], sigma, axis=0, mode='nearest', output=trajectory[start:end, :]) return trajectory
def update_mouse_mask(self): """ updates the internal mask that tells us where the mouse has been """ mouse_track = self.data['pass2/mouse_trajectory'] # extract the mouse track since the last _frame trail_length = self.params['output/video/period'] + 1 trail_width = int(2*self.params['mouse/model_radius']) time_start = max(0, self.frame_id - trail_length) track = mouse_track.pos[time_start:self.frame_id, :].astype(np.int32) # find the regions where the points are finite # Here, we compare to 0 to capture nans in the int32 array indices = contiguous_true_regions(track[:, 0] > 0) for start, end in indices: cv2.polylines(self.mouse_mask, [track[start:end, :]], isClosed=False, color=255, thickness=trail_width)
def calculate_velocities(self, sigma): """ calculates the velocity from smoothed positions """ velocity = np.empty_like(self.pos) velocity.fill(np.nan) indices = contiguous_true_regions(np.isfinite(self.pos[:, 0])) for start, end in indices: if end - start > 1: # smooth position pos = filters.gaussian_filter1d( self.pos[start:end, :], sigma, axis=0, mode="nearest" # < std of the filter # < time axis ) # differentiate to get velocity velocity[start:end, 0] = np.gradient(pos[:, 0]) velocity[start:end, 1] = np.gradient(pos[:, 1]) self.velocity = velocity
def _get_burrow_exit_length(self, burrow): """ calculates the length of all exists of the given burrow """ # identify all points that are close to the ground line dist_max = burrow.parameters['ground_point_distance'] g_line = self.ground_line.linestring points = burrow.contour exitpoints = [ g_line.distance(geometry.Point(point)) < dist_max for point in points ] # find the indices of contiguous true regions indices = math.contiguous_true_regions(exitpoints) # find the total length of all exits exit_length = 0 for a, b in indices: exit_length += curves.curve_length(points[a:b + 1]) # handle the first and last point if they both belong to an exit if exitpoints[0] and exitpoints[-1]: exit_length += curves.point_distance(points[0], points[-1]) return exit_length