# What's wrong when trying to convert a model's coordinate points into a volume?

**URL:** <https://discourse.slicer.org/t/whats-wrong-when-trying-to-convert-a-models-coordinate-points-into-a-volume/33791>\
**Category:** Development\
**Tags:** python\
**Created:** [January 16, 2024, 5:36am UTC](https://discourse.slicer.org/t/whats-wrong-when-trying-to-convert-a-models-coordinate-points-into-a-volume/33791 "2024-01-16T05:36:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [January 16, 2024, 5:36am UTC](https://discourse.slicer.org/t/whats-wrong-when-trying-to-convert-a-models-coordinate-points-into-a-volume/33791/1 "2024-01-16T05:36:46Z")

</div>

1. First, create a new volume and obtain the coordinates of the model based on it.
2. Convert the coordinates of the model (`RAS`) to `IJK` (from: [ (slicer.readthedocs.io)](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#get-volume-voxel-coordinates-from-markup-control-point-ras-coordinates))

```python
def ras2IjkFor(ps, reVol):
  mat = vtk.vtkMatrix4x4()
  reVol.GetRASToIJKMatrix(mat)
  ijks= []
  for p in ps:
    ijk = [0, 0, 0, 1]
    mat.MultiplyPoint(np.append(p,1.0), ijk)
    ijk = [int(round(c)) for c in ijk[0:3] ]
    ijks.append(ijk)
  return np.asarray(ijks)

```

1. Then, according to `IJK` staining

```python
ps = slicer.util.array(mod)
reVol = slicer.util.getNode(reVol)
reArr = slicer.util.array(reVol)
ijk = ras2IjkFor(ps, reVol)
mArr = np.zeros_like(reArr)
z = ijk[:, 0].astype(int)
y = ijk[:, 1].astype(int)
x = ijk[:, 2].astype(int)
mArr[x, y, z] = lb
slicer.util.addVolumeFromArray(mArr)

```

but …

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/9/3/937f1245d9b7179aca3c9c4402eac0f4113af275.png)

The distance between the red vertebral model and the newly generated volume is so outrageous. what’s wrong??? @lassoan@pieper@Juicy@jamesobutler @jcfr

> **[Google Drive: Sign-in](https://accounts.google.com/v3/signin/identifier?continue=https%3A%2F%2Fdrive.google.com%2Ffile%2Fd%2F1_lRorqdx9SIxkj8DGV35d6pkGowa1GI8%2Fview%3Fusp%3Dsharing&followup=https%3A%2F%2Fdrive.google.com%2Ffile%2Fd%2F1_lRorqdx9SIxkj8DGV35d6pkGowa1GI8%2Fview%3Fusp%3Dsharing&ifkv=ASKXGp3q-v3QSvNhPTtiI0vc5PCm5oIqwL8_dGDjzGKOHMbW2MV99146Gb_X7if881K1v2BLKAUj5Q&osid=1&passive=1209600&service=wise&flowName=GlifWebSignIn&flowEntry=ServiceLogin&dsh=S1543613071%3A1705384969877507&theme=glif)**
>
> Access Google Drive with a Google account (for personal use) or Google Workspace account (for business use).

---

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [January 17, 2024, 9:55pm UTC](https://discourse.slicer.org/t/whats-wrong-when-trying-to-convert-a-models-coordinate-points-into-a-volume/33791/2 "2024-01-17T21:55:39Z")

</div>

@lassoan@pieper@Juicy@jamesobutler @jcfr

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [January 17, 2024, 11:16pm UTC](https://discourse.slicer.org/t/whats-wrong-when-trying-to-convert-a-models-coordinate-points-into-a-volume/33791/3 "2024-01-17T23:16:33Z")

</div>

You have found the correct code snippet in the script repository. It works well, you can use it as is.

I cannot decypher what you intend to do from your code sample, but if you want to paint a small spheres in a volume at markup point positions then I would recommend to create a model that contains a small sphere at each markup position and then import that into a segmentation as it is shown here:

[https://slicer.readthedocs.io/en/latest/developer\_guide/script\_repository.html#create-segmentation-from-a-model-node](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#create-segmentation-from-a-model-node)

Another example where several spheres are added (it is more efficient to append all the spheres into a single polydata and add it as one segment if you have many points):

> <https://gist.github.com/lassoan/2d5a5b73645f65a5eb6f8d5f97abf31b>

---

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [January 17, 2024, 11:58pm UTC](https://discourse.slicer.org/t/whats-wrong-when-trying-to-convert-a-models-coordinate-points-into-a-volume/33791/4 "2024-01-17T23:58:04Z")

</div>

@lassoan I have also seen the [snippet](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#create-segmentation-from-a-model-node) you provided and it was very successful, but I want to achieve the conversion from coordinate array to pixel array Because I want to convert [the model](https://drive.google.com/file/d/1WMobSpaHQpKQ39mgrt5kFknWVNIjpLB5/view?usp=drive_link) or the array to a volume. Teacher, help me take a look Or perhaps there is something wrong with the code I wrote?

```python
def mod2Vol(mod, rVol=None, lbl = 1, mNam=''):
  mod = slicer.util.getNode(mod)
  if rVol is None:
    rVol = slicer.mrmlScene.GetFirstNodeByClass('vtkMRMLScalarVolumeNode')
  else:
    rVol = slicer.util.getNode(rVol)
  seg = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")
  seg.SetReferenceImageGeometryParameterFromVolumeNode(rVol)
  slicer.modules.segmentations.logic().ImportModelToSegmentationNode(mod, seg)
  seg.CreateBinaryLabelmapRepresentation()
  vol = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLLabelMapVolumeNode", mNam)
  slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(seg, vol, rVol)
  segArr = getArr(vol)
  Helper.nodsDel(seg)
  return sicer.util.updateVolumeFromArray(vol, segArr*lbl)

# vol = mod2Vol('pjMod')

```

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/b/2/b2635f30f25a5c284f85293b880cf4c1b1ffbd5a.png)

👆🏻 Um Yes, I used the `vtk.vtkProjectPointsToPlane()` method to press(or project) the vertebral body model into a plane along a certain direction…and then run `mod2Vol('pjMod')`, get `a none volume`👇.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/8/1/81432514c65a7712c01936757bd5acea71a3f78c.png)

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [January 18, 2024, 3:10am UTC](https://discourse.slicer.org/t/whats-wrong-when-trying-to-convert-a-models-coordinate-points-into-a-volume/33791/5 "2024-01-18T03:10:20Z")

</div>

> [@jumbojing](#):
>
> Because I want to convert [the model](https://drive.google.com/file/d/1WMobSpaHQpKQ39mgrt5kFknWVNIjpLB5/view?usp=drive_link) or the array to a volume

You can export the segmentation to a labelmap volume.

---

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [January 18, 2024, 6:27am UTC](https://discourse.slicer.org/t/whats-wrong-when-trying-to-convert-a-models-coordinate-points-into-a-volume/33791/6 "2024-01-18T06:27:02Z")

</div>

> [@jumbojing](#):
>
> `egmentations.logic()`

`slicer.modules.segmentations.logic().ExportVisibleSegmentsToLabelmapNode(seg, vol, rVol)`, 👈🏻It seems that there is a problem with this sentence, Too thin to converted to? How to convert with code??  
@lassoan

---

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [January 18, 2024, 11:59am UTC](https://discourse.slicer.org/t/whats-wrong-when-trying-to-convert-a-models-coordinate-points-into-a-volume/33791/8 "2024-01-18T11:59:09Z")

</div>

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/8/c/8c4a350a01153b865b73f7cd32f0d8c395570692.jpeg)

可算对齐了!
