geometry.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:CElegansBehaviour 作者: ChristophKirst 项目源码 文件源码
def skeleton_from_image_discrete(image, sigma = 1, absolute_threshold = None, threshold_factor = 0.95, with_head_tail = False, verbose = False):
  """Detect skeleton points of wormshape in image

  Arguments:
    image (array): the image to detect venterline of worm from
    sigma (float or None): width of Gaussian smoothing on image, if None use raw image
    absolute_threshold (float or None): if set use this as the threshold, if None the threshold is set via Otsu
    threshold_level (float): in case the threshold is determined by Otsu multiply by this factor
    verbose (bool): plot results

  Returns:
    array (nx2): unsorted skeleton points
  """
  ### smooth image
  if sigma is not None:
    imgs = filters.gaussian_filter(np.asarray(image, float), sigma);
  else:
    imgs = image;

  ### get worm foreground
  if absolute_threshold is not None:
    level = absolute_threshold;
  else:
    level = threshold_factor * threshold_otsu(imgs);
  imgth = imgs < level;  

  ### skeletonize
  skel = skeletonize(imgth);
  y,x = np.where(skel);

  if verbose:
    plt.imshow(imgth, interpolation = 'none');
    plt.scatter(x,y,c = 'k', s = 40);

  if with_head_tail:
    # find end points:
    adj = skeleton_to_adjacency(skel);
    nhs = np.array([len(v) for v in adj.values()]);
    ht = np.where(nhs == 1)[0];

    if verbose:
      xy = np.vstack([x,y]).T;
      if ht.shape[0] > 0:
        xyht = xy[ht];
        plt.scatter(xyht[:,0], xyht[:,1], s = 60, c = 'r');

    return np.vstack([x,y]).T, ht;
  else:
    return np.vstack([x,y]).T;
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号