# Rotate X-axis by 90 degrees

**URL:** <https://community.mrtrix.org/t/rotate-x-axis-by-90-degrees/796>\
**Category:** Uncategorized\
**Created:** [March 26, 2017, 8:02am UTC](https://community.mrtrix.org/t/rotate-x-axis-by-90-degrees/796 "2017-03-26T08:02:05Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![david142](https://community.mrtrix.org/letter_avatar_proxy/v4/letter/d/71e660/32.png) [@david142](https://community.mrtrix.org/u/david142)\
**Post date:** [March 26, 2017, 8:02am UTC](https://community.mrtrix.org/t/rotate-x-axis-by-90-degrees/796/1 "2017-03-26T08:02:05Z")

</div>

Hi! First of all, thank you once again for this amazing program!

I have one question. How can I rotate ONLY X-axis, and by only 90 degrees.

We have acquired a bunch of images with bruker machine, and somehow mrview and fslview just show me wrong sections. For example: where there should be axial, there is corronal section, and in saggital section, it is flipped AP.

I have noticed that when I rotate (using DSI studio) x axis by 90 degrees, everything turns out fine.  
Same thing happens when I convert in ITK-snap orientation from LPI to LIA.  
Basically, if I got it right, rotating x axis by 90, is same as doing swap y and z, and than inverting y?

I would like to know how can I just do that in mrtrix more quickly. Thanks in advance!

---

<div class="post-metadata">

**Author:** ![rsmith](https://community.mrtrix.org/user_avatar/community.mrtrix.org/rsmith/32/2672_2.png) [@rsmith](https://community.mrtrix.org/u/rsmith)\
**Post date:** [March 27, 2017, 2:43am UTC](https://community.mrtrix.org/t/rotate-x-axis-by-90-degrees/796/2 "2017-03-27T02:43:28Z")

</div>

Hi David,

When it comes to erroneous importing of images, personally I tend to think about it in terms of axes being permuted or flipped, as opposed to rotations around axes. This is also probably the easier way to apply the corrections in _MRtrix3_:

- `mrconvert` has the `-axes` option, which enables (among other things) permutation of axes. So for instance, to switch the Y and Z axes, you would use: `mrconvert -axes 0,2,1`.

- `mrtransform` has the `-flip` option, which enables flipping of a specific axis.

Play with the combination of these two until you get the correct result.

The other, less “friendly” approach, is to construct the appropriate transformation matrix in a text editor, and apply it using: `mrtransform -linear`. However you need to be confident in handling such data (and figuring out what to change if you don’t get it right the first time) if you want to try this approach.

Cheers  
Rob

---

<div class="post-metadata">

**Author:** ![david142](https://community.mrtrix.org/letter_avatar_proxy/v4/letter/d/71e660/32.png) [@david142](https://community.mrtrix.org/u/david142)\
**Post date:** [March 29, 2017, 6:21pm UTC](https://community.mrtrix.org/t/rotate-x-axis-by-90-degrees/796/3 "2017-03-29T18:21:54Z")

</div>

Hi Robert, thank you for your fast answer!

After trying what you have recommended I get an error:

“[ERROR]: ommited axis “3” has dimension greater than 1”

Really in front of a wall right now 😕

---

<div class="post-metadata">

**Author:** ![maxpietsch](https://community.mrtrix.org/user_avatar/community.mrtrix.org/maxpietsch/32/25_2.png) [@maxpietsch](https://community.mrtrix.org/u/maxpietsch)\
**Post date:** [March 29, 2017, 7:57pm UTC](https://community.mrtrix.org/t/rotate-x-axis-by-90-degrees/796/4 "2017-03-29T19:57:48Z")

</div>

Change

`mrconvert -axes 0,2,1`  
to  
`mrconvert -axes 0,2,1,3`

to switch the Y and Z axes if you have 4D images.

Cheers,  
Max

---

<div class="post-metadata">

**Author:** ![jdtournier](https://community.mrtrix.org/user_avatar/community.mrtrix.org/jdtournier/32/2594_2.png) [@jdtournier](https://community.mrtrix.org/u/jdtournier)\
**Post date:** [March 29, 2017, 8:03pm UTC](https://community.mrtrix.org/t/rotate-x-axis-by-90-degrees/796/5 "2017-03-29T20:03:09Z")

</div>

That’s simply because `mrconvert` needs to know how to re-order all _non-singleton_ axes. This is presumably a 4D image, but you’ve only supplied 3 axes for the output - so `mrconvert` reports that you’ve omitted one axis. The reason for the slightly cryptic message is that if the 4th dimension had size one (e.g. `dim: 128 x 128 x 90 x 1`), it would correspond to a single 3D volume, and `mrconvert` would treat it as such, and allow you to omit that axis. Might need to think of a slightly less confusing way of phrasing that…

In any case, try `mrconvert -axes 0,2,1,3` instead.

---

<div class="post-metadata">

**Author:** ![maxpietsch](https://community.mrtrix.org/user_avatar/community.mrtrix.org/maxpietsch/32/25_2.png) [@maxpietsch](https://community.mrtrix.org/u/maxpietsch)\
**Post date:** [March 29, 2017, 8:11pm UTC](https://community.mrtrix.org/t/rotate-x-axis-by-90-degrees/796/6 "2017-03-29T20:11:50Z")

</div>

Alternatively, as Rob said, rotate the header transformation with `mrtransform -linear trafo.txt input.mif output.mif`.

Content of trafo.txt should be something like:

```auto
1 0 0 0
0 0 -1 0
0 1 0 0
0 0 0 1
```

or the transpose of that.

Some kind of “Donald is typing…” [symbol](https://i.ytimg.com/vi/Wa3IO6pBT1E/hqdefault.jpg) would be useful for the forum 😉

---

<div class="post-metadata">

**Author:** ![jdtournier](https://community.mrtrix.org/user_avatar/community.mrtrix.org/jdtournier/32/2594_2.png) [@jdtournier](https://community.mrtrix.org/u/jdtournier)\
**Post date:** [March 29, 2017, 8:15pm UTC](https://community.mrtrix.org/t/rotate-x-axis-by-90-degrees/796/7 "2017-03-29T20:15:39Z")

</div>

> [@maxpietsch](#):
>
> Some kind of “Donald is typing…” symbol would be useful for the forum

😂 I thought the same when I saw your post (after I’d hit ‘reply’, obviously…).

---

<div class="post-metadata">

**Author:** ![david142](https://community.mrtrix.org/letter_avatar_proxy/v4/letter/d/71e660/32.png) [@david142](https://community.mrtrix.org/u/david142)\
**Post date:** [March 31, 2017, 4:26pm UTC](https://community.mrtrix.org/t/rotate-x-axis-by-90-degrees/796/8 "2017-03-31T16:26:20Z")

</div>

Guys what can I say… You solved my problem!!

There are no words how can I express how thankfull I am!! You really spared me headaches and troubles!!

Keep up with the good work 🙂

---

<div class="post-metadata">

**Author:** ![Bauke](https://community.mrtrix.org/letter_avatar_proxy/v4/letter/b/839c29/32.png) [@Bauke](https://community.mrtrix.org/u/Bauke)\
**Post date:** [May 4, 2021, 11:22am UTC](https://community.mrtrix.org/t/rotate-x-axis-by-90-degrees/796/9 "2021-05-04T11:22:59Z")

</div>

Dear all,

I would like to add a question regarding rotating around the x-axis. I am able to do a 90 degree rotation with the matrix maxpietsch provided in the above discussion. If I plot the input and the rotated image with mrview, you can nicely see a 90 degree rotation.

However, I want to do a 45 degree rotation around the x-axis. Therefore, I created the following matrix:

1 0 0 0  
0 0.70710678 -0.70710678 0  
0 0.70710678 0.70710678 0  
0 0 0 1

However, if I use this matrix to rotate an image with mrtransform, the output image is still in the same position as the input image when I plot it with mrview. I do however see that the transform matrix in the header has changed. So there seems to be happening something different than with the 90 degree rotation. I do not understand why. Is it possible to do a rotation of 45 degrees while keeping the transform matrix in the header constant (zeros with ones at the diagonal) and thus visualize the rotation with mrview?

Bauke

---

<div class="post-metadata">

**Author:** ![rsmith](https://community.mrtrix.org/user_avatar/community.mrtrix.org/rsmith/32/2672_2.png) [@rsmith](https://community.mrtrix.org/u/rsmith)\
**Post date:** [May 7, 2021, 2:49am UTC](https://community.mrtrix.org/t/rotate-x-axis-by-90-degrees/796/10 "2021-05-07T02:49:32Z")

</div>

Welcome Bauke!

I think you may have fallen for a common trap with `mrview`. This is something we really should think about making more prominent in the documentation somehow; I’ve also [thought](https://github.com/MRtrix3/mrtrix3/issues/452) about implementing something in the software to make this effect more obvious, but such things regularly fall off the priority list.

I suspect what you’re doing is opening both images within `mrview`, and then switching between them. The problem here is that every time you switch from one image to another, the `mrview` “camera” automatically rotates in order to align itself with the closest axes of the new image. This is done so that when `mrview` is operating in its standard “slice” view, when you change from one image to another, you are still looking at a “slice” of data within the newly shown image. So the reason it looks like the images are in the same position is because the camera is rotating to compensate for the change in position.

There are two ways this can be demonstrated:

1. Turn off the “snap to axes” button in the `mrview` toolbar. Now, when you change images, what is shown in the viewer is an interpolation of the new 3D volume through the 2D plane defined by where the camera had been set for the previous image.

2. Load only one of the two images into `mrview`, and instead load the second image using the Overlay tool.

But if I’m wrong in my diagnosis let us know.

Rob

---

<div class="post-metadata">

**Author:** ![Bauke](https://community.mrtrix.org/letter_avatar_proxy/v4/letter/b/839c29/32.png) [@Bauke](https://community.mrtrix.org/u/Bauke)\
**Post date:** [May 20, 2021, 4:38pm UTC](https://community.mrtrix.org/t/rotate-x-axis-by-90-degrees/796/11 "2021-05-20T16:38:12Z")

</div>

Hi Rob,

Thank you for taking the time to look into my question. I think your suggestion is indeed what was happening.

I opened my 3 images (original, 45 degrees rotation and 90 degrees rotation) separately in mrview. So open an image in mrview, close mrview and then open another image in mrview. The original and 45 degree rotated image looked similar, but the 90 degree rotated image is rotated. So it seemed to me that my 90 degree rotation worked, but my 45 degree rotation did not (although the transformation matrix in the header was changed). However, when I overlay the 45 degree rotated image on the original image as you suggested, I could see that it was 45 degrees rotated.

Thanks again for your explanation!  
Bauke

---

<div class="post-metadata">

**Author:** ![suren](https://community.mrtrix.org/user_avatar/community.mrtrix.org/suren/32/3215_2.png) [@suren](https://community.mrtrix.org/u/suren)\
**Post date:** [September 29, 2021, 10:26pm UTC](https://community.mrtrix.org/t/rotate-x-axis-by-90-degrees/796/12 "2021-09-29T22:26:05Z")

</div>

Hello @david142, I first used permute from MATLAB, and then mrtransform and mrconvert will work fine. Many Thanks.

Suren
