Obtaining Segmentations for Fiducials

Operating system: Windows 10 Pro
Slicer version: 4.10 and / or 4.11
Hi all,

I have a set of fiducial points and a segmentation and I need to obtain the segments which the points belong to.
There are so many points that I need an automated way to do that.
How can I obtain the segments for each point using the Python interface ?
I can see the regions manually by pointing the mouse on them, but I don’t know how I can get them by code.

Thank you all and best regards.

See this example in the script repository:

Thanks !

Another related question : How can I obtain RAS coordinates of a segment ?
I mean not just the extends or boundaries, but the whole segment.

Thank you and best regards.

Sorry, I don’t understand what you mean. What would you like to do?

I mean obtaining the RAS coordinates the segments in a segmentation.

I can get the max / min RAS coordinates, but I need the coordinates of the whole 3-D region, in RAS coordinates.

“coordinates of the whole 3-D region” is undefined. A region’s boundaries have coordinates, the region’s center of gravity has coordinates, each non-zero voxel in the region has coordinates, etc. If you explain what you want to do with the data (maybe draw a sketch or annotate a screenshot) then it may make it more clear.

Let me try to define what I need :

Let’s assume we have a region in a segmentation. That region covers a 3-D region in the MRI space.
I need coordinates of the voxel in this 3-D, but not only in the MRI space, also in the RAS coordinates.

Or, if I can get the coordinates of all of the boundary points, that would also work well.

You can get all segment voxels as numpy array (using arrayFromVolume method), get IJK coordinates of non-zero voxels using standard numpy array operations (where, …),and convert IJK to RAS as shown in examples in the script repository (see here). You can also export segmentation to model and get coordinates of model points (see examples in script repository).

Although you can obtain all these data, this should be very rarely necessary. Are you sure you need coordinates of all these hundreds of thousands or millions of points? What is your overall goal?

I agree but I need this for a complicated project where the segmentation itself is not perfect…
I tried your suggestion but I have some problems - the resulting RAS coordinates are not correct yet.
Also, I get this warning :

arrayFromSegment is deprecated! Binary labelmap representation may be shared between multiple segments! Use arrayFromSegmentBinaryLabelmap to access a copy of the binary labelmap that will not modify the original labelmap. Use arrayFromSegmentInternalBinaryLabelmap to access a modifiable internal lablemap representation that may be shared between multiple segments.

I must be doing a mistake during IJK to RAS conversion, but I don’t know what it is.
I am using Slicer 4.11. Should I do anything different for this version ? Or is the code provided in


fine for 4.11 ?

Thank you so much.

This is just a deprecation warning. Since you don’t modify the segment, you can still use arrayFromSegment, but it would be more clear if you switched to arrayFromSegmentBinaryLabelmap function.

That’s fine - there is no such thing as a perfect segmentation. There are tools and best practices to manage that. If you tell what is your overall goal and what your problems are then we can give advice of what methods have been developed to address them.

Let me summariza what my overall goal is :

I need to assign some fiducial points to their corresponding segments. But since the segmentation has errors, some points, though they are quite close to the segment, are not assigned to their regions.
So I am trying to assign the points if they are close enough to a segment (say, 1mm away from the segment boundaries). This is why I need the RAS coordinates.
But if there is a built-in function in Slicer which can do these kind of things, that would work, too.

Thanks !

There are many ways to achieve this, but one simple way is to export the segmentation to a model node then get the closest point on the surface.

I also have models of the segmentation but the model seems even more imperfect than the segmentation.

Is there a way to do this with the segmentation itself ?

The model that Segmentations module generates from the segment should not be worse. Maybe you can smooth the binary labelmap representation and disable smoothing for the surface generation to make the generated surface more closely match the labelmap. Can you attach an example image so that we have a better idea of the difficulty of the problem?

Here is an example. I think the model boundaries aren’t really close enough to the segmentation itself.
Also in some cases, two neighboring models cross each other , i.e. one single point seems to belong to two different models at the same time.

This segmentation looks good. There is no significant noise or other issues.

You can reduce or disable surface smoothing to make the generated surface match the labelmap more closely.

I don’t know how to disable surface smoothing to generate the model, could you help me with that ?

See Segment editor documentation page (look for “smoothing”).

Thank you for all the information !
I found the smoothing settings.

But when I try to view new models I cannot see them, even though the visibilty box is checked. What might cause that ?

And I also obtained the minimum distances. But some of the distances are negative. Should I take their absolute value ?

Thanks !

Computaiton of closed surface representation takes time, so it is not created unless it is requested.
You can create it in Segmentations module by clicking “Create” button in the corresponding row in Representations section (or in Segment Editor module click “Show 3D”).

Negative distance means inside, positive distance means outside.