Relationship between pixels and mm from Matlab

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.


Marina C.

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:

1 Like

Thank you very much!
I’ll use this info :slight_smile:


Marina C.

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.

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]' )

1 Like

Thank you very much! It worked :slight_smile:


Marina C.