How to convert from pixels to mm and from mm to pixels?
36 次查看(过去 30 天)
显示 更早的评论
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).
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/190269/image.png)
0 个评论
回答(2 个)
Alfonso
2018-5-3
编辑:Alfonso
2018-5-3
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Convert Image Type 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!