Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ideas how to measure the length of a skeleton using python

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?

like image 938
icypy Avatar asked Sep 27 '22 02:09

icypy


2 Answers

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

like image 54
mjkvaak Avatar answered Sep 29 '22 00:09

mjkvaak


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.

like image 40
FiReTiTi Avatar answered Sep 29 '22 00:09

FiReTiTi