MATLAB: Image Processing – Finding Length and Diameter of a Curved Object.

image analysisimage processingImage Processing Toolbox

I have a curved wire, and I am trying to find its contour length and average diameter.
I have already imported the image and converted it to a binary matrix. I then cleaned up the image with bwareaopen, imfill, and imopen. I found the perimeter of the wire with bwperim and I hope to now find the centerline of the wire. This is where I am having trouble. I hope to calculate the contour length of the centerline to get the length of the wire, and will use the difference between the perimeter lines and the centerline to find radius.
I have attempted to use bwmorph(IMG,'skel') and bwmorph(IMG,'thin) to find the centerline, however I havn't found a method of properly trimming the branches without affecting the centerline. (I have tried bwmorph(IMG,'spur) I end up with the image below after using the thin function.
Both of the bwmorph images appear to have gaps however they are continuous.
I have seen forum users such as Image Analyst suggest taking the skeleton and multiplying it by the euclidean distance transform of the original image to find the distances however I haven't been able to get that method to work either.
Any advice would be great. I have been stuck on this all week.
Thanks,
Kevin

Best Answer

For a long skinny object, you can get the length by getting the perimeter and dividing by 2, which is a fair guess for thin things but less accurate as the object gets thicker. Then the diameter is just the area divided by the length.
The gaps you see are most likely just due to subsampling for display - like you said they're really continuous.
The thinned object looks pretty good. Maybe you can get the length just by summing the binary image to add up all the white pixels. Then the width (diameter) is the full area divided by the length.
If you really need the diameter as a function of length, or if you want a histogram of different diameters, then you need to use the method you said I mentioned - multiplying the EDT by the thinned or skeletonized image. You wouldn't need to do this for your image since the width is pretty uniform. If your object is really lumpy and you wanted all the diameters then you'd have to do that, but I don't think you need to in your case.