python - Reordering image skeleton coordinates to make interp1d work better -
for school project i'm analyzing centerlines of c. elegans images. i've managed generate reasonable threshold , i'm using skimage.morphology.skeletonize
generate centerline:
then use np.nonzero
coordinates of centerline eventual goal of parametrizing these points gain insight centerline geometry.
however, when use scipy.interpolate.interp1d
, mess: i'm sure happens because when np.nonzero
looks nonzero values, goes bottom-up , right-left , orders points such, why zig-zag effect during interpolation. there way can reorder these points interp1d
plays nice them?
here code:
import cv2 import numpy np matplotlib import pyplot plt scipy import ndimage scipy import stats scipy import misc skimage.morphology import skeletonize scipy.interpolate import interp1d """curved worm""" img = misc.imread("model_image_crop_curved.tif") plt.imshow(img) plt.show() imgthresh = img>200 plt.imshow(imgthresh) plt.show() misc.imsave('model_image_crop_curved_binary.tif',imgthresh) imgskel = skeletonize(imgthresh) plt.imshow(imgskel) plt.show() misc.imsave('model_image_crop_curved_skeleton.tif',imgskel) cv2skel = cv2.imread('model_image_crop_curved_skeleton.tif',cv2.imread_grayscale) skelcoord = np.nonzero(imgskel) x = skelcoord[1] y = skelcoord[0] plt.plot(x,y,'.') plt.show() = np.arange(len(x)) interp_i = np.linspace(0,i.max(),5*i.max()) interpkind = 'linear' xi = interp1d(i,x,kind=interpkind)(interp_i) yi = interp1d(i,y,kind=interpkind)(interp_i) fig, ax = plt.subplots() ax.plot(xi,yi,'b-') ax.plot(x,y,'ko') plt.show()
and here points np.nonzero
:
[51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 45 46 47 48 49 50 68 69 70 71 72 73 74 75 40 41 42 43 44 76 77 78 36 37 38 39 79 80 34 35 32 33 31 30 30 29 28 28 27 27 27 27 27 27 27 27 26 26 26 26 26 26 27 27 27 27 27 27 27 28 28 29 30 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 56] [16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 17 17 17 17 17 17 17 17 17 17 17 17 17 17 18 18 18 18 18 18 18 18 19 19 19 19 19 19 20 20 21 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 58 59 59 60 61 61 61 62 62 62 63 64 64 65 65 66 67 68 69 70 71 72]
edit
to solve ordering problem, followed ev-br's suggestion , began end points. used series of possible end point orientations , ndimage.binary_hit_or_miss
function isolate end points. wrote function walked down skeleton checking each pixel's neighbors next in skeleton, moving one, , keeping running list. running list became set of ordered points looking for.
however, once accomplished after many hours of frustration (including entire half hour spent agonizing on problem solved replacing and
or
in 1 of if
statements), realized when interpolate these data points don't provide additional information. since data points collected walking along skeleton parametric in themselves, parametric analysis can use points. while figured out how order points, interpolation didn't turn out end goal anyways.
if wants see code wrote accomplish this, send me message , i'll gladly share.
as noticed, need order nonzero pixels. since you've skeletonized image, can start endpoint (whixh has 1 neighbor) , walk along path until reach other one. way, ordered list of pixel coordinates, interpolate. notice though question of how parameterize curve not trivial, , might need things beyond tossing in interp1d
one keyword searching internet "analyze skeleton".
Comments
Post a Comment