1

I'm trying to measure the longest and the transverse branchs of a banana picture, using skimage to get the skeleton and then using fil_finder to get the length of the longest branch , the code is adapted from this post

Ideas how to measure the length of a skeleton using python

but i can't get it fil_finder to work with the skeleton output of skimage, i get the following error:

raise TypeError("Data must be 2D. Please re-check the inputs.")
TypeError: Data must be 2D. Please re-check the inputs.

skeleton made using skimage banana picture used in code

any help solving the error and updates to code if any needed to measure the length of longest and transverse length

from skimage import io,img_as_float
from skimage.morphology import skeletonize
from matplotlib import pyplot as plt
from skimage.util import invert
import numpy as np
from fil_finder import FilFinder2D
import astropy.units as u

image=img_as_float(io.imread('C:/python/fruits/banana.jpg'))


gray = invert(image)
skeleton=skeletonize(gray)


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()
Yunus Temurlenk
  • 4,085
  • 4
  • 18
  • 39
Moomoh
  • 13
  • 3

1 Answers1

0

(1) Your image is probably RGB (color), and

(2) it needs to be binary (black and white, not even gray) to be skeletonized.

So, you need to do:

from skimage.color import rgb2gray
from skimage import filters

image_rgb = img_as_float(io.imread('C:/python/fruits/banana.jpg'))

image_gray = invert(rgb2gray(image_rgb))
image_binary = image_gray > filters.threshold_otsu(image_gray)  # for example

skeleton = skeletonize(image_binary)

(3) Disclaimer: I am the main author of skan, but just thought I'd throw it out there as an alternative to filfinder: an n-dimensional skeleton analysis tool: https://jni.github.io/skan/

Juan
  • 5,433
  • 21
  • 23