close all;
clear;
clc;
workspace;
format longg;
format compact;
fontSize = 20;
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
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')
return;
end
end
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
fullFileName = baseFileName;
if ~exist(fullFileName, 'file')
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
[rows columns numberOfColorBands] = size(grayImage);
subplot(2, 1, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
units = 'pixels';
spatialCalibration = 1.0;
button = 1;
while button ~= 3
button = menu('Choose an action', 'Calibrate', 'Measure', 'Exit');
if button == 3
break;
end
subplot(2, 1, 1);
title('Left-click first point. Right click last point.', 'FontSize', fontSize);
[x, y, profile] = improfile();
title('Original Grayscale Image', 'FontSize', fontSize);
distanceInPixels = sqrt((x(1)-x(end))^2 + (y(1)-y(end))^2);
subplot(2,1,2);
plot(profile);
grid on;
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
userPrompts = {'Enter true size:','Enter units:'};
defaultValues = {'180', 'cm'};
titleBar = 'Enter known distance';
caUserInput = inputdlg(userPrompts, titleBar, 2, defaultValues);
if isempty(caUserInput),return,end;
realWorldNumericalValue = str2double(caUserInput{1});
units = char(caUserInput{2});
if isnan(realWorldNumericalValue)
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;
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
2 Comments
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/56087-how-can-i-find-the-spatial-calibration-factor#comment_1184433
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/56087-how-can-i-find-the-spatial-calibration-factor#comment_1184433
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/56087-how-can-i-find-the-spatial-calibration-factor#comment_1184498
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/56087-how-can-i-find-the-spatial-calibration-factor#comment_1184498
Sign in to comment.