How can I find the spatial calibration factor?

Hi, According to this link http://www.azurs.net/photoblog/resolution-taille-pixels-image.html real heigth of the image (mm)= image heigth of the image (pixel)/image resolution (ppp)
So the spatial calibration factor can be find by this formula??
calibration factor=real heigth of the image (mm)/image heigth (pixel)
thaks in advance

2 Comments

Hello everyone. Please how can i convert a measurement from pixel to mm using code. For example 40pixel to mm.
Your responses will be highly appreciated.
Thank you
pixelsPerMm = 40; % Define the spatial calibration factor
lengthInMm = lengthInPixels / pixelsPerMm; % Convert length in pixels to mm.

Sign in to comment.

Answers (4)

The calibration factor is (the actual "true" height of a known object) / (the height in pixels of that same object in your image).
Then when you measure any other distances, multiply by that factor. See demo code below:
% Code to spatially calibrate and image.
% Code asks user to draw a line and then to specify the length
% of the line in real world units. It then calculates a spatial calibration factor.
% User can then draw lines and have them reported in real world units.
% Take out the next two lines if you're transferring this to your program.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables.
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 20;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(2, 1, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Initialize
units = 'pixels';
spatialCalibration = 1.0;
button = 1;
while button ~= 3
% Get which action the user wants to do.
button = menu('Choose an action', 'Calibrate', 'Measure', 'Exit');
if button == 3
% Bail out because they clicked Exit.
break;
end
% Make caption the instructions.
subplot(2, 1, 1);
title('Left-click first point. Right click last point.', 'FontSize', fontSize);
% Ask user to plot a line.
[x, y, profile] = improfile();
% Restore caption.
title('Original Grayscale Image', 'FontSize', fontSize);
% Calculate distance
distanceInPixels = sqrt((x(1)-x(end))^2 + (y(1)-y(end))^2);
% Plot it.
subplot(2,1,2);
plot(profile);
grid on;
% Initialize
realWorldNumericalValue = distanceInPixels;
caption = sprintf('Intensity Profile Along Line\nThe distance = %f pixels', ...
distanceInPixels);
title(caption, 'FontSize', fontSize);
ylabel('Gray Level', 'FontSize', fontSize);
xlabel('Pixels Along Line', 'FontSize', fontSize);
if button == 1
% They want to calibrate.
% Ask user for a number.
userPrompts = {'Enter true size:','Enter units:'};
defaultValues = {'180', 'cm'};
titleBar = 'Enter known distance';
caUserInput = inputdlg(userPrompts, titleBar, 2, defaultValues);
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
% Initialize.
realWorldNumericalValue = str2double(caUserInput{1});
units = char(caUserInput{2});
% Check for a valid integer.
if isnan(realWorldNumericalValue)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
message = sprintf('I said it had to be an number.\nI will use %d and continue.', distanceInPixels);
uiwait(warndlg(message));
realWorldNumericalValue = distanceInPixels;
units = 'pixels';
spatialCalibration = 1.0;
% continue; % Skip to end of loop.
end
spatialCalibration = realWorldNumericalValue / distanceInPixels;
end
realWorldDistance = distanceInPixels * spatialCalibration;
caption = sprintf('Intensity Profile Along Line\nThe distance = %f pixels = %f %s', ...
distanceInPixels, realWorldDistance, units);
title(caption, 'FontSize', fontSize);
ylabel('Gray Level', 'FontSize', fontSize);
xlabel('Pixels Along Line', 'FontSize', fontSize);
end

15 Comments

Hi, thanks for this code. It's interesting but I don't have the realWorldNumericalValue. I'm asking for a formula to find this in my first message above
real heigth of the image (mm)= image heigth of the image (pixel)/image resolution (ppp)
So the spatial calibration factor can be find by this formula??
calibration factor=real heigth of the image (mm)/image heigth (pixel)
Then you're probably stuck, because I don't think you'd be able to measure the height of the image on your sensor because it's internal to the camera.
So why in this link there's a formula used to find the real dimensions of the image http://www.azurs.net/photoblog/resolution-taille-pixels-image.html
real heigth of the image (mm)= image heigth of the image (pixel)/image resolution (ppp)
Sorry - my French is rather weak. The items that are of interest are
  1. the actual height of the object in the scene
  2. the height of the object's image on the sensor.
  3. the distance between the lens and the object in the scene
  4. the distance of the image to the lens (pretty close to the focal length of the lens)
  5. the number of pixels that the image of the object covers on the sensor
  6. the size of a pixel in microns.
These are all related though equations. Given enough of those quantities, you can calculate the others. If you don't have enough, then you can't calculate the item of interest. Tell me which measurements you have and which you need to measure, calculate, or determine in some way.
thanks
the height of the image on the sensor: 480pixel/72ppp=6.66 pouces so 6.66*25.4=169.164 mm
the distance between the lens and the object in the scene: 36 mm
the distance of the image to the lens (pretty close to the focal length of the lens) 40 mm
FOV=40 mm
the number of pixels that the image of the object covers on the sensor and the size of a pixel in microns: I can't understand what do you mean by these parameters ??
I guarantee you that the size of the image on the sensor is not 169.164 mm, unless you have about the largest sensor in the world. I don't even know if they make any sensors that large.
Let's say your image sensor is 2048 pixels wide by 1536 pixels high. And let's say you imaged a 6 foot tall person. Let's say that the person's image took up half the height of the sensor, so the number of pixels high that the person would be would be 1536/2 = 768 pixels. The size of the pixel in microns would be like if you took your sensor and put it under an electron microscope and looked at all the ccd wells. The size of one CCD well might be 1 micron to maybe as large as 9 microns across.
72 ppp is almost always a display resolution and not a sensor resolution.
If the heigth of the image on the sensor is 480 pixels, how can I conver it in mm
Are you looking for the height of the image on the sensor? If so then you need to know the sensor resolution and size of the sensor.
To give you an idea of the magnitude:
I have a (2010) Nikon Coolpix 8000. The maximum resolution is 4320 x 3240. The sensor size is 1/2.3" -- that is, less than half an inch, but slightly more than 11 mm. 480 pixels / 4320 pixels * 11.0435 mm = 1.2271 mm image height on sensor.
Nikon recently released their Coolpix 7200 with 1/1.7" sensor, 4000 x 3000 resolution. On that, 25.4 mm/inch * 1/1.7 inch = 14.9412 mm sensor, and 480 / 4000 * 14.9412 = 1.7929 mm image height on sensor.
Tomas
Tomas on 15 Dec 2012
Edited: Tomas on 15 Dec 2012
For a device having these caracterestics, what are the heigth and the resolution of the image on the sensor. I have some confusions. 480 pixels is the heigth?? and 1280 X 960 pixels is the resolution??
Résolution : 640 X 480 pixels, 1280 X 960 pixels
Ecran LCD : 2,4" 5,9 cm
No. That device has a sensor that is 960 pixels high and 1280 pixels wide. It can also operate in a mode with 480 pixels high by 640 pixels wide. Perhaps the most common way that is done is to just take the central chunk out of the sensor. The LCD information refers to a display, not the sensor.
I would have thought that a more common way would be averaging sensor pixels in the camera firmware ?
You can do that but I don't think it's more common. It's called binning, and it's where you combine adjacent pixels. It might depend on the camera sensor architecture. I just had a case where I wanted to reduce the resolution of a camera to get a higher frame rate so the users could position an object in front of the camera with no delay/lag. When set up with a lower resolution, it took the central portion of the sensor, so the field of view changed drastically. When I asked for the same field of view and use subsampling (say every 4th pixel) the engineer thought at first he could do it and then said the chip architecture wouldn't allow it but he could do binning. However with binning the image had to be monochrome because it combined adjacent pixels which are different colors (remember it couldn't do subsampling). And this was with a CMOS sensor which are supposed to be more addressable than CCD sensors. Anyway, neither of these was what I wanted so I just went with a lower resolution camera because the higher frame rate was really what was needed more than the resolution.
Hi there,
I am new to matlab code and I was wondering if it was possible to use this code (or something slightly different) to find the spatial calibration of a video? I have a moving object over a grid and I am trying to determine the speed of that object. I have most of the code worked out but struggling to determine the number of pixels/mm of my video, can anyone help?
Thanks

Suppose, I have this image and I want to measure the area of the table surrounded by a red line. This table is rectangular but not looking as a rectangle. Here we can't take pixel values uniform how can we measure the area of this table?

Sign in to comment.

Hi there,
I am new to matlab code and I was wondering if it was possible to use this code (or something slightly different) to find the spatial calibration of a video? I have a moving object over a grid and I am trying to determine the speed of that object. I have most of the code worked out but struggling to determine the number of pixels/mm of my video, can anyone help?
Thanks

3 Comments

Again, you usually have to have an object of known length in the scene that you can measure. You must know the grid spacing. And you can find the spacing in pixels, so that's your spatial calibration. If you watch a certain grid line and know how far it moved, then you can get the relative speed of the camera.
Hi All I have this zigzag line and need to measure the total length of it,
the length law L=sqrt((x(1)-x(2))^2+(y(1)-y(2))^2) can not used straight forward because the complicated shape of the line
You should start your own question. But basically since that is a single wide binary image of a line, you can use sum to get the number of pixels in the line.
LineLengthInPixels = sum(binaryImage(:));
If you want Euclidean distance, so a two pixel long line at an angle of 45 degrees has a length of sqrt(2) instead of 2, you can do that with the sqrt() function and knowledge of where the vertices in your line are.

Sign in to comment.

How to adapt different retinal image resolutions using spatial calibration with a field of view of 45°

1 Comment

I don't know what that means so all I can suggest is that you use my spatial calibration code to calibrate from known things. Perhaps your ophthalmoscope will tell you what the spatial resolution is.

Sign in to comment.

Amine Alileche
Amine Alileche on 29 Apr 2021
Edited: Amine Alileche on 29 Apr 2021
Hi can I apply that demo code to a jpg file ?

6 Comments

What demo code? Just change the file name in imread() and it should be fine. Since you didn't share your code or your workflow, we have no idea why you can't apply code to a JPG file that you could apply to a PNG file previously. Also, don't use JPG files if you can help it - they have compression artifacts that PNG files don't.
thanks; am asking about the code you posted below. and in fact I need to detect the two boundaries of the file to plot temperatures along it's length so I have a csv file and a png file knowing that I know only one dimension (the real dimension) on png . Also png and csv doesn't have same size . see attached files
Sorry it's been so long -- I've been busy with my real job. Did you solve it yet? If not, I think it would be best to start your own question rather than try to solve it here, in an answer to @Tomas's 9 year old question.
it's okay thank youu :) I have posted
@Amine Alileche, I answered it there in that question. I'm attaching the results here too.
% Demo by Image Analyst.
clc; % Clear command window.
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = 18;
markerSize = 20;
% Read in file.
fileName = '125-1.csv';
data = readmatrix(fileName);
[rows, columns] = size(data);
nexttile;
imshow(data, []);
axis('on', 'image');
% Scan rows for max in each column.
minValue = min(data(:));
maxProfile = minValue * ones(rows, 1);
colOfMax = ones(rows, 1);
for row = 1 : rows
[maxProfile(row), colOfMax(row)] = max(data(row, :));
end
% Plot red line over image.
hold on;
plot(colOfMax, 1:rows, 'r-', 'LineWidth', 2);
% Plot intensity along row.
nexttile;
plot(maxProfile, 'b-', 'LineWidth', 2);
grid on;
xlabel('row', 'FontSize', fontSize);
ylabel('Intensity', 'FontSize', fontSize);
% Maximize image.
g = gcf;
g.WindowState = 'maximized'
Note: the graph is the vertical profile along the centerline of the wire (going down row by row).
It is not a cross section of the wire horizontally.
Hi I have posted new topic if you can help. Thank you :)

Sign in to comment.

Asked:

on 8 Dec 2012

Commented:

on 31 May 2021

Community Treasure Hunt

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

Start Hunting!