# Relationship between pixels and mm from Matlab

**URL:** <https://discourse.slicer.org/t/relationship-between-pixels-and-mm-from-matlab/427>\
**Category:** Development\
**Created:** [June 2, 2017, 4:14pm UTC](https://discourse.slicer.org/t/relationship-between-pixels-and-mm-from-matlab/427 "2017-06-02T16:14:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marina\_CM](https://avatars.discourse-cdn.com/v4/letter/m/53a042/32.png) [@Marina\_CM](https://discourse.slicer.org/u/Marina_CM)\
**Post date:** [June 2, 2017, 4:14pm UTC](https://discourse.slicer.org/t/relationship-between-pixels-and-mm-from-matlab/427/1 "2017-06-02T16:14:30Z")

</div>

Hi all again!

I’d like to know how can I get the relationship between pixels and mm from Matlab, as I know it has to be somewhere because with the Slicer Ruler tool, it’s possible to measure structures in mm.

Thank you very much in advance.

BR,

Marina C.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [June 2, 2017, 4:35pm UTC](https://discourse.slicer.org/t/relationship-between-pixels-and-mm-from-matlab/427/2 "2017-06-02T16:35:03Z")

</div>

It is defined in `img.ijkToLpsTransform` as a 4x4 homogeneous transformation matrix. From `nrrdread.m`:

```
 img.ijkToLpsTransform: pixel (IJK) to physical (LPS, assuming 'space' is 'left-posterior-superior')
  coordinate system transformation, the origin of the IJK coordinate system is (1,1,1) to match Matlab matrix indexing

```

Note that this matrix include origin, spacing, and axis directions of the volume.

You can convert a coordinate from IJK (voxel) to LPS (physical, in mm) coordinate system by multiplying the coordinate by ijkToLpsTransform from the left. Spacing is the norm of column vectors of ijkToLpsTransform.

See more information for example here: [https://www.slicer.org/wiki/Coordinate\_systems](https://www.slicer.org/wiki/Coordinate_systems)

---

<div class="post-metadata">

**Author:** ![Marina\_CM](https://avatars.discourse-cdn.com/v4/letter/m/53a042/32.png) [@Marina\_CM](https://discourse.slicer.org/u/Marina_CM)\
**Post date:** [June 2, 2017, 4:51pm UTC](https://discourse.slicer.org/t/relationship-between-pixels-and-mm-from-matlab/427/3 "2017-06-02T16:51:00Z")

</div>

Thank you very much!  
I’ll use this info 🙂

BR,

Marina C.

---

<div class="post-metadata">

**Author:** ![Marina\_CM](https://avatars.discourse-cdn.com/v4/letter/m/53a042/32.png) [@Marina\_CM](https://discourse.slicer.org/u/Marina_CM)\
**Post date:** [June 5, 2017, 11:23pm UTC](https://discourse.slicer.org/t/relationship-between-pixels-and-mm-from-matlab/427/4 "2017-06-05T23:23:14Z")

</div>

Hi all,

Using the valuable information provided, I tried to measure the distance in mm between two points with matlab, but I think I’m doing something wrong:

Assuming that `M = cli_imageread(inputParams.inputvolume);`  
And `Point1` and `Point2` are the coordinates of two different points from M which are in the same slice, I’ve used the next code but I must have misunderstood something:

```
Point1 = [x1, y1, z1, 1];
Point2 = [x2, y2, z2, 1];

spacing_x = norm(img.ijkToLpsTransform(:,1));
spacing_y = norm(img.ijkToLpsTransform(:,2));

Point1_LPS = img.ijkToLpsTransform*(Point1');
Point2_LPS = img.ijkToLpsTransform*(Point2');

dis_x = (Point1_LPS(1)-Point2_LPS(1))*spacing_x;
dis_y = (Point1_LPS(1)-Point2_LPS(1))*spacing_y;
distance_p = sqrt((dis_x^2) + (dis_y^2));

```

How should I use this inputs to find the right distance?

Thank you very much.

Best Regards,

Marina C.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [June 6, 2017, 12:06am UTC](https://discourse.slicer.org/t/relationship-between-pixels-and-mm-from-matlab/427/5 "2017-06-06T00:06:48Z")

</div>

Don’t compute spacing. Use the matrix to convert point coordinates from IJK to LPS coordinate system. Unit of LPS coordinate system is mm, so distance that you compute in LPS is the distance you need. Something like this:

`norm( img.ijkToLpsTransform*[p1_i, p1_j, p1_k, 1]' - img.ijkToLpsTransform*[p2_i, p2_j, p2_k, 1]' )`

---

<div class="post-metadata">

**Author:** ![Marina\_CM](https://avatars.discourse-cdn.com/v4/letter/m/53a042/32.png) [@Marina\_CM](https://discourse.slicer.org/u/Marina_CM)\
**Post date:** [June 6, 2017, 4:28pm UTC](https://discourse.slicer.org/t/relationship-between-pixels-and-mm-from-matlab/427/6 "2017-06-06T16:28:49Z")

</div>

Thank you very much! It worked 🙂

BR

Marina C.
