How to get pixels per millimeter from image of a scale

4 views (last 30 days)
How can I get the number of pixels per millimeter in this image of a grid? I was told that I should get it by taking the Fourier transform, but I do not know how.

Answers (2)

DGM
DGM on 17 Dec 2023
Why bother with all that for one image?
% an image
inpict = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1570782/image.png');
% display the image
imshow(inpict)
% pick two points a known distance apart
[x y] = ginput(2);
% get the distances
Dpx = norm(diff([x y],1,1)); % in pixels
Dmm = 16; % known distance in mm
% get scaling factor
pxpermm = Dpx/Dmm
... or just throw it in any image editor and measure it.
Scaling factor is 1242px/16mm (assuming the scale is mm)

Image Analyst
Image Analyst on 17 Dec 2023
Whoever told you to use the Fourier transform to spatially calibrate your image was either (1) teasing you, (2) pulling a prank on you to send you on a wild goose chase, (3) making a joke (that went over your head), or (4) woefully unskilled in image processing. 😕
What you want to do is to simply mark two point on your scale and get the distance between them, and then compute your spatial calibration factor in mm per pixel.
grayImage = imread('adrian.png');
imshow(grayImage, []);
uiwait(helpdlg('Position endpoints at 0 and 16 mm.'))
roi = drawline(gca)
xy = roi.Position
distance = sqrt((xy(1,1) - xy(2,1)).^2 + (xy(1,2) - xy(2,2)).^2)
realWorldDistance = 16; % mm
mmPerPixel = realWorldDistance / distance
See attached demo for a full demo.
Once you have mmPerPixel then you can get distances and areas in pixels in your algorithm and then multiply the distances by mmPerPixel and areas by mmPerPixel^2 to get the distances in mm and areas in square mm.

Products


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!