How to iterate through a model () based on pixels?
I want to find the largest inscribed circle based on this. Like this way or this by cv2 .
How to iterate through a model () based on pixels?
I want to find the largest inscribed circle based on this. Like this way or this by cv2 .
You can get model point positions as a numpy array using
pointArray = slicer.util.arrayFromModelPoints(modelNode)
Note that you cannot implement algorithms in Python that iterate through tens of thousands of points, because it will be extremely slow. You need to work with high-level functions that operate on the entire array.
The first link you described performs an exhaustive search using trial and error. It will be very slow, especially in 3D.
The second method, based on distance transform, should work. There is no need to mess with OpenCV (it is a very big and problematic package), but you can export the segmentation to label volume and use Simple Filters module and use any of the distance map filters to compute the distance map.
You can also use Extract Centerline module in SlicerVMTK extension to get all the minmum inscribed spheres for your shape (along with the radius values). If you need only a single radius value then you can use the maximum radius value (and the corresponding centerline point position). If your object is not spherical (as in your example above) then it might make sense to look at more centerline points (e.g., all local maxima points), because a single sphere may not be a good representation of your object shape.
? but…How to get the maximum inscribed sphere model by code.
Extract Centerline module gives you a centerline model or curve. You get the computed radii as a numpy array (slicer.util.arrayFromMarkupsCurveData()
), get the index of the point that has the largest radius value, and get the position of that point (slicer.util.arrayFromMarkupsCurvePoints()
).
It may be even simpler to use the Voronoi diagram that Extract centerline module can also provide you. What you get is model node contains all the centerpoints of the inscribed spheres that can be inscribed in your mesh. You can get the radii values as a numpy array (slicer.util.arrayFromModelPointData()
), find the index of the maximum value, and get the position of that point (slicer.util.arrayFromModelPoints()
).
Give it a try and let us know if you get stuck at any point.