MrBean has already answered this really well, but I thought I'd elaborate a bit more on your question.
You say: 'What I specifically am curious about is how I can take an array [0.5,0,-0.8660254,0,1,0] and be able to say it is a sagittal view'
The sagittal, axial, coronal views are, if you want to be strict about it, only those along the axes of the patient. So, as you point out, only those with all 1's or 0's in the IOP vectors.
So, for your example above, you *couldn't say [0.5,0,-0.8660254,0,1,0] is sagittal, as it's cross product isn't strictly in-line with any of the unit vectors.
But what everyone does, and what Roni in his great post somewhat says, is that you can say a particular orientation is more, or closest to axial, or sagittal or coronal, and call it that.
Or, you could put a tolerance around each of the unit views and only call it coronal or axial or sagittal if it's within that tolerance of the unit vectors - and otherwise say it's undefined. (you can think of this tolerance being like an angle - so, for example, if it's with 10 degrees of a unit vector).
So MrBean's code above is actually doing just that - for a given input IOP it's finding the closest unit vector to the normal (note the call to max
):
main_index = list(abs_image_z).index(max(abs_image_z))
In my code, I use this definition regularly, so made my own method to calculate it. Note, like MrBean's code, it also will find the 'closest' orientation, iop_rounded = [round(x) for x in iop]
. This will round all values to 0 or 1, and so at the end of the day, is equivalent to what MrBean used.
def get_orientation(dcm: DcmObj) -> str:
"""Get orientation of dicom object.
Returns one of
Transverse
Coronal
Sagittal
NA (if ImageOrientationPatient not available)
"""
iop = getattr(dcm, 'ImageOrientationPatient', None)
if iop is None:
return 'NA'
iop_rounded = [round(x) for x in iop]
plane_cross = np.cross(iop_rounded[0:3], iop_rounded[3:6])
plane = [abs(x) for x in plane_cross]
if plane[0] == 1:
return 'Sagittal'
elif plane[1] == 1:
return 'Coronal'
elif plane[2] == 1:
return 'Transverse'
else:
return 'NA'