marcomusy / vedo

A python module for scientific analysis of 3D data based on VTK and Numpy
https://vedo.embl.es
MIT License
2.04k stars 265 forks source link

Why volume center is diffrent with 3DSlicer center #778

Closed zyy13579 closed 1 year ago

zyy13579 commented 1 year ago

I use vedo convert the nifti label volume to obj mesh,by this code

vol = load(label_path)
m = vol.isosurface(1)
write(m, obj_file_path)

but,the coverted obj file center,is diffrent with 3DSlicer convert obj file WX20230105-144752

So I try to change the volume center,but I saw the code of fucntion center is this

def center(self, center=None):
        """Set/get the volume coordinates of its center.
        Position is reset to (0,0,0)."""
        if center is not None:
            cn = self._data.GetCenter()
            self._data.SetOrigin(-np.array(cn) / 2)
            self._update(self._data)
            self.pos(0, 0, 0)
            return self
        return np.array(self._data.GetCenter())

the center paramter is not use. Is set center any problem?Thanks a lot

marcomusy commented 1 year ago

It looks difficult to understand for me what is happening because I cannot see the axes units.. I would try to shift the mesh produced by isosurface() by the desired amount (you can get the bounds of the volume with .bounds())

zyy13579 commented 1 year ago

I mean the mesh convert by vedo,have a diffrent coordinate with the 3dslicer covert, image origin has been changed I try to use vtkjs show the mesh(by vedo convert),and draw some measureline, so I find coordinate is not right,the center of vedo have a offset compare by slicer obj_file.zip this is my obj file all_vessel.nii.gz this is my label file(volume file)

marcomusy commented 1 year ago

Uhm , I'm not sure why you get a different a shifted and rotated image in slicer3d, it looks correct here:

from vedo import *
vol = Volume("all_vessel.nii")
iso = vol.isosurface()
iso.write("iso.obj")
iso2 = Mesh("iso.obj")  # read back
show(vol, iso, iso2, N=3, axes=1)
Screenshot 2023-02-02 at 14 26 53
zyy13579 commented 1 year ago

I think,when vedo load volume,the origin offset has been made. image slicer obj is ras coordinate,vedo obj is ijk coordinate? so I try to change center of volume by this code,but failed

label_img = sitk.ReadImage(volume_path)
raw_origin = label_img.GetOrigin()
print(raw_origin)
print(label_img.GetSpacing())
print(label_img.GetSize())
vol = Volume(volume_path)
print(vol.origin())
print(vol.spacing())
print(vol.dimensions())
print(vol.center())
vol = vol.origin(raw_origin)
vol = vol.center([0,0,0])
print(vol.origin())
print(vol.center())
print(vol.origin())
marcomusy commented 1 year ago

thanks for drawing my attention to it... it should be now fixed in the master:

pip install -U git+https://github.com/marcomusy/vedo.git

then

import SimpleITK as sitk
from vedo import Volume, show

volume_path = 'data/all_vessel.nii'

print(" ----- SimpleITK")
label_img = sitk.ReadImage(volume_path)
raw_origin = label_img.GetOrigin()
print(label_img.GetOrigin())
print(label_img.GetSpacing())
print(label_img.GetSize())

print(" ----- vedo")
vol = Volume(volume_path)
vol.origin(raw_origin)
vol.print()

iso = vol.isosurface().print()
show(vol, iso, N=2, axes=1)

Screenshot from 2023-02-03 15-22-47

zyy13579 commented 1 year ago

thanks a lot,great job!