Beispiel #1
0
    def centerline(self):
        """ determine the center line of the tail """
        mask, offset = self.mask
        dist_map = cv2.distanceTransform(mask, cv2.DIST_L2, 5)

        # setup active contour algorithm
        ac = ActiveContour(
            blur_radius=self.centerline_blur_radius,
            closed_loop=False,
            alpha=0,  #< line length is constraint by beta
            beta=self.centerline_bending_stiffness,
            gamma=self.centerline_adaptation_rate)
        ac.max_iterations = self.centerline_max_iterations
        ac.set_potential(dist_map)

        # find centerline starting from the ventral_side
        points = curves.translate_points(self.ventral_side, -offset[0],
                                         -offset[1])
        spacing = self.centerline_spacing
        points = curves.make_curve_equidistant(points, spacing=spacing)
        # use the active contour algorithm
        points = ac.find_contour(points)
        points = curves.make_curve_equidistant(points, spacing=spacing)
        # translate points back into global coordinate system
        points = curves.translate_points(points, offset[0], offset[1])

        # orient the centerline such that it starts at the posterior end
        dist1 = curves.point_distance(points[0], self.endpoints[0])
        dist2 = curves.point_distance(points[-1], self.endpoints[0])
        if dist1 > dist2:
            points = points[::-1]

        return points
Beispiel #2
0
 def centerline(self):
     """ determine the center line of the tail """
     mask, offset = self.mask
     dist_map = cv2.distanceTransform(mask, cv2.DIST_L2, 5)
     
     # setup active contour algorithm
     ac = ActiveContour(blur_radius=self.centerline_blur_radius,
                        closed_loop=False,
                        alpha=0, #< line length is constraint by beta
                        beta=self.centerline_bending_stiffness,
                        gamma=self.centerline_adaptation_rate)
     ac.max_iterations = self.centerline_max_iterations
     ac.set_potential(dist_map)
     
     # find centerline starting from the ventral_side
     points = curves.translate_points(self.ventral_side,
                                      -offset[0], -offset[1])
     spacing = self.centerline_spacing
     points = curves.make_curve_equidistant(points, spacing=spacing)
     # use the active contour algorithm
     points = ac.find_contour(points)
     points = curves.make_curve_equidistant(points, spacing=spacing)
     # translate points back into global coordinate system
     points = curves.translate_points(points, offset[0], offset[1])
     
     # orient the centerline such that it starts at the posterior end
     dist1 = curves.point_distance(points[0], self.endpoints[0])
     dist2 = curves.point_distance(points[-1], self.endpoints[0])
     if dist1 > dist2:
         points = points[::-1]
     
     return points
Beispiel #3
0
    def adapt_tail_contours_initially(self, tails):
        """ adapt tail contour to _frame, assuming that they could be quite far
        away """
        
        # setup active contour algorithm
        ac = ActiveContour(blur_radius=self.params['contour/blur_radius_initial'],
                           closed_loop=True,
                           alpha=self.params['contour/line_tension'], 
                           beta=self.params['contour/bending_stiffness'],
                           gamma=self.params['contour/adaptation_rate'])
        ac.max_iterations = self.params['contour/max_iterations']
        ac.set_potential(self.contour_potential)
        
        # get rectangle describing the interior of the _frame
        height, width = self._frame.shape
        region_center = shapes.Rectangle(0, 0, width, height)
        region_center.buffer(-self.params['contour/border_anchor_distance'])

        # adapt all the tails
        for tail in tails:
            # find points not at the center
            anchor_idx = ~region_center.points_inside(tail.contour)
            tail.contour = ac.find_contour(tail.contour, anchor_idx, anchor_idx)

            logging.debug('Active contour took %d iterations.',
                          ac.info['iteration_count'])
Beispiel #4
0
    def find_burrows_using_active_contour(self, frame, gradient_strength):
        """ finds burrows using the previous estimate from pass3 and adapting
        it to the image using an active contour model """
        burrow_tracks = self.result['burrows/tracks']
        ground_line = self.ground.linestring
        dist_max = self.params['burrows/ground_point_distance']
        parameters = self.params['burrows/active_contour']
        
        for burrow_track in burrow_tracks:
            # get the burrow at the current time from this track 
            try:
                burrow = burrow_track.get_burrow(self.frame_id)
            except IndexError:
                # this burrow does not exist for this frame
                continue
        
            # setup active contour algorithm
            ac = ActiveContour(blur_radius=parameters['blur_radius'],
                               closed_loop=True,
                               alpha=0, 
                               beta=parameters['stiffness'],
                               gamma=parameters['convergence_rate'])
            ac.max_iterations = parameters['max_iterations']
            ac.set_potential(gradient_strength)

            # find the points close to the ground line, which will be anchored
            ground_line = self.ground.linestring
            anchor_idx = [idx for idx, point in enumerate(burrow.contour)
                          if ground_line.distance(geometry.Point(point)) < dist_max]

            # adapt the contour
            burrow.contour = ac.find_contour(burrow.contour, anchor_idx, anchor_idx)
Beispiel #5
0
    def adapt_tail_contours(self, tails):
        """ adapt tail contour to _frame, assuming that they are already close """
        # get the tails that we want to adapt
        if self.params['detection/every_frame']:
            tails_estimate = self.locate_tails_roughly(tails)
        else:
            tails_estimate = tails[:] #< copy list

        if len(tails_estimate) != len(tails):
            raise RuntimeError('Found %d instead of %d tails in this frame.' % 
                               (len(tails), len(tails_estimate)))
        
        # setup active contour algorithm
        ac = ActiveContour(blur_radius=self.params['contour/blur_radius'],
                           closed_loop=True,
                           alpha=self.params['contour/line_tension'],
                           beta=self.params['contour/bending_stiffness'],
                           gamma=self.params['contour/adaptation_rate'])
        ac.max_iterations = self.params['contour/max_iterations']
        #potential_approx = self.threshold_gradient_strength(gradient_mag)
        ac.set_potential(self.contour_potential)

#         debug.show_shape(*[t.contour for t in tails],
#                          background=self.contour_potential)        
        
        # get rectangle describing the interior of the _frame
        height, width = self._frame.shape
        region_center = shapes.Rectangle(0, 0, width, height)
        region_center.buffer(-self.params['contour/border_anchor_distance'])
        
        # iterate through all previous tails
        for k, tail_prev in enumerate(tails):
            # find the tail that is closest
            center = tail_prev.centroid
            idx = np.argmin([curves.point_distance(center, t.centroid)
                             for t in tails_estimate])
            
            # adapt this contour to the potential
            tail_estimate = tails_estimate.pop(idx)
            
            # determine the points close to the boundary that will be anchored
            # at their position, because there are no features to track at the
            # boundary 
            anchor_idx = ~region_center.points_inside(tail_estimate.contour)
            
            # disable anchoring for points at the posterior end of the tail
            ps = tail_estimate.contour[anchor_idx]
            dists = spatial.distance.cdist(ps, [tail_estimate.endpoints[0]])
            dist_threshold = self.params['contour/typical_width']
            anchor_idx[anchor_idx] = (dists.flat > dist_threshold)
            
            # use an active contour algorithm to adapt the contour points
            contour = ac.find_contour(tail_estimate.contour, anchor_idx,
                                      anchor_idx)
            logging.debug('Active contour took %d iterations.',
                          ac.info['iteration_count'])
            
            # update the old tail to keep the identity of sides
            tails[k] = Tail.create_similar(contour, tail_prev)
            
        return tails