# How to convert from pixels to mm and from mm to pixels?

51 views (last 30 days)
sana3 sal on 2 May 2018
Edited: Alfonso on 3 May 2018
Hello, I need to convert number of pixels to mm, and the mm to pixels, as in the next figure to compare between the results. the upper image taken from DICOM viewer software, i measure the height manually in mm (11.72mm) i need to convert it to pixels to compare it with the auto calculation shown in the lower image (24 pixel). Alfonso on 3 May 2018
Edited: Alfonso on 3 May 2018
In order to obtain the number of pixels of the yellow line which you know it's real distance is 11.72 mm in the vertebral column image, you could try this
% Draw a line convering the desired distance and get the number of pixels
imshow(my_image)
% Retrieve the intensity values of pixels along the line for 1000 points
[cx,cy, rgbValues, xi,yi] = improfile(1000);
oimg = findobj(gca,'Type','image'); %image which is open
img = get(oimg, 'CData'); % image data
d_line = round(sqrt((xi(1)-xi(2))^2 + (yi(1)-yi(2))^2))
[cx,cy, rgbValues] = improfile(img, xi, yi, d_line);
rgbValues = squeeze(rgbValues); % change dim
% distance in pixels of the line drawn
d_pixels= sqrt( (xi(2)-xi(1)).^2 + (yi(2)-yi(1)).^2);
% plot drawn line
hold on;
plot(xi, yi, 'r-');
For the first image you would finally know the distance in pixels and the real distance (mm) , being able to define the conversion factor: mm/px or px/mm, but only for elements in that specific image.
In order to compare between the pixel distances of each image to see if they match, you should get a scale factor of each image (haven't tried the code right now)
scaleFactor1=d_pixels/image1_rows
And for the second image the same,
scaleFactor2=height_px/image2_rows
Then you could scale one image to the other and finally compare the heights.
S=scaleFactor1/scaleFactor2
height_px_scaled=S*height_px

Walter Roberson on 3 May 2018