cornerstonejs / cornerstone-nifti-image-loader

Nifti image loader module for cornerstone/ohif viewer
MIT License
56 stars 27 forks source link

NIFTI files flipped left-right / coordinate system #17

Open tpenzkofer opened 3 years ago

tpenzkofer commented 3 years ago

Hi,

I have two - maybe related issues,

  1. I have some Nifti files (derived from DICOM using dcm2niix) which are flipped left-right in cornerstone when loaded through cornerstone-nifti-image-loader, any ideas?

  2. when calculating the patient space positions using "imagePointToPatientPoint" from cornerstoneTools using the imagePlane metaData information from cornerstone-nifti-loader the derived x/y coordinates are from the opposite corner of the image and the z coordinate is completely off (imagePointToPatientPoint does not take into account imageOrientationPatient, maybe that's the problem), as compared to Slicer RAS fiducial coordinates

Any ideas?

tpenzkofer commented 3 years ago

Specifically: The Patientposition matrix within cornerstone-nifti-image-loader ist different from slicers Image Origin (that shouldn't happen, imho)

cornerstone: coordinates1

slicer: Screen Shot 2020-07-25 at 12 30 45

tpenzkofer commented 3 years ago

ok, some more digging, the matrices / images are apparently flipped in these two function calls in Volume.js:

    this[convertToNeurologicalView]();
    this[convertRAStoLPS]();

Omitting them restores the correct imageOrientation matrix and ijk->patient space calculations are okay again BUT images are flipped bottom-up and left-right in cornerstone...

tpenzkofer commented 3 years ago

even more digging, looks like all three functions call

this[ensureVoxelStorageInXYZ]();
this[convertToNeurologicalView]();
this[convertRAStoLPS]();

are distorting the coordinate calculations when displaying it in cornerstone / doing ijk->patientspace calculations (I presume the when flipping the rotation matrix signs the origin coordinates need to be recalculated as well, especially in case of off-axis imaging), any thoughts on that?

ramonemiliani93 commented 2 years ago

Hi @tpenzkofer! Did you manage to solve this? I am facing the same problem with the loader, Nifti files appear to be flipped.

ramonemiliani93 commented 2 years ago

For anyone that runs into the same issue, it seems OHIF changed from Neurological to Radiological convention hence the image looks flipped on latest versions. The issue can be easily fixed by changing line 181 of the Volume.js file, instead of

      // if 'X-', we need to flip x axis so patient's right is
      // shown on the right
      if (senses[0] === '-') {
        matrix[0][0] *= -1;
        matrix[0][1] *= -1;
        matrix[0][2] *= -1;
        matrix[0][3] *= -1;
        steps[0] = -1;
      }
      // if 'Y+' we need to flip y axis so patient's anterior is shown on the
      // top
      if (senses[1] === '+') {
        matrix[1][0] *= -1;
        matrix[1][1] *= -1;
        matrix[1][2] *= -1;
        matrix[1][3] *= -1;
        steps[1] = -1;
      }
      // if 'Z+' we need to flip z axis so patient's head is shown on the top
      if (senses[2] === '+') {
        matrix[2][0] *= -1;
        matrix[2][1] *= -1;
        matrix[2][2] *= -1;
        matrix[2][3] *= -1;
        steps[2] = -1;
      }
    }

you can change the convention to the Radiological view by switching the line to

      // if 'X+', we need to flip x axis so patient's right is
      // shown on the left
      if (senses[0] === '+') {
        matrix[0][0] *= -1;
        matrix[0][1] *= -1;
        matrix[0][2] *= -1;
        matrix[0][3] *= -1;
        steps[0] = -1;
      }
      // if 'Y+' we need to flip y axis so patient's anterior is shown on the
      // top
      if (senses[1] === '+') {
        matrix[1][0] *= -1;
        matrix[1][1] *= -1;
        matrix[1][2] *= -1;
        matrix[1][3] *= -1;
        steps[1] = -1;
      }
      // if 'Z+' we need to flip z axis so patient's head is shown on the top
      if (senses[2] === '+') {
        matrix[2][0] *= -1;
        matrix[2][1] *= -1;
        matrix[2][2] *= -1;
        matrix[2][3] *= -1;
        steps[2] = -1;
      }
    }

Ideally the method should be renamed from convertToNeurologicalView to convertToRadiologicalView.