# Cube Segments / Conversion of ROIs into Segments

**URL:** <https://discourse.slicer.org/t/cube-segments-conversion-of-rois-into-segments/32350>\
**Category:** Support\
**Tags:** segmentation, cubic-rois\
**Created:** [October 20, 2023, 4:34pm UTC](https://discourse.slicer.org/t/cube-segments-conversion-of-rois-into-segments/32350 "2023-10-20T16:34:15Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dokay1](https://avatars.discourse-cdn.com/v4/letter/d/b4bc9f/32.png) [@dokay1](https://discourse.slicer.org/u/dokay1)\
**Post date:** [October 20, 2023, 4:34pm UTC](https://discourse.slicer.org/t/cube-segments-conversion-of-rois-into-segments/32350/1 "2023-10-20T16:34:15Z")

</div>

Dear Slicers,

I have a project where I want to place a number of 2x2x2cm voxels on brain scans to imitate MR Spectroscopy voxel placement. It seems that using ROIs to generate these is the best approach, however, I couldn’t find an efficient way to convert these CUBIC ROIs into Segments/Label maps.

Is there a solution for this?

Thank you!

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/4/a/4aaebae710af6ba1245435f9e5768cbdd7b145d2.jpeg)

---

<div class="post-metadata">

**Author:** ![dokay1](https://avatars.discourse-cdn.com/v4/letter/d/b4bc9f/32.png) [@dokay1](https://discourse.slicer.org/u/dokay1)\
**Post date:** [October 21, 2023, 12:40am UTC](https://discourse.slicer.org/t/cube-segments-conversion-of-rois-into-segments/32350/2 "2023-10-21T00:40:50Z")

</div>

Well, figured it out. You create a template Segment using thresholding 0-to-max (i.e. to select all voxels). Convert the segment to a labelmap. Use this labelmap with Crop Volume module and use the ROI for cropping, and there you go. A labelmap with shaped exactly as the ROI.

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [October 21, 2023, 4:07am UTC](https://discourse.slicer.org/t/cube-segments-conversion-of-rois-into-segments/32350/3 "2023-10-21T04:07:00Z")

</div>

Flood fill effect in aegment editor extra effects extension takes an ROi.

That may save you a couple steps.

---

<div class="post-metadata">

**Author:** ![dokay1](https://avatars.discourse-cdn.com/v4/letter/d/b4bc9f/32.png) [@dokay1](https://discourse.slicer.org/u/dokay1)\
**Post date:** [October 21, 2023, 12:34pm UTC](https://discourse.slicer.org/t/cube-segments-conversion-of-rois-into-segments/32350/4 "2023-10-21T12:34:40Z")

</div>

Neat! Thank you!

I tested it (see below). It seems like the Flood Fill provides an approximation of the ROI, while Crop will make it match the ROI’s exact dimensions.

Probably the scalable solution will be a python script for Batch Cropping. I’ll share once I made it.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/1/6164202311784ee86c71cecb0be0dd1394039854.jpeg)

Thank you again!

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [October 21, 2023, 8:10pm UTC](https://discourse.slicer.org/t/cube-segments-conversion-of-rois-into-segments/32350/5 "2023-10-21T20:10:06Z")

</div>

To me, they align quite well. The extra space outside of the ROI is due your spacing. Segmentation has to follow the voxel boundaries.

If you want to ROI boundary and the segmentation to align more precisely, you can increase the resolution of the segmentation (but of course that will also increase the memory consumption).

(purple is output of flood will, red box is the ROI).

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/4/3/43e5bef2e202eb3ce9c21c1c74fbb7f3e2687b76.jpeg)

---

<div class="post-metadata">

**Author:** ![muratmaga](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/muratmaga/32/3622_2.png) [@muratmaga](https://discourse.slicer.org/u/muratmaga)\
**Post date:** [October 21, 2023, 8:15pm UTC](https://discourse.slicer.org/t/cube-segments-conversion-of-rois-into-segments/32350/6 "2023-10-21T20:15:04Z")

</div>

What is doesn’t work well with the rotated ROIs though.

@lassoan is it possible to add this feature?

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/0/d/0d4f03c26de040e7f45f34d655dc3352ff4083f3.jpeg)

---

<div class="post-metadata">

**Author:** ![chir.set](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chir.set/32/66982_2.png) [@chir.set](https://discourse.slicer.org/u/chir.set)\
**Post date:** [October 21, 2023, 8:43pm UTC](https://discourse.slicer.org/t/cube-segments-conversion-of-rois-into-segments/32350/7 "2023-10-21T20:43:14Z")

</div>

> [@muratmaga](#):
>
> doesn’t work well with the rotated ROIs

You may have a look at a custom module, [Markups to surface,](https://gitlab.com/chir-set/Tools7/-/tree/master/MarkupsToSurface/) that can handle a rotated ROI. It could probably fit this requirement.

---

<div class="post-metadata">

**Author:** ![dokay1](https://avatars.discourse-cdn.com/v4/letter/d/b4bc9f/32.png) [@dokay1](https://discourse.slicer.org/u/dokay1)\
**Post date:** [October 22, 2023, 1:02am UTC](https://discourse.slicer.org/t/cube-segments-conversion-of-rois-into-segments/32350/8 "2023-10-22T01:02:37Z")

</div>

But then It would still have to be converted to a segment or label map. So it’s 1 step less to just have a hi-resolution template file to Crop to the dimensions of the ROI.

---

<div class="post-metadata">

**Author:** ![dokay1](https://avatars.discourse-cdn.com/v4/letter/d/b4bc9f/32.png) [@dokay1](https://discourse.slicer.org/u/dokay1)\
**Post date:** [October 22, 2023, 2:37am UTC](https://discourse.slicer.org/t/cube-segments-conversion-of-rois-into-segments/32350/9 "2023-10-22T02:37:57Z")

</div>

OK, created a script (using chat GPT).

It uses the Template volume (“TEMPLATE\_for\_CROPPING”), and then the ROIs are named V1…10. And then the script iterates on the ROIs and generates Cropped\_V1…Cropped\_V10 scalar volumes. Simple but useful. My project is expected to have few hundreds of. Voxels, so this can save substantial amounts of time.

> # Initialize CropVolume logic
> 
> cropVolumeLogic = slicer.modules.cropvolume.logic()
> 
> # Get the volume node for the template
> 
> templateVolumeNode = slicer.util.getNode(‘TEMPLATE\_for\_CROPPING’)
> 
> # Loop through ROI nodes V1 to V10
> 
> for i in range(1, 11): # Adjust the range based on your actual ROI names  
> roiName = f"V{i}"  
> roiNode = slicer.util.getNode(roiName)
> 
> ```
> if roiNode: # Only proceed if the ROI node actually exists
> # Create a new CropVolumeParameters Node for each iteration
> cropVolumeNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLCropVolumeParametersNode')
>     
> # Set the input volume and ROI for cropping
> cropVolumeNode.SetInputVolumeNodeID(templateVolumeNode.GetID())
> cropVolumeNode.SetROINodeID(roiNode.GetID())
>     
> # Apply cropping
> cropVolumeLogic.Apply(cropVolumeNode)
>     
> # Get the output volume node
> outputVolumeNode = slicer.mrmlScene.GetNodeByID(cropVolumeNode.GetOutputVolumeNodeID())
>     
> # Rename the output volume node to match the ROI with 'Cropped_' prefix
> outputVolumeNode.SetName(f"Cropped_{roiName}")
> 
> # Optionally, save the cropped volume to disk
> # outputPath = f"C:/path/to/save/Cropped_{roiName}.nrrd"
> # slicer.util.saveNode(outputVolumeNode, outputPath)
> 
> # Remove the cropVolumeNode to avoid potential conflicts in the next iteration
> slicer.mrmlScene.RemoveNode(cropVolumeNode)
> 
> ```
