unexpected axes orientation in volshow()

23 views (last 30 days)
Matlab documentation recommends using volshow() to display nifti-format images. volshow() does NOT map the indices i,j,k to the x,y,z axes, contrary to the nifti format specification. Have others noticed this? Do you see it as a problem? Could this cause medical scans to display in mirror image from what they should be?
The nifti format specification (nifti1.h, here) says that the indices i,j,k in V(i,j,k) map to x,y,z respectively, by default. (This is called Method 1 in lines 1038-1040 of nifti1.h.) Since I have not specified otherwise, I expect the default method to be used. Furthermore, if I do specify a mapping, using the an affine transform with the identity matrix, i,j,k should map to x,y,z respectively, but they do not. This method is called Method 3 in lines 1101-1103 of the nifti1.h specification. In case you're wondering, the nifti1.h file also explains at lines 1008-1011 that the first, second,third array indices (i,j,k) should change fastest, medium, slowest respectively, as the array is stored in memory. And that is how Matlab array indices work.
Whether I use Method 1 or 3, index i maps to y and index j maps to x, contrary to expectation.
The code below demonstrates this by making and plotting a 3D scan. The volume V is 99x99x99. It has three lines from the center leading out to the centers of the imax face, the jmax face, and the kmax face. At the max end of each line, there are different size squares, to allow us to tell which is which. The imax face has a small square, the jmax face has a large square, and the kmax face has a medium square. I expect these to be at the +x, +y, and +z axes, respectively, but the large and small are reversed (x-y) from what I expect. This is not just a rotation of the data, it is a reflection, since data expected at +x and +y are flipped. See below.
Other things I've tried:
  • Write the volume to a nifti file, then read it in, then display it. Same result.
  • Define tform=affinetform3d(eye(4)); and use it: volshow(V,Transformation=tform);. Same result.
I realize that I can use an affine transformation (reflection) S=[0,1,0,0; 1,0,0,0; 0,0,1,0; 0,0,0,1] to make this look like I expected. But I think I shouldn't have to, or it should be documented.
Since volshow() does not display in Matlab Answers, I have attached a screenshot of the displayed volume that results from the code below.
imax=99; jmax=99; kmax=99; % volume dimensions
V=zeros(imax,jmax,kmax); % allocate array
% draw line from cube center to center of imax face
V(50:imax,50,50)=ones(50,1,1);
% draw line from cube center to center of jmax face
V(50,50:jmax,50)=ones(1,50,1);
% draw line from cube center to center of kmax face
V(50,50,50:kmax)=ones(1,1,50);
% small (9x9) square at center of imax face (imax,jmid,kmid)
V(imax,46:54,46:54)=ones(1,9,9);
% large (21x21) square at center of jmax face (imid, jmax, kmid)
V(40:60,jmax,40:60)=ones(21,1,21);
% medium (15x15) square at center of kmax face (imid, jmid, kmax)
V(43:57,43:57,kmax)=ones(15,15,1);
% Display simulated scan
volshow(V);

Accepted Answer

Tim Jackman
Tim Jackman on 10 Jan 2026
This is a valid concern. volshow itself is given a brick of data without knowledge of where it came from and follows a longstanding convention in the Image Processing Toolbox, but this puts a burden on people working with data that follows a different convention.
We recommend that users with medical data use medicalVolume to read their data:
and then pass that medicalVolume into volshow:
This extends volshow and brings in the metadata from the volume so the data can be displayed correctly in the patient coordinate system.
  3 Comments
Stephen23
Stephen23 on 11 Jan 2026
Edited: Stephen23 on 11 Jan 2026
"I do recommend that Mathworks update the documentation for volshow()"
Then tell TMW this directly:
Open the VOLSHOW webpage, scroll to the very bottom of the page to "How useful was this information?", then provide a rating and a comment explaining exactly how the documentation should be improved.
William Rose
William Rose on 12 Jan 2026 at 18:35
@Stephen23, thank you. I did what you recommended.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!