MATLAB: Fitting a cilinder to 3D vertices

3d fittingpoint clouds

I am having problems fitting a cylinder to a point cloud. Trying to use "pcfitcylinder", an error saying 'the value of 'ptCloud' is invalid. Expected ptCloud to be a pointCloud object' while the pointcloud is a nx3 created from the vertices of an .stl file. The code is:
Bone_model=stlread(fullfile(Bone_path,Bone_file));
maxDistance=.5
cilinder = pcfitcylinder(Bone_model.vertices,maxDistance);
pcshow (Bone_model.vertices);hold on;
plot(cilinder)
Is there anything i'm doing wrong?

Best Answer

stlread() appears to be one of the functions from the File Exchange, probably https://www.mathworks.com/matlabcentral/fileexchange/22409-stl-file-reader
The vertices returned by that stlread() function are configured for use by patch(). pcfitcylinder() requires that a pointcloud object be passed in.
You might be able to use
pc = pointcloud(Bone_model.vertices);
[cilinder,inlierIndices] = pcfitcylinder(pc, maxDistance);
cylpc = select(cilinder, inlierIndices);
pcshow(cylpc)
Note that pcfitcylinder attempts to find a subset of the pointcloud that is in the form of an cylinder.