MATLAB Answers

Somehow my code doesn't work and I don't know why

1 view (last 30 days)
charles on 2 Sep 2017
Answered: charles on 5 Sep 2017
I'm writing an algorithm to succesfully segment 3D images of roots in soil. I have to seperate the main root from the first order branches and so on. In order to do so I have made a skeleton, which I used to find the junctions between branches. I use these points as the centre of a growing logical sphere which I multiply with the original binary (tresholded) image. As soon as there are 2 objects in my image, the sphere needs to stop growing so I can segment the image.
I wrote this code to do this:
mid_point = {31,38,39};
number_of_objects = 1;
count = 1;
while(number_of_objects < 2)
volume = zeros(dimx,dimy,dimz);
B = imdilate(volume,strel('sphere', count) );
new_im = (g_thres.*abs(1-B));
[a number_of_objects] = bwlabeln(new_im);
count = count+1;
The dimensions are 100*100*80, however it will also be used on way bigger images (1000*1000*1000), so using a for loop isn't really an option I think.
When I run the code, after 30 minutes there are still no results and than I terminate it, since it isn't usefull if it is that slow. Can somebody please explain to me why this code doesn't work (or is at least very slow).
Thank you very much,

Accepted Answer

charles on 5 Sep 2017
Worked it out myself.
sphere = zeros(dimx,dimy,dimz);
B = bwdist(sphere) <= count;
I put this in a while loop which increased count at every iteration. This way the sphere would become bigger every iteration untill my criteria are met.

More Answers (1)

Image Analyst
Image Analyst on 2 Sep 2017
Try attaching a small data file so we wan start working on solving this for you. And let us know whether the foreground is black or white, because the algorithm doesn't make sense to me. And add some comments.
One way might be to identify endpoints, then use a shortest path algorithm, bwdistgeodesic(), to find the longest root. See Steve's 5 part blog series on this:
  1 Comment
charles on 2 Sep 2017
The foreground is white and the background is black. I inverse the image, obtained with imdilate, in order to substract the sphere from the original image.
I don't think a method which needs endpoints isn't really a viable option since there can be hundreds of endpoint in a single image.
Last of all I have no idea how I could get a small enough data file to uploud. The root that I have has a width of atleast 80 pixels, therefor I thing cannot make a file which is less than 5 MB and still has enough information in it the analyse. Everything I'm doing is self thaught so maybe I'm wrong about this.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!