7

After applying skeletonization on an image(),enter image description here

I want to measure the longest branch, or spine of the skeleton using python. ImageJ has several tools that do this job one is Measure_Skeleton_length, another is AnalyzeSkeleton. Any tools or suggestions in python?

icypy
  • 3,062
  • 5
  • 25
  • 27

2 Answers2

6

This comes as a very late reply to the original question, but just in case someone who still is in need might end up reading this post. There is an awesome python package for analyzing skeletons called FilFinder (pip install fil_finder) which solves this problem elegantly. Below is a code adopted from their tutorial

import numpy as np
import cv2
import matplotlib.pyplot as plt
from fil_finder import FilFinder2D
import astropy.units as u

skeleton = ... #in numpy array format

fil = FilFinder2D(skeleton, distance=250 * u.pc, mask=skeleton)
fil.preprocess_image(flatten_percent=85)
fil.create_mask(border_masking=True, verbose=False,
use_existing_mask=True)
fil.medskel(verbose=False)
fil.analyze_skeletons(branch_thresh=40* u.pix, skel_thresh=10 * u.pix, prune_criteria='length')

plt.imshow(fil.skeleton, cmap='gray')
plt.contour(fil.skeleton_longpath, colors='r')
plt.axis('off')
plt.show()

which outputs

enter image description here

mjkvaak
  • 399
  • 3
  • 6
1

I don't know for python tools, but here is the way to do it from an algorithmic point of view:

  1. Detect all the extreme pixels (last pixel on a branch, so pixels with only one neighbor)
  2. for each o these pixels, compute a geodesic distance map.

Then you the maximum distance found during the maps computation will be the distance you want. Point 1 is basic coding, so you can do it in Python, but you have to find a library for point 2.

FiReTiTi
  • 5,597
  • 12
  • 30
  • 58
  • 3
    In a similar vein, you could detect a few extremal points and then calculate the shortest connected paths between them, using ``skimage.graph``. The maximum result is probably close to the longest branch length. – Stefan van der Walt Dec 01 '15 at 09:31