Example #1
0
def cost_from_countour(model,
                       contour,
                       head_tail_xy=None,
                       verbose=False,
                       weight_head_tail=0.9,
                       weight_shape=0.1):
    if head_tail_xy is None:
        head_tail_xy = wgeo.head_tail_from_contour_discrete(contour,
                                                            ncontour=all,
                                                            delta=0.3,
                                                            smooth=1.0,
                                                            with_index=False,
                                                            verbose=False,
                                                            save=None)

    left, right, normals = model.shape(with_normals=True)
    res = wgeo.distance_shape_to_contour_discrete(left,
                                                  right,
                                                  normals,
                                                  contour,
                                                  search_radius=[15, 20],
                                                  min_alignment=0,
                                                  match_head_tail=head_tail_xy,
                                                  verbose=verbose)
    return cost_from_distance(res,
                              weight_head_tail=weight_head_tail,
                              weight_shape=weight_shape)
 def distance_to_contour(self, contour,  search_radius=[5,20], min_alignment = 0, match_head_tail = None, with_points = False, verbose = False):
   """Match worm shape to contour and return distances
   
   Arguments:
     left,right (nx2 array): shape 
     normals (nx2 array): normals for the shape
     contour (Curve): the contour curve
     search_radius (float or array): the search radius to check for points
     min_alignment (float or None): if not None also ensure the normals are aligned
     with_points (bool): if True also return intersection points coordinates
     verbose (bool): plot results    
   
   Returns:
     array: distances between the worm shape sample points and the nearest contour points, occluded points will be set to NaNs.
   
   Note:
     The number of distances is 2 * (npoints-2) + 2 = 2*npoints - 2
     as the head and tail are only counted once.
     
     It might be useful to average distances of corresponding left and right points
   """
   
   left, right, normals = self.shape(with_normals = True);
   res = wormgeo.distance_shape_to_contour_discrete(left,right,normals,contour,
                                                    search_radius=search_radius, min_alignment=min_alignment, match_head_tail=match_head_tail,
                                                    verbose = verbose);
   if match_head_tail is not None:
     distances_left, intersection_pts_left, distances_right, intersection_pts_right, distance_head, head_match, distance_tail, tail_match = res;
     distances = np.hstack([distance_head, distances_left, distances_right, distance_tail]);
     
     if with_points:
       if head_match is None:
         head_pos = np.array([np.nan, np.nan]);
       else:
         head_pos = match_head_tail[head_match];
       if tail_match is None:
         tail_pos = np.array([np.nan, np.nan]);
       else:
         tail_pos = match_head_tail[tail_match]; 
       intersecs = np.vstack([head_pos, intersection_pts_left, intersection_pts_right, tail_pos]).T;
       return (distances, intersecs);
     else:
       return distances;
     
   else:
     distances_left, intersection_pts_left, distances_right, intersection_pts_right = res;
     distances = np.hstack([distances_left[:-1], distances_right[1:]]);
     if with_points:
       return (distances, np.vstack([intersection_pts_left[:-1], intersection_pts_right[1:]]));
     else:
       return distances;
Example #3
0
    def distance_to_contour(self,
                            contour,
                            search_radius=[5, 20],
                            min_alignment=0,
                            match_head_tail=None,
                            with_points=False,
                            verbose=False):
        """Match worm shape to contour and return distances
    
    Arguments:
      left,right (nx2 array): shape 
      normals (nx2 array): normals for the shape
      contour (Curve): the contour curve
      search_radius (float or array): the search radius to check for points
      min_alignment (float or None): if not None also ensure the normals are aligned
      with_points (bool): if True also return intersection points coordinates
      verbose (bool): plot results    
    
    Returns:
      array: distances between the worm shape sample points and the nearest contour points, occluded points will be set to NaNs.
    
    Note:
      The number of distances is 2 * (npoints-2) + 2 = 2*npoints - 2
      as the head and tail are only counted once.
      
      It might be useful to average distances of corresponding left and right points
    """

        left, right, normals = self.shape(with_normals=True)
        res = wormgeo.distance_shape_to_contour_discrete(
            left,
            right,
            normals,
            contour,
            search_radius=search_radius,
            min_alignment=min_alignment,
            match_head_tail=match_head_tail,
            verbose=verbose)
        if match_head_tail is not None:
            distances_left, intersection_pts_left, distances_right, intersection_pts_right, distance_head, head_match, distance_tail, tail_match = res
            distances = np.hstack([
                distance_head, distances_left, distances_right, distance_tail
            ])

            if with_points:
                if head_match is None:
                    head_pos = np.array([np.nan, np.nan])
                else:
                    head_pos = match_head_tail[head_match]
                if tail_match is None:
                    tail_pos = np.array([np.nan, np.nan])
                else:
                    tail_pos = match_head_tail[tail_match]
                intersecs = np.vstack([
                    head_pos, intersection_pts_left, intersection_pts_right,
                    tail_pos
                ]).T
                return (distances, intersecs)
            else:
                return distances

        else:
            distances_left, intersection_pts_left, distances_right, intersection_pts_right = res
            distances = np.hstack([distances_left[:-1], distances_right[1:]])
            if with_points:
                return (distances,
                        np.vstack([
                            intersection_pts_left[:-1],
                            intersection_pts_right[1:]
                        ]))
            else:
                return distances
Example #4
0
ik = w.center.shape[0]
eps[:ik] = 0.5

###

plt.figure(20)
plt.clf()
left, right, normals = w.shape(with_normals=True)

reload(wgeo)
reload(wc)

dl, xyl, dr, xyr, dh, hm, dt, tm = wgeo.distance_shape_to_contour_discrete(
    left,
    right,
    normals,
    contour,
    search_radius=[15, 20],
    min_alignment=0,
    match_head_tail=head_tail_xy,
    verbose=True)

wc.cost_func(w,
             par_1,
             contour,
             head_tail_xy=head_tail_xy,
             verbose=True,
             weight_head_tail=10,
             weight_shape=5.0,
             weight_distances=5.0)
cntrs = wgeo.contour_from_image(imgs, sigma = 1, absolute_threshold = None, threshold_factor = 0.9, 
                          verbose = True, save = None);
cntr = resample_curve(cntrs[0], 100);
contour = Curve(cntr, nparameter = 50);                        
                      
                      
plt.figure(2); plt.clf();
head_tail_xy = wgeo.head_tail_from_contour(cntrs, ncontour = all, delta = 0.3, smooth = 1.0, with_index = False,
                            verbose = True, save = None, image = imgs);


left,right,normals = w.shape(with_normals=True);  
plt.figure(3); plt.clf()
reload(wgeo)
res = wgeo.distance_shape_to_contour_discrete(left,right,normals,contour,
                                              search_radius=[5,20], min_alignment=0, match_head_tail=head_tail_xy,
                                              verbose = True);
 


reload(wgeo)

def cost_func(model, parameter, contours):
  """Cost function"""
  model.set_parameter(parameter);
  return wgeo.cost_from_contours(model, contours);


def cost_func_grad(model, parameter, contour, epsilon = 0.1):
  """Numerical approximation of the gradient of the cost function"""
  nparameter = parameter.shape[0];