Created
March 26, 2014 14:04
-
-
Save kmader/9783822 to your computer and use it in GitHub Desktop.
Extract Skeleton From Image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
distSkel=MIJ.getCurrentImage; | |
% transform to a list | |
[xx,yy,zz]=meshgrid(1:size(distSkel,1),1:size(distSkel,2),1:size(distSkel,3)); | |
% find the non zero values inside objects | |
nonZeroPts=find(distSkel>0); | |
skelList=[xx(nonZeroPts) yy(nonZeroPts) zz(nonZeroPts)]; | |
%% plot the points in 3D (if a 3d image) | |
plot3(skelList(:,1),skelList(:,2),skelList(:,3),'b.') | |
%% plot them in 2D | |
plot(skelList(:,1),skelList(:,2),'r.') | |
%% apply a threshold | |
skelPrune=distSkel>8; | |
% perform a component labeling to identify the connected and disconnected structures | |
skelCL = bwlabel(skelPrune); | |
% find the largest component | |
compVoxCount=hist(skelCL(skelCL>0),1:max(skelCL(:))); | |
[maxSize,argmax]=max(compVoxCount); | |
% show just these objects | |
mainSkel=skelCL==argmax; | |
imagesc(mainSkel); | |
%% find junctions | |
juncImage=nlfilter(double(mainSkel),[3 3],@(x) sum(x(:))); | |
ljuncImage=zeros(size(juncImage)); | |
ljuncImage(juncImage==3)=2; % straight segments (3 voxels in neighborhood, in front, self and behind) | |
ljuncImage(juncImage<3)=1; % terminal segments | |
ljuncImage(juncImage>3)=3; % connections | |
ljuncImage(juncImage==0)=0; % set everything else to 0 | |
imagesc(ljuncImage) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment