How can I find the spatial calibration factor?
8 views (last 30 days)
Show older comments
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
JAMES RABBA
on 5 Dec 2020
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
Image Analyst
on 5 Dec 2020
pixelsPerMm = 40; % Define the spatial calibration factor
lengthInMm = lengthInPixels / pixelsPerMm; % Convert length in pixels to mm.
Answers (4)
Image Analyst
on 10 Dec 2012
Edited: Image Analyst
on 10 Dec 2012
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
Catherine Perna
on 22 May 2014
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
MOHIT
on 19 Jul 2016
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?
![](/matlabcentral/answers/uploaded_files/56315/table.jpg)
Catherine Perna
on 22 May 2014
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
Hazim Nasir
on 20 Aug 2016
Hi All I have this zigzag line and need to measure the total length of it,
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/184418/image.png)
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
Image Analyst
on 20 Aug 2016
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.
Aiswarya M D
on 7 Oct 2016
How to adapt different retinal image resolutions using spatial calibration with a field of view of 45°
1 Comment
Image Analyst
on 8 Oct 2016
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.
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
Image Analyst
on 4 May 2021
@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'
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/607610/image.png)
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.
See Also
Categories
Find more on Calibration and Sensor Fusion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!